Skip to content

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

Merged
Andriy Knysh (aknysh) merged 23 commits into
masterfrom
update-docs-5
Jun 27, 2022
Merged

Andriy Knysh (aknysh) merged 23 commits into
masterfrom
update-docs-5

Conversation

@aknysh

@aknysh Andriy Knysh (aknysh) commented Jun 25, 2022 •

Copy link
Copy Markdown
Member

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 Andriy Knysh (aknysh) self-assigned this Jun 25, 2022
@aknysh
Andriy Knysh (aknysh) temporarily deployed to preview June 25, 2022 22:53 Inactive
@aknysh Andriy Knysh (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
Comment thread cmd/cmd_utils.go Resolved
Comment thread cmd/cmd_utils.go Resolved
Comment thread cmd/cmd_utils.go Resolved
Comment thread cmd/cmd_utils.go Resolved
Comment thread cmd/cmd_utils.go Resolved
Comment thread cmd/cmd_utils.go Resolved
Comment thread cmd/cmd_utils.go Resolved
Comment thread internal/exec/shell_utils.go Resolved
Comment thread pkg/config/schema.go Resolved
Comment thread pkg/config/schema.go Resolved
nitrocode
RB (nitrocode) previously approved these changes Jun 27, 2022

@nitrocode RB (nitrocode) left a comment

Copy link
Copy Markdown
Member

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
Andriy Knysh (aknysh) temporarily deployed to preview June 27, 2022 12:10 Inactive
@aknysh
Andriy Knysh (aknysh) temporarily deployed to preview June 27, 2022 12:14 Inactive
@osterman

Copy link
Copy Markdown
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
Copy Markdown
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 RB (nitrocode) left a comment

Copy link
Copy Markdown
Member

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.

This branch was previously deployed

1 inactive deployment
preview — 2f159e7a Deployed Jun 27, 2022 by aknysh
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