Saltstack Official Salt Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

formulas.sls 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. {% set processed_gitdirs = {} %}
  2. {% set processed_gitdir_envs = [] %}
  3. {% set processed_basedirs = [] %}
  4. {% from "salt/map.jinja" import formulas_settings with context %}
  5. {% from "salt/formulas.jinja" import formulas_git_opt with context %}
  6. {% from "salt/formulas.jinja" import formulas_opts_for_git_latest with context %}
  7. # Loop over all formulas listed in pillar data
  8. {% for env, entries in salt['pillar.get']('salt_formulas:list', {}).items() %}
  9. {% for entry in entries %}
  10. {% set basedir = formulas_git_opt(env, 'basedir')|load_yaml %}
  11. {% set gitdir = '{0}/{1}'.format(basedir, entry) %}
  12. {% set update = formulas_git_opt(env, 'update')|load_yaml %}
  13. {% if formulas_settings.checkout_orig_branch %}
  14. {% if not salt['file.directory_exists']('{0}/{1}'.format(gitdir, '.git')) %}
  15. {% set gitdir_branch = '' %}
  16. {% else %}
  17. {% set gitdir_branch = salt['git.current_branch'](gitdir) %}
  18. {% endif %}
  19. {% do processed_gitdirs.update({gitdir:gitdir_branch}) %}
  20. {% endif %}
  21. # Setup the directory hosting the Git repository
  22. {% if basedir not in processed_basedirs %}
  23. {% do processed_basedirs.append(basedir) %}
  24. {{ basedir }}:
  25. file.directory:
  26. {%- for key, value in salt['pillar.get']('salt_formulas:basedir_opts',
  27. {'makedirs': True}).items() %}
  28. - {{ key }}: {{ value }}
  29. {%- endfor %}
  30. {% endif %}
  31. # Setup the formula Git repository
  32. {% set gitdir_env = '{0}_{1}'.format(gitdir, env) %}
  33. {% if gitdir_env not in processed_gitdir_envs %}
  34. {% do processed_gitdir_envs.append(gitdir_env) %}
  35. {% set options = formulas_opts_for_git_latest(env)|load_yaml %}
  36. {% set baseurl = formulas_git_opt(env, 'baseurl')|load_yaml %}
  37. {{ gitdir_env }}:
  38. git.latest:
  39. - name: {{ baseurl }}/{{ entry }}.git
  40. - target: {{ gitdir }}
  41. {%- for key, value in options.items() %}
  42. - {{ key }}: {{ value }}
  43. {%- endfor %}
  44. - require:
  45. - file: {{ basedir }}
  46. {%- if not update %}
  47. - onlyif: rm -fr {{ gitdir }} >/dev/null 2>&1 | true
  48. {%- endif %}
  49. {% endif %}
  50. {% endfor %}
  51. {% endfor %}
  52. {% if formulas_settings.checkout_orig_branch %}
  53. # For each directory processed, explicitly checkout the original branch before
  54. # the `git.latest` state ran
  55. {% for gitdir, original_branch in processed_gitdirs.items() %}
  56. {% if original_branch %}
  57. {% set gitdir_user = salt['file.get_user'](gitdir) %}
  58. checkout_original_branch_for_{{ gitdir }}:
  59. module.run:
  60. - name: git.checkout
  61. - order: last
  62. - cwd: {{ gitdir }}
  63. - rev: {{ original_branch }}
  64. - user: {{ gitdir_user }}
  65. - unless: test "$(cd {{ gitdir }}; git rev-parse --abbrev-ref HEAD)" = "{{ original_branch }}"
  66. {% endif %}
  67. {% endfor %}
  68. {% endif %}