Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ATMOS_CLI_CONFIG_PATH ENV var. Add functionality to define atmos custom CLI commands #168

Merged
merged 23 commits into from
Jun 27, 2022

Conversation

aknysh
Copy link
Member

@aknysh aknysh commented Jun 25, 2022

what

  • Add ATMOS_CLI_CONFIG_PATH ENV var
  • Detect more YAML stack misconfigurations
  • Add functionality to define atmos custom CLI commands

why

  • ATMOS_CLI_CONFIG_PATH ENV var allows specifying the location of atmos.yaml CLI config file. This is useful for CI/CD environments (e.g. Spacelift) where an infrastructure repository gets loaded into a custom path and atmos.yaml is not in the locations where atmos expects to find it (no need to copy atmos.yaml into /usr/local/etc/atmos/atmos.yaml)

  • Detect more YAML stack misconfigurations, e.g. when the same tenant/environment/stage is defined in more than one top-level YAML stack config file (directly or via imports).

    For example, if the same var.tenant = tenant1 is specified for tenant1-ue2-dev and tenant2-ue2-dev stacks, the
    command atmos describe component test/test-component-override -s tenant1-ue2-dev will throw this error

    Searching for stack config where the component 'test/test-component-override' is defined
    Found config for the component 'test/test-component-override' for the stack 'tenant1-ue2-dev' in the file 'tenant1/ue2/dev'
    Found config for the component 'test/test-component-override' for the stack 'tenant1-ue2-dev' in the file 'tenant2/ue2/dev'
    
    Found duplicate config for the component 'test/test-component-override' for the stack 'tenant1-ue2-dev' in the files: tenant1/ue2/dev, tenant2/ue2/dev.
    Check that all context variables in the stack name pattern '{tenant}-{environment}-{stage}' are correctly defined in the files and not duplicated.
    Check that imports are valid.
    
  • Allow extending atmos with custom commands. Custom commands can be defined in atmos.yaml CLI config file. Custom commands support subcommands at any level (e.g. atmos my-command subcommand1 suncommand2 argument1 argument2 flag1 flag2)

# Custom CLI commands
commands:
  - name: tf
    description: Execute terraform commands
    # subcommands
    commands:
      - name: plan
        description: This command plans terraform components
        arguments:
          - name: component
            description: Name of the component
        flags:
          - name: stack
            shorthand: s
            description: Name of the stack
            required: true
        env:
          - key: ENV_VAR_1
            value: ENV_VAR_1_value
          - key: ENV_VAR_2
            # `valueCommand` is an external command to execute to get the value for the ENV var
            # Either 'value' or 'valueCommand' can be specified for the ENV var, but not both
            valueCommand: echo ENV_VAR_2_value
        # steps support Go templates
        steps:
          - atmos terraform plan {{ .Arguments.component }} -s {{ .Flags.stack }}
  - name: terraform
    description: Execute terraform commands
    # subcommands
    commands:
      - name: provision
        description: This command provisions terraform components
        arguments:
          - name: component
            description: Name of the component
        flags:
          - name: stack
            shorthand: s
            description: Name of the stack
            required: true
        # ENV var values support Go templates
        env:
          - key: ATMOS_COMPONENT
            value: "{{ .Arguments.component }}"
          - key: ATMOS_STACK
            value: "{{ .Flags.stack }}"
        steps:
          - atmos terraform plan $ATMOS_COMPONENT -s $ATMOS_STACK
          - atmos terraform apply $ATMOS_COMPONENT -s $ATMOS_STACK
  - name: play
    description: This command plays games
    steps:
      - echo Playing...
    # subcommands
    commands:
      - name: hello
        description: This command says Hello world
        steps:
          - echo Saying Hello world...
          - echo Hello world
      - name: ping
        description: This command plays ping-pong
        steps:
          - echo Playing ping-pong...
          - echo pong

Custom commands support Go templates and ENV vars in commands steps, and Go templates in ENV vars values, as well as allow specifying an external executable to be called to get the value for an ENV var.

They are automatically added to atmos help:

Available Commands:
  aws         Execute 'aws' commands
  completion  Generate the autocompletion script for the specified shell
  describe    Execute 'describe' commands
  helmfile    Execute 'helmfile' commands
  help        Help about any command
  play        This command plays games
  terraform   Execute 'terraform' commands
  tf          Execute terraform commands
  validate    Execute 'validate' commands
  vendor      Execute 'vendor' commands
  version     Print the CLI version
  workflow    Execute a workflow

Custom commands test

atmos play ping
Executing command:
/bin/echo Playing ping-pong...
Playing ping-pong...

Executing command:
/bin/echo pong
pong
atmos play hello
Executing command:
/bin/echo Saying Hello world...
Saying Hello world...

Executing command:
/bin/echo Hello world
Hello world
atmos terraform provision test/test-component-override -s tenant1-ue2-dev
Using ENV vars:
ATMOS_COMPONENT=test/test-component-override
ATMOS_STACK=tenant1-ue2-dev

Executing command:
/usr/local/bin/atmos terraform plan test/test-component-override -s tenant1-ue2-dev

....

Executing command:
/usr/local/bin/atmos terraform apply test/test-component-override -s tenant1-ue2-dev
atmos tf plan test/test-component-override -s tenant1-ue2-dev
# This command gets the value for the ENV var by calling an external executable
Executing command:
/bin/echo ENV_VAR_2_value

Executing command:
/usr/local/bin/atmos terraform plan test/test-component-override -s tenant1-ue2-dev

references

@aknysh aknysh added the patch A minor, backward compatible change label Jun 25, 2022
@aknysh aknysh requested a review from a team as a code owner June 25, 2022 22:53
@aknysh aknysh self-assigned this Jun 25, 2022
@aknysh aknysh temporarily deployed to preview June 25, 2022 22:53 Inactive
@aknysh aknysh changed the title Add ATMOS_CLI_CONFIG_PATH ENV var. Add functionality to specify atmos custom commands Add ATMOS_CLI_CONFIG_PATH ENV var. Add functionality to define atmos custom CLI commands Jun 25, 2022
cmd/cmd_utils.go Outdated Show resolved Hide resolved
cmd/cmd_utils.go Outdated Show resolved Hide resolved
cmd/cmd_utils.go Outdated Show resolved Hide resolved
cmd/cmd_utils.go Outdated Show resolved Hide resolved
nitrocode
nitrocode previously approved these changes Jun 27, 2022
Copy link
Member

@nitrocode nitrocode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of nitpicks but looks good to me

Co-authored-by: nitrocode <[email protected]>
@aknysh aknysh temporarily deployed to preview June 27, 2022 12:10 Inactive
@aknysh aknysh temporarily deployed to preview June 27, 2022 12:14 Inactive
@osterman
Copy link
Member

This is beautiful! So amazing we can now add a subcommand to any existing command or add totally new commands/subcommands.

@@ -214,6 +214,16 @@
"spec": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add something to the readme regarding this or will updating the website suffice ?

Also can we add an "inspiration" page to show that we looked into ahoy and choria-io to help build this interface ?

https://github.com/choria-io/appbuilder
https://github.com/ahoy-cli/ahoy

@nitrocode nitrocode mentioned this pull request Jun 27, 2022
Copy link
Member

@nitrocode nitrocode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nitpicks. I think we should have an inspiration section. I don't want to forget it so I'll write up a new ticket #169.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch A minor, backward compatible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants