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

Allow selection of default workflow #244

Closed
ldormoy opened this issue Aug 28, 2018 · 6 comments
Closed

Allow selection of default workflow #244

ldormoy opened this issue Aug 28, 2018 · 6 comments
Labels
feature New functionality/enhancement

Comments

@ldormoy
Copy link

ldormoy commented Aug 28, 2018

Currently, the altantis.yaml config must include the definition of all projects.

My use case is quite simple: I only use atlantis.yaml to update the workflow, as I use terragrunt.
My terragrunt repository structure is quite complex, as I use a multi-account AWS environment, where each account and region have their own repository.

I would like to define a custom workflow and set it as default without losing the atlantis "directory discovery".

The atlantis.yaml would then look like:

version: 2
workflows:
  terragrunt:
    - default
    plan:
      steps:
      - run: terragrunt plan -no-color -out $PLANFILE
    apply:
      steps:
      - run: terragrunt apply -no-color $PLANFILE
@lkysow
Copy link
Member

lkysow commented Aug 28, 2018

Hi Laurent, yes I can see that it would be a total pain to have to specify each directory. Maybe the config could be like

version: 2
workflows:
  default:
    plan:
    ...

Where Atlantis recognizes the default keyword and will use that.

When I first added the atlantis.yaml file I knew there would be a desire to generalize some of it's config and make it more DRY but I wasn't sure exactly what ways people would need to do that. I'll take your feedback and others into account and come up with something that's more generic.

For now, you'll have to specify each directory though. You could probably write a quick script that would output all the

- dir: my/dir
  workflow: terragrunt

for you for each of your projects.

@ldormoy
Copy link
Author

ldormoy commented Aug 29, 2018

Hi Luke, your config proposal looks good to me!

And yes for now I'll go with the manually-triggered script solution, along with a local pre-commit hook to ensure all terragrunt directories are tracked by atlantis.

@mechastorm
Copy link

I do like a default workflow option.

@ldormoy
Copy link
Author

ldormoy commented Oct 8, 2018

For the record I ended up writing a script to generate a new atlantis.yaml.

Please note that it's meant to detect a terragrunt config, you'll have to adapt it to your needs.
Example command: scripts/atlantis_generator.py -o atlantis.yaml .

usage: atlantis_generator.py [-h] [--output FILE] PATH

Generate an atlantis.yaml config from a terragrunt repository.

positional arguments:
  PATH                  path to the terragrunt repository

optional arguments:
  -h, --help            show this help message and exit
  --output FILE, -o FILE
                        name of the created file. If no --output flag is used,
                        the script will output to stdout
#!/usr/bin/env python

#from collections import OrderedDict
import argparse
import os
import os.path
from yaml import dump

"""A script that can be used to generate an atlantis.yaml from the provided path."""
       
def find_terragrunt_projects(path):
  """Return a list of terragrunt project paths."""
  projects = []  

  # list of directories to exclude from the config
  exclude = set(['.terragrunt-cache'])

  # return a list of all project paths
  for root, dirs, files in os.walk(path, topdown=True):
    dirs[:] = [d for d in dirs if d not in exclude]
    for file in [f for f in files if f.endswith("terraform.tfvars")]:
      projects.append(os.path.relpath(root, './'))
  # remove top directory from list of projects
  # as it does not contain a deployable config    
  projects.remove(path)

  return set(projects)

def generate_config(args):
  """Generate an atlantis.yaml config file."""
  # get list of projects
  projects = find_terragrunt_projects(args.path)

  # generate dict with empty project list
  # OrderedDict is useless as yaml.dump() sorts the dict.
  # see https://github.com/yaml/pyyaml/issues/110
  config = {
    'version': 2,
    'projects': [],
    'workflows': {
      'terragrunt': {
        'apply': {
          'steps': [{
            'run': 'terragrunt apply -no-color $PLANFILE'
          }]
        },
        'plan': {
          'steps': [{
            'run': 'terragrunt plan -no-color -out $PLANFILE'
          }]
        }
      }
    }
  }

  # add projects to the config dict
  for project in projects:
    project = {
      'dir': project,
      'workflow': 'terragrunt'
    }
    config['projects'].append(project)

  # create a YAML structure
  result = dump(config, default_flow_style=False)

  # write to output file if provided
  # otherwise write to stdout
  if args.output:
    with open(args.output, 'w') as new_config:
      new_config.write(result)
      exit(0)
  
  print("\nReplace the content of atlantis.yaml by this output:\n\n\n" + result + "\n")

def main():
  """Manage the script arguments."""
  parser = argparse.ArgumentParser(description='Generate an atlantis.yaml config from a terragrunt repository.')
  parser.add_argument('path', metavar='PATH',
                    help='path to the terragrunt repository')
  parser.add_argument('--output', '-o', metavar='FILE',
                    help='name of the created file. If no --output flag is used, the script will output to stdout')
  parser.set_defaults(func=generate_config)
  args = parser.parse_args()
  args.func(args)

if __name__=="__main__":
	main()

@sstarcher
Copy link

A default would be a fantastic addition for those with dozens and dozens of terraform setups

@lkysow
Copy link
Member

lkysow commented Apr 4, 2019

This can be accomplished using server-side config now: https://www.runatlantis.io/docs/server-side-repo-config.html#change-the-default-atlantis-workflow

@lkysow lkysow closed this as completed Apr 4, 2019
@lkysow lkysow added the feature New functionality/enhancement label Apr 9, 2019
jamengual pushed a commit that referenced this issue Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality/enhancement
Projects
None yet
Development

No branches or pull requests

4 participants