Saltstack Official Linux 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.

253 lines
7.3KB

  1. {%- from "linux/map.jinja" import system with context %}
  2. {%- if system.enabled %}
  3. linux_repo_prereq_pkgs:
  4. pkg.installed:
  5. - pkgs: {{ system.pkgs }}
  6. # global proxy setup
  7. {%- if system.proxy.get('pkg', {}).get('enabled', False) %}
  8. {%- if grains.os_family == 'Debian' %}
  9. /etc/apt/apt.conf.d/99proxies-salt:
  10. file.managed:
  11. - template: jinja
  12. - source: salt://linux/files/apt.conf.d_proxies
  13. - defaults:
  14. external_host: False
  15. https: {{ system.proxy.get('pkg', {}).get('https', None) | default(system.proxy.get('https', None), true) }}
  16. http: {{ system.proxy.get('pkg', {}).get('http', None) | default(system.proxy.get('http', None), true) }}
  17. ftp: {{ system.proxy.get('pkg', {}).get('ftp', None) | default(system.proxy.get('ftp', None), true) }}
  18. {%- else %}
  19. /etc/apt/apt.conf.d/99proxies-salt:
  20. file.absent
  21. {%- endif %}
  22. {%- endif %}
  23. {% set default_repos = {} %}
  24. {%- if system.purge_repos|default(False) %}
  25. purge_sources_list_d_repos:
  26. file.directory:
  27. - name: /etc/apt/sources.list.d/
  28. - clean: True
  29. {%- endif %}
  30. {%- for name, repo in system.repo.items() %}
  31. {%- set name=repo.get('name', name) %}
  32. {%- if grains.os_family == 'Debian' %}
  33. # per repository proxy setup
  34. {%- if repo.get('proxy', {}).get('enabled', False) %}
  35. {%- set external_host = repo.proxy.get('host', None) or repo.source.split('/')[2] %}
  36. /etc/apt/apt.conf.d/99proxies-salt-{{ name }}:
  37. file.managed:
  38. - template: jinja
  39. - source: salt://linux/files/apt.conf.d_proxies
  40. - defaults:
  41. external_host: {{ external_host }}
  42. https: {{ repo.proxy.get('https', None) or system.proxy.get('pkg', {}).get('https', None) | default(system.proxy.get('https', None), True) }}
  43. http: {{ repo.proxy.get('http', None) or system.proxy.get('pkg', {}).get('http', None) | default(system.proxy.get('http', None), True) }}
  44. ftp: {{ repo.proxy.get('ftp', None) or system.proxy.get('pkg', {}).get('ftp', None) | default(system.proxy.get('ftp', None), True) }}
  45. {%- else %}
  46. /etc/apt/apt.conf.d/99proxies-salt-{{ name }}:
  47. file.absent
  48. {%- endif %}
  49. {%- if repo.pin is defined %}
  50. linux_repo_{{ name }}_pin:
  51. file.managed:
  52. - name: /etc/apt/preferences.d/{{ name }}
  53. - source: salt://linux/files/preferences_repo
  54. - template: jinja
  55. - defaults:
  56. repo_name: {{ name }}
  57. {%- else %}
  58. linux_repo_{{ name }}_pin:
  59. file.absent:
  60. - name: /etc/apt/preferences.d/{{ name }}
  61. {%- endif %}
  62. {%- if repo.get('key') %} {# 2 #}
  63. linux_repo_{{ name }}_key:
  64. cmd.run:
  65. - name: |
  66. echo "{{ repo.key | indent(12) }}" | apt-key add -
  67. - unless: |
  68. apt-key finger --with-colons | grep -qF $(echo "{{ repo.key| indent(12) }}" | gpg --with-fingerprint --with-colons | grep -E '^fpr')
  69. - require_in:
  70. {%- if repo.get('default', False) %}
  71. - file: default_repo_list
  72. {% else %}
  73. - pkgrepo: linux_repo_{{ name }}
  74. {% endif %}
  75. {# key_url fetch by curl when salt <2017.7, higher version of salt has fixed bug for using a proxy_host/port specified at minion.conf #}
  76. {#
  77. NOTE: curl/cmd.run usage to fetch gpg key has limited functionality behind proxy. Environments with salt >= 2017.7 shoul use
  78. key_url specified at pkgrepo.manage state (which uses properly configured http_host at minion.conf). Older versions of
  79. salt require to have proxy set at ENV and curl way to fetch gpg key here can have a sense for backward compatibility.
  80. #}
  81. {%- if grains['saltversioninfo'] < [2017, 7] %}
  82. {%- elif repo.key_url|default(False) and not repo.key_url.startswith('salt://') %}
  83. linux_repo_{{ name }}_key:
  84. cmd.run:
  85. - name: "curl -sL {{ repo.key_url }} | apt-key add -"
  86. - unless: "apt-key finger --with-colons | grep -qF $(curl -sL {{ repo.key_url }} | gpg --with-fingerprint --with-colons | grep -E '^fpr')"
  87. - require_in:
  88. {%- if repo.get('default', False) %}
  89. - file: default_repo_list
  90. {% else %}
  91. - pkgrepo: linux_repo_{{ name }}
  92. {% endif %}
  93. {%- endif %} {# key_url fetch by curl when salt <2017.7 #}
  94. {%- endif %} {# 2 #}
  95. {%- if repo.get('default', False) %} {# 1 #}
  96. {%- do default_repos.update({name: repo}) %} {# for 'default' repos #}
  97. {%- else %} {# for all others repos #}
  98. {%- if repo.get('enabled', True) %}
  99. linux_repo_{{ name }}:
  100. pkgrepo.managed:
  101. {%- if repo.ppa is defined %}
  102. - ppa: {{ repo.ppa }}
  103. {%- else %}
  104. - humanname: {{ name }}
  105. - name: {{ repo.source }}
  106. {%- if repo.architectures is defined %}
  107. - architectures: {{ repo.architectures }}
  108. {%- endif %}
  109. - file: /etc/apt/sources.list.d/{{ name }}.list
  110. - clean_file: {{ repo.clean|default(True) }}
  111. {%- if repo.key_id is defined %}
  112. - keyid: {{ repo.key_id }}
  113. {%- endif %}
  114. {%- if repo.key_server is defined %}
  115. - keyserver: {{ repo.key_server }}
  116. {%- endif %}
  117. {%- if repo.key_url is defined and grains['saltversioninfo'] >= [2017, 7] %}
  118. - key_url: {{ repo.key_url }}
  119. {%- endif %}
  120. - consolidate: {{ repo.get('consolidate', False) }}
  121. - clean_file: {{ repo.get('clean_file', False) }}
  122. - refresh_db: {{ repo.get('refresh_db', True) }}
  123. - require:
  124. - pkg: linux_repo_prereq_pkgs
  125. {%- if repo.get('proxy', {}).get('enabled', False) %}
  126. - file: /etc/apt/apt.conf.d/99proxies-salt-{{ name }}
  127. {%- endif %}
  128. {%- if system.proxy.get('pkg', {}).get('enabled', False) %}
  129. - file: /etc/apt/apt.conf.d/99proxies-salt
  130. {%- endif %}
  131. {%- if system.purge_repos|default(False) %}
  132. - file: purge_sources_list_d_repos
  133. {%- endif %}
  134. {%- endif %}
  135. {%- else %}
  136. linux_repo_{{ name }}_absent:
  137. pkgrepo.absent:
  138. {%- if repo.ppa is defined %}
  139. - ppa: {{ repo.ppa }}
  140. {%- if repo.key_id is defined %}
  141. - keyid_ppa: {{ repo.keyid_ppa }}
  142. {%- endif %}
  143. {%- else %}
  144. - file: /etc/apt/sources.list.d/{{ name }}.list
  145. {%- if repo.key_id is defined %}
  146. - keyid: {{ repo.key_id }}
  147. {%- endif %}
  148. {%- endif %}
  149. file.absent:
  150. - name: /etc/apt/sources.list.d/{{ name }}.list
  151. {%- endif %}
  152. {%- endif %} {# 1 #}
  153. {#- os_family Debian #}
  154. {%- endif %}
  155. {%- if grains.os_family == "RedHat" %}
  156. {%- if repo.get('enabled', True) %}
  157. {%- if repo.get('proxy', {}).get('enabled', False) %}
  158. # PLACEHOLDER
  159. # TODO, implement per proxy configuration for Yum
  160. {%- endif %}
  161. {%- if not repo.get('default', False) %}
  162. linux_repo_{{ name }}:
  163. pkgrepo.managed:
  164. - name: {{ name }}
  165. - humanname: {{ repo.get('humanname', name) }}
  166. {%- if repo.mirrorlist is defined %}
  167. - mirrorlist: {{ repo.mirrorlist }}
  168. {%- else %}
  169. - baseurl: {{ repo.source }}
  170. {%- endif %}
  171. - gpgcheck: {% if repo.get('gpgcheck', False) %}1{% else %}0{% endif %}
  172. {%- if repo.gpgkey is defined %}
  173. - gpgkey: {{ repo.gpgkey }}
  174. {%- endif %}
  175. - require:
  176. - pkg: linux_repo_prereq_pkgs
  177. {%- endif %}
  178. {#- repo.enabled is false #}
  179. {%- else %}
  180. pkgrepo.absent:
  181. - name: {{ repo.source }}
  182. {%- endif %}
  183. {#- os_family Redhat #}
  184. {%- endif %}
  185. {#- repo.items() loop #}
  186. {%- endfor %}
  187. {%- if default_repos|length > 0 and grains.os_family == 'Debian' %}
  188. default_repo_list:
  189. file.managed:
  190. - name: /etc/apt/sources.list
  191. - source: salt://linux/files/sources.list
  192. - template: jinja
  193. - user: root
  194. - group: root
  195. - mode: 0644
  196. {%- if system.purge_repos|default(False) %}
  197. - replace: True
  198. {%- endif %}
  199. - defaults:
  200. default_repos: {{ default_repos }}
  201. - require:
  202. - pkg: linux_repo_prereq_pkgs
  203. refresh_default_repo:
  204. module.wait:
  205. - name: pkg.refresh_db
  206. - watch:
  207. - file: default_repo_list
  208. {%- endif %}
  209. {%- endif %}