github.com/verrazzano/verrazzano@v1.7.1/tools/psr/tests/pkg/psrctlcli/psrctl_cli.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package psrctlcli
     5  
     6  import (
     7  	"encoding/json"
     8  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/scenario"
     9  	"os"
    10  	"os/exec"
    11  
    12  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    13  	vzos "github.com/verrazzano/verrazzano/pkg/os"
    14  )
    15  
    16  // Debug is set from a platform-operator arg and sets the helm --debug flag
    17  var Debug bool
    18  
    19  // cmdRunner needed for unit tests
    20  var runner vzos.CmdRunner = vzos.DefaultRunner{}
    21  
    22  const PsrctlCmdKey = "PSR_COMMAND"
    23  
    24  // GetPsrctlCmd Returns the path to the psrctl command for the test run
    25  func GetPsrctlCmd() string {
    26  	psrcmd := os.Getenv(PsrctlCmdKey)
    27  	if psrcmd == "" {
    28  		psrcmd = "psrctl"
    29  	}
    30  	return psrcmd
    31  }
    32  
    33  // IsScenarioRunning Returns true if the named scenario is running
    34  func IsScenarioRunning(log vzlog.VerrazzanoLogger, scenarioName string, namespace string) bool {
    35  	_, found := FindScenario(log, scenarioName, namespace)
    36  	return found
    37  }
    38  
    39  // FindScenario Returns true if the named scenario is running
    40  func FindScenario(log vzlog.VerrazzanoLogger, scenarioID string, namespace string) (scenario.Scenario, bool) {
    41  	runningScenarios, err := ListScenarios(log, namespace)
    42  	if err != nil {
    43  		log.Errorf("Error listing scenarios: %s", err.Error())
    44  		return scenario.Scenario{}, false
    45  	}
    46  	for _, scenario := range runningScenarios {
    47  		if scenario.ScenarioManifest.ID == scenarioID {
    48  			return scenario, true
    49  		}
    50  	}
    51  	return scenario.Scenario{}, false
    52  }
    53  
    54  // ListScenarios Lists any running scenarios in the specified namespace
    55  func ListScenarios(log vzlog.VerrazzanoLogger, namespace string) ([]scenario.Scenario, error) {
    56  	// Helm get values command will get the current set values for the installed chart.
    57  	// The output will be used as input to the helm upgrade command.
    58  	args := []string{"list", "-o", "json"}
    59  	if namespace != "" {
    60  		args = append(args, "--namespace")
    61  		args = append(args, namespace)
    62  	}
    63  
    64  	psrctlCmd := GetPsrctlCmd()
    65  	log.Infof("psrctl: %s", psrctlCmd)
    66  	cmd := exec.Command(psrctlCmd, args...)
    67  	log.Debugf("Running command to list scenarios: %s", cmd.String())
    68  	stdout, stderr, err := runner.Run(cmd)
    69  	if err != nil {
    70  		log.Errorf("Failed to list scenarios for namespace %s: stderr %s", namespace, string(stderr))
    71  		return []scenario.Scenario{}, err
    72  	}
    73  
    74  	//  Log get values output
    75  	log.Debugf("Successfully listed scenarios in namespace %s: %v", namespace, string(stdout))
    76  	var scenarios []scenario.Scenario
    77  	if err := json.Unmarshal(stdout, &scenarios); err != nil {
    78  		return nil, err
    79  	}
    80  	return scenarios, nil
    81  }
    82  
    83  // StartScenario Starts a PSR scenario in the specified namespace.
    84  func StartScenario(log vzlog.VerrazzanoLogger, scenario string, namespace string, additionalArgs ...string) ([]byte, []byte, error) {
    85  	// Helm get values command will get the current set values for the installed chart.
    86  	// The output will be used as input to the helm upgrade command.
    87  	args := []string{"start", "-s", scenario, "-n", namespace}
    88  	args = append(args, additionalArgs...)
    89  
    90  	psrctlCmd := GetPsrctlCmd()
    91  	cmd := exec.Command(psrctlCmd, args...)
    92  	log.Debugf("Run scenario command: %s", cmd.String())
    93  	return runner.Run(cmd)
    94  }
    95  
    96  // StopScenario Starts a PSR scenario in the specified namespace.
    97  func StopScenario(log vzlog.VerrazzanoLogger, scenario string, namespace string, additionalArgs ...string) ([]byte, []byte, error) {
    98  	// Helm get values command will get the current set values for the installed chart.
    99  	// The output will be used as input to the helm upgrade command.
   100  	args := []string{"stop", "-s", scenario, "-n", namespace}
   101  	args = append(args, additionalArgs...)
   102  
   103  	psrctlCmd := GetPsrctlCmd()
   104  	cmd := exec.Command(psrctlCmd, args...)
   105  	log.Debugf("Run scenario command: %s", cmd.String())
   106  	return runner.Run(cmd)
   107  }