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.

283 lines
9.4KB

  1. {%- set tplroot = tpldir.split('/')[0] %}
  2. {%- from tplroot ~ "/map.jinja" import salt_settings with context %}
  3. {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
  4. {% if salt_settings.pin_version and salt_settings.version and grains.os_family|lower == 'debian' %}
  5. include:
  6. - .pin
  7. {% endif %}
  8. {%- if grains.os == 'MacOS' %}
  9. {% if salt_settings.install_packages %}
  10. download-salt-minion:
  11. {% if salt_settings.salt_minion_pkg_source %} {# minion upgrade? #}
  12. file.managed:
  13. - name: '/tmp/salt.pkg'
  14. - source: {{ salt_settings.salt_minion_pkg_source }}
  15. {%- if salt_settings.salt_minion_pkg_hash %}
  16. - source_hash: {{ salt_settings.salt_minion_pkg_hash }}
  17. {% else %}
  18. - skip_verify: True
  19. {% endif %}
  20. {%- if grains['kernel'] != 'Windows' %}
  21. - user: {{ salt_settings.rootuser }}
  22. - group: {{ salt_settings.rootgroup }}
  23. {% endif %}
  24. - mode: '0644'
  25. - unless:
  26. - test -n "{{ salt_settings.version }}" && '/opt/salt/bin/salt-minion --version=.*{{ salt_settings.version }}.*'
  27. - require_in:
  28. - macpackage: salt-minion
  29. - retry: {{ salt_settings.retry_options | json }}
  30. {%- elif "workaround https://github.com/saltstack/salt/issues/49348" %}
  31. {% if salt_settings.install_packages %}
  32. pkg.installed:
  33. - name: {{ salt_settings.salt_minion }}
  34. {%- if salt_settings.version is defined %}
  35. - version: {{ salt_settings.version }}
  36. {%- endif %}
  37. {% endif %}
  38. {%- endif %}
  39. salt-minion-macos:
  40. file.managed:
  41. - onlyif: {{ grains.os == 'MacOS' }}
  42. - name: /Library/LaunchDaemons/com.saltstack.salt.minion.plist
  43. - source: https://raw.githubusercontent.com/saltstack/salt/master/pkg/osx/scripts/com.saltstack.salt.master.plist
  44. - source_hash: {{ salt_settings.salt_minion_macos_plist_hash }}
  45. - retry: {{ salt_settings.retry_options | json }}
  46. - require_in:
  47. - service: salt-minion
  48. - watch_in:
  49. - service: salt-minion
  50. {%- endif %}
  51. {%- endif %}
  52. salt-minion:
  53. {% if salt_settings.install_packages %}
  54. {%- if grains.os == 'MacOS' and salt_settings.salt_minion_pkg_source %}
  55. macpackage.installed:
  56. - name: '/tmp/salt.pkg'
  57. - target: /
  58. {# macpackage.installed is weird with version_check, detects diff but incomplete install #}
  59. - force: True {# workaround #}
  60. - unless:
  61. - test -n "{{ salt_settings.version }}" && '/opt/salt/bin/salt-minion --version=.*{{ salt_settings.version }}.*'
  62. {% if salt_settings.minion_service_details.state != 'ignore' %}
  63. - require_in:
  64. - service: salt-minion
  65. {% endif %}
  66. - onchanges_in:
  67. - cmd: remove-macpackage-salt
  68. {%- elif grains.os != 'MacOS' and "workaround https://github.com/saltstack/salt/issues/49348" %}
  69. pkg.installed:
  70. - name: {{ salt_settings.salt_minion }}
  71. {%- if salt_settings.version %}
  72. - version: {{ salt_settings.version }}
  73. {%- endif %}
  74. {% if salt_settings.minion_service_details.state != 'ignore' %}
  75. - require_in:
  76. - service: salt-minion
  77. {% endif %}
  78. {%- endif %}
  79. {% endif %}
  80. file.recurse:
  81. - name: {{ salt_settings.config_path | path_join('minion.d') }}
  82. {%- if salt_settings.minion_config_use_TOFS %}
  83. - template: ''
  84. - source: {{ files_switch(['minion.d'],
  85. lookup='salt-minion'
  86. )
  87. }}
  88. {%- else %}
  89. - template: jinja
  90. - source: salt://{{ tplroot }}/files/minion.d
  91. - context:
  92. standalone: False
  93. {%- endif %}
  94. - clean: {{ salt_settings.clean_config_d_dir }}
  95. - exclude_pat: _*
  96. {% if salt_settings.minion_service_details.state != 'ignore' %}
  97. service.{{ salt_settings.minion_service_details.state }}:
  98. - enable: {{ salt_settings.minion_service_details.enabled }}
  99. - name: {{ salt_settings.minion_service }}
  100. {%- if grains.os_family in ['FreeBSD', 'Gentoo'] %}
  101. - retry: {{ salt_settings.retry_options | json }}
  102. {%- endif %}
  103. - watch:
  104. - file: remove-old-minion-conf-file
  105. - order: last
  106. {% endif %}
  107. {%- if not salt_settings.restart_via_at %}
  108. cmd.run:
  109. {%- if grains['saltversioninfo'] >= [ 2016, 3 ] %}
  110. {%- if grains['kernel'] == 'Windows' %}
  111. - name: 'salt-call.bat --local service.restart {{ salt_settings.minion_service }}'
  112. {%- else %}
  113. - name: 'salt-call --local service.restart {{ salt_settings.minion_service }} --out-file /dev/null'
  114. {%- endif %}
  115. - bg: True
  116. {%- else %}
  117. {%- if grains['kernel'] == 'Windows' %}
  118. - name: 'start powershell "Restart-Service -Name {{ salt_settings.minion_service }}"'
  119. {%- else %}
  120. # old style, pre 2016.3. fork and disown the process
  121. - name: |-
  122. exec 0>&- # close stdin
  123. exec 1>&- # close stdout
  124. exec 2>&- # close stderr
  125. nohup salt-call --local service.restart {{ salt_settings.minion_service }} --out-file /dev/null &
  126. {%- endif %}
  127. {%- endif %}
  128. - onchanges:
  129. {%- if salt_settings.install_packages %}
  130. {%- if grains.os == 'MacOS' and salt_settings.salt_minion_pkg_source %}
  131. - macpackage: salt-minion
  132. {%- elif grains.os == 'MacOS' %}
  133. - pkg: download-salt-minion
  134. {%- else %}
  135. - pkg: salt-minion
  136. {%- endif %}
  137. {%- endif %}
  138. - file: salt-minion
  139. - file: remove-old-minion-conf-file
  140. {%- else %}
  141. {% if grains.os_family not in ['MacOS', 'FreeBSD'] %}
  142. {# MacOS and FreeBSD have the 'at' command; but there's no package to install #}
  143. at:
  144. pkg.installed:
  145. - require_in: restart-salt-minion
  146. {% endif %}
  147. restart-salt-minion:
  148. cmd.run:
  149. - name: echo salt-call --local service.restart {{ salt_settings.minion_service }} | at now + 1 minute
  150. - order: last
  151. - onchanges:
  152. {%- if salt_settings.install_packages %}
  153. {%- if grains.os == 'MacOS' and salt_settings.salt_minion_pkg_source %}
  154. - macpackage: salt-minion
  155. {%- elif grains.os == 'MacOS' %}
  156. - pkg: download-salt-minion
  157. {%- else %}
  158. - pkg: salt-minion
  159. {%- endif %}
  160. {%- endif %}
  161. - file: salt-minion
  162. - file: remove-old-minion-conf-file
  163. {%- endif %}
  164. {% if 'inotify' in salt_settings.get('minion', {}).get('beacons', {}) and salt_settings.get('pyinotify', False) %}
  165. salt-minion-beacon-inotify:
  166. pkg.installed:
  167. - name: {{ salt_settings.pyinotify }}
  168. - require_in:
  169. - service: salt-minion
  170. - watch_in:
  171. - service: salt-minion
  172. {% endif %}
  173. {% if salt_settings.minion_remove_config %}
  174. remove-default-minion-conf-file:
  175. file.absent:
  176. - name: {{ salt_settings.config_path }}/minion
  177. {% endif %}
  178. # clean up old _defaults.conf file if they have it around
  179. remove-old-minion-conf-file:
  180. file.absent:
  181. - name: {{ salt_settings.config_path | path_join('minion.d', '_defaults.conf') }}
  182. {% if grains.os == 'MacOS' %}
  183. remove-macpackage-salt:
  184. file.absent:
  185. - name: /tmp/salt.pkg
  186. - force: True
  187. {% endif %}
  188. {% if not salt_settings.minion_remove_config %}
  189. permissions-minion-config:
  190. file.managed:
  191. - name: {{ salt_settings.config_path | path_join('minion') }}
  192. {%- if grains['kernel'] != 'Windows' %}
  193. - user: {{ salt_settings.rootuser }}
  194. - group:
  195. {%- if grains['kernel'] in ['FreeBSD', 'OpenBSD', 'NetBSD'] %}
  196. wheel
  197. {%- else %}
  198. {{ salt_settings.rootgroup }}
  199. {%- endif %}
  200. {% endif %}
  201. {%- if grains['kernel'] != 'Windows' %}
  202. - mode: 640
  203. {% endif %}
  204. - replace: False
  205. {% endif %}
  206. salt-minion-pki-dir:
  207. file.directory:
  208. {% if 'pki_dir' in salt_settings.minion %}
  209. - name: {{ salt_settings.minion.pki_dir }}
  210. {% else %}
  211. - name: {{ salt_settings.config_path | path_join('pki', 'minion') }}
  212. {% endif %}
  213. {%- if grains['kernel'] != 'Windows' %}
  214. - user: {{ salt_settings.rootuser }}
  215. - group:
  216. {%- if grains['kernel'] in ['FreeBSD', 'OpenBSD', 'NetBSD'] %}
  217. wheel
  218. {%- else %}
  219. {{ salt_settings.rootgroup }}
  220. {%- endif %}
  221. {% endif %}
  222. {%- if grains['kernel'] != 'Windows' %}
  223. - mode: 700
  224. {% endif %}
  225. - makedirs: True
  226. permissions-minion.pem:
  227. file.managed:
  228. {% if 'pki_dir' in salt_settings.minion %}
  229. - name: {{ salt_settings.minion.pki_dir | path_join('minion.pem') }}
  230. {% else %}
  231. - name: {{ salt_settings.config_path | path_join('pki', 'minion', 'minion.pem') }}
  232. {% endif %}
  233. {%- if grains['kernel'] != 'Windows' %}
  234. - user: {{ salt_settings.rootuser }}
  235. - group:
  236. {%- if grains['kernel'] in ['FreeBSD', 'OpenBSD', 'NetBSD'] %}
  237. wheel
  238. {%- else %}
  239. {{ salt_settings.rootgroup }}
  240. {%- endif %}
  241. {% endif %}
  242. {%- if grains['kernel'] != 'Windows' %}
  243. - mode: 400
  244. {% endif %}
  245. - replace: False
  246. - require:
  247. - file: salt-minion-pki-dir
  248. permissions-minion.pub:
  249. file.managed:
  250. {% if 'pki_dir' in salt_settings.minion %}
  251. - name: {{ salt_settings.minion.pki_dir | path_join('minion.pub') }}
  252. {% else %}
  253. - name: {{ salt_settings.config_path | path_join('pki', 'minion', 'minion.pub') }}
  254. {% endif %}
  255. - user: {{ salt_settings.rootuser }}
  256. - group:
  257. {%- if grains['kernel'] in ['FreeBSD', 'OpenBSD', 'NetBSD'] %}
  258. wheel
  259. {%- else %}
  260. {{ salt_settings.rootgroup }}
  261. {%- endif %}
  262. {%- if grains['kernel'] != 'Windows' %}
  263. - mode: 644
  264. {% endif %}
  265. - replace: False
  266. - require:
  267. - file: salt-minion-pki-dir