Skip to content

Commit

Permalink
Merge pull request #5 from Genymobile/dev/support-API-Token
Browse files Browse the repository at this point in the history
Dev/support api token
  • Loading branch information
thomascarpentier authored Jun 28, 2024
2 parents 8253cde + f3def9f commit 4a3926f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 19 deletions.
36 changes: 34 additions & 2 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ app:
envs:
# If you want to share this step into a StepLib
- BITRISE_STEP_ID: genymotion-saas-install-gmsaas-cli
- BITRISE_STEP_VERSION: "0.0.2"
- BITRISE_STEP_VERSION: "0.0.3"
- BITRISE_STEP_GIT_CLONE_URL: https://github.com/genymobile/bitrise-step-genymotion-saas-install-gmsaas-cli.git
- MY_STEPLIB_REPO_FORK_GIT_URL: $MY_STEPLIB_REPO_FORK_GIT_URL
# Define these in your .bitrise.secrets.yml
- GMCLOUD_SAAS_EMAIL: $GMCLOUD_SAAS_EMAIL
- GMCLOUD_SAAS_PASSWORD: $GMCLOUD_SAAS_PASSWORD
- GMCLOUD_SAAS_API_TOKEN: $GMCLOUD_SAAS_API_TOKEN

workflows:
ci:
Expand Down Expand Up @@ -39,14 +40,45 @@ workflows:
title: Genymotion SaaS - Install gmsaas
run_if: "true"
inputs:
- gmsaas_version: "1.9.0"
- gmsaas_version: "1.11.0"
- email: $GMCLOUD_SAAS_EMAIL
- password: $GMCLOUD_SAAS_PASSWORD
- script:
inputs:
- content: |
#!/bin/bash
gmsaas auth whoami
test-api-token:
before_run:
- audit-this-step
steps:
- change-workdir:
title: Switch working dir to test / _tmp dir
description: |-
To prevent step testing issues, like referencing relative
files with just './some-file' in the step's code, which would
work for testing the step from this directory directly
but would break if the step is included in another `bitrise.yml`.
run_if: true
inputs:
- path: ./_tmp
- is_create_path: true
- path::./:
title: Genymotion SaaS - Install gmsaas
run_if: "true"
inputs:
- gmsaas_version: "1.11.0"
- api_token: $GMCLOUD_SAAS_API_TOKEN
- script:
inputs:
- content: |
#!/bin/bash
gmsaas doctor
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Error"
fi
exit $retVal
Expand Down
41 changes: 30 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ var isError bool = false

// Config ...
type Config struct {
GMCloudSaaSEmail string `env:"email,required"`
GMCloudSaaSPassword stepconf.Secret `env:"password,required"`
GMCloudSaaSGmsaasVersion string `env:"gmsaas_version"`
GMCloudSaaSEmail string `env:"email"`
GMCloudSaaSPassword stepconf.Secret `env:"password"`
GMCloudSaaSGmsaasVersion string `env:"gmsaas_version"`
GMCloudSaaSAPIToken stepconf.Secret `env:"api_token"`
}

// install gmsaas if not installed.
Expand All @@ -29,15 +30,18 @@ func ensureGMSAASisInstalled(version string) error {

var installCmd *exec.Cmd
if version != "" {
installCmd = exec.Command("pip3", "install", "gmsaas=="+version)
installCmd = exec.Command("pip3", "install", "gmsaas=="+version, "--break-system-packages")
} else {
installCmd = exec.Command("pip3", "install", "gmsaas")
installCmd = exec.Command("pip3", "install", "gmsaas", "--break-system-packages")
}

if out, err := installCmd.CombinedOutput(); err != nil {
return fmt.Errorf("%s failed, error: %s | output: %s", installCmd.Args, err, out)
}

// Execute asdf reshim to update PATH
exec.Command("asdf", "reshim", "python").CombinedOutput()

if version != "" {
log.Infof("gmsaas %s has been installed.", version)
} else {
Expand Down Expand Up @@ -88,13 +92,24 @@ func configureAndroidSDKPath() {
}
}

func login(username, password string) {
func login(api_token, username, password string) {
log.Infof("Login Genymotion Account")
cmd := command.New("gmsaas", "auth", "login", username, password)
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
abortf("Failed to log with gmsaas, error: error: %s | output: %s", cmd.PrintableCommandArgs(), err, out)

var cmd *exec.Cmd
if api_token != "" {
cmd = exec.Command("gmsaas", "auth", "token", api_token)
} else if username != "" && password != "" {
cmd = exec.Command("gmsaas", "auth", "login", username, password)
} else {
abortf("Invalid arguments. Must provide either a token or both email and password.")
return
}

if out, err := cmd.CombinedOutput(); err != nil {
abortf("Failed to login with gmsaas, error: error: %s | output: %s", cmd.Args, err, out)
return
}

log.Infof("Logged to Genymotion Cloud SaaS platform")
}

Expand All @@ -115,7 +130,11 @@ func main() {
printError("Failed to export %s, error: %v", "GMSAAS_USER_AGENT_EXTRA_DATA", err)
}

login(c.GMCloudSaaSEmail, string(c.GMCloudSaaSPassword))
if c.GMCloudSaaSAPIToken != "" {
login(string(c.GMCloudSaaSAPIToken), "", "")
} else {
login("", c.GMCloudSaaSEmail, string(c.GMCloudSaaSPassword))
}

// --- Exit codes:
// The exit code of your Step is very important. If you return
Expand Down
17 changes: 11 additions & 6 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,32 @@ toolkit:


inputs:
- api_token: ""
opts:
title: Genymotion Cloud SaaS API Token
summary: ""
description: |-
API Token to authenticate to your Genymotion Cloud SaaS account. If you don't have an account please register on [https://cloud.geny.io](https://cloud.geny.io/?&utm_source=web-referral&utm_medium=docs&utm_campaign=bitrise&utm_content=signup) and create an [API Token](https://cloud.geny.io/api)
- email: ""
opts:
title: Genymotion Cloud SaaS email
summary: ""
description: |-
Email of your Genymotion Cloud SaaS account, if you don't have an account please create it first on [https://cloud.geny.io](https://cloud.geny.io/?&utm_source=web-referral&utm_medium=docs&utm_campaign=bitrise&utm_content=signup)
is_required: true
DEPRECATED : Email of your Genymotion Cloud SaaS account, if you don't have an account please create it first on [https://cloud.geny.io](https://cloud.geny.io/?&utm_source=web-referral&utm_medium=docs&utm_campaign=bitrise&utm_content=signup)
- password: ""
opts:
title: Genymotion Cloud SaaS password
summary: ""
description: |-
Password of your Genymotion Cloud SaaS account.
DEPRECATED: Password of your Genymotion Cloud SaaS account.
is_sensitive: true
is_required: true

- gmsaas_version: "1.9.0"
- gmsaas_version: "1.11.0"
opts:
title: gmsaas version
summary: ""
description: |-
Install a specific version of gmsaas, per default it will install the latest gmsaas compatible : 1.9.0
Install a specific version of gmsaas, per default it will install the latest gmsaas compatible : 1.11.0

0 comments on commit 4a3926f

Please sign in to comment.