Saltstack Official Linux Formula
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

306 lines
8.3KB

  1. {%- from "linux/map.jinja" import network with context %}
  2. {%- from "linux/map.jinja" import system with context %}
  3. {%- if network.enabled %}
  4. {%- macro set_param(param_name, param_dict) -%}
  5. {%- if param_dict.get(param_name, False) -%}
  6. - {{ param_name }}: {{ param_dict[param_name] }}
  7. {%- endif -%}
  8. {%- endmacro -%}
  9. {%- if network.bridge != 'none' %}
  10. linux_network_bridge_pkgs:
  11. pkg.installed:
  12. {%- if network.bridge == 'openvswitch' %}
  13. - pkgs: {{ network.ovs_pkgs }}
  14. {%- else %}
  15. - pkgs: {{ network.bridge_pkgs }}
  16. {%- endif %}
  17. {%- endif %}
  18. {%- for interface_name, interface in network.interface.iteritems() %}
  19. {%- set interface_name = interface.get('name', interface_name) %}
  20. {# it is not used for any interface with type preffix dpdk,eg. dpdk_ovs_port #}
  21. {%- if interface.get('managed', True) and not 'dpdk' in interface.type %}
  22. {%- if grains.os_family in ['RedHat', 'Debian'] %}
  23. {%- if interface.type == 'ovs_bridge' %}
  24. ovs_bridge_{{ interface_name }}:
  25. openvswitch_bridge.present:
  26. - name: {{ interface_name }}
  27. {# add linux network interface into OVS bridge #}
  28. {%- for int_name, int in network.interface.iteritems() %}
  29. {%- set int_name = int.get('name', int_name) %}
  30. {%- if int.ovs_bridge is defined and interface_name == int.ovs_bridge %}
  31. add_int_{{ int_name }}_to_ovs_bridge_{{ interface_name }}:
  32. cmd.run:
  33. - unless: ovs-vsctl show | grep {{ int_name }}
  34. - name: ovs-vsctl add-port {{ interface_name }} {{ int_name }}
  35. {%- endif %}
  36. {%- endfor %}
  37. {%- elif interface.type == 'ovs_port' %}
  38. {%- if interface.get('port_type','internal') == 'patch' %}
  39. ovs_port_{{ interface_name }}:
  40. openvswitch_port.present:
  41. - name: {{ interface_name }}
  42. - bridge: {{ interface.bridge }}
  43. - require:
  44. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  45. ovs_port_set_type_{{ interface_name }}:
  46. cmd.run:
  47. - name: ovs-vsctl set interface {{ interface_name }} type=patch
  48. - unless: ovs-vsctl show | grep -A 1 'Interface {{ interface_name }}' | grep patch
  49. ovs_port_set_peer_{{ interface_name }}:
  50. cmd.run:
  51. - name: ovs-vsctl set interface {{ interface_name }} options:peer={{ interface.peer }}
  52. - unless: ovs-vsctl show | grep -A 2 'Interface {{ interface_name }}' | grep {{ interface.peer }}
  53. {%- else %}
  54. linux_interfaces_include_{{ interface_name }}:
  55. file.prepend:
  56. - name: /etc/network/interfaces
  57. - text: |
  58. source /etc/network/interfaces.d/*
  59. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  60. source /etc/network/interfaces.u/*
  61. ovs_port_{{ interface_name }}:
  62. file.managed:
  63. - name: /etc/network/interfaces.u/ifcfg-{{ interface_name }}
  64. - makedirs: True
  65. - source: salt://linux/files/ovs_port
  66. - defaults:
  67. port: {{ interface|yaml }}
  68. port_name: {{ interface_name }}
  69. - template: jinja
  70. ovs_port_{{ interface_name }}_line1:
  71. file.replace:
  72. - name: /etc/network/interfaces
  73. - pattern: auto {{ interface_name }}
  74. - repl: ""
  75. ovs_port_{{ interface_name }}_line2:
  76. file.replace:
  77. - name: /etc/network/interfaces
  78. - pattern: 'iface {{ interface_name }} inet .*'
  79. - repl: ""
  80. ovs_port_up_{{ interface_name }}:
  81. cmd.run:
  82. - name: ifup {{ interface_name }}
  83. - require:
  84. - file: ovs_port_{{ interface_name }}
  85. - file: ovs_port_{{ interface_name }}_line1
  86. - file: ovs_port_{{ interface_name }}_line2
  87. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  88. - file: linux_interfaces_final_include
  89. {%- endif %}
  90. {%- else %}
  91. linux_interface_{{ interface_name }}:
  92. network.managed:
  93. - enabled: {{ interface.enabled }}
  94. - name: {{ interface_name }}
  95. - type: {{ interface.type }}
  96. {%- if interface.address is defined %}
  97. {%- if grains.os_family == 'Debian' %}
  98. - proto: {{ interface.get('proto', 'static') }}
  99. {% endif %}
  100. {%- if grains.os_family == 'RedHat' %}
  101. {%- if interface.get('proto', 'none') == 'manual' %}
  102. - proto: 'none'
  103. {%- else %}
  104. - proto: {{ interface.get('proto', 'none') }}
  105. {%- endif %}
  106. {% endif %}
  107. - ipaddr: {{ interface.address }}
  108. - netmask: {{ interface.netmask }}
  109. {%- else %}
  110. - proto: {{ interface.get('proto', 'dhcp') }}
  111. {%- endif %}
  112. {%- if interface.type == 'slave' %}
  113. - master: {{ interface.master }}
  114. {%- endif %}
  115. {%- if interface.name_servers is defined %}
  116. - dns: {{ interface.name_servers }}
  117. {%- endif %}
  118. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  119. {%- if interface.wireless.security == "wpa" %}
  120. - wpa-ssid: {{ interface.wireless.essid }}
  121. - wpa-psk: {{ interface.wireless.key }}
  122. {%- else %}
  123. - wireless-ssid: {{ interface.wireless.essid }}
  124. - wireless-psk: {{ interface.wireless.key }}
  125. {%- endif %}
  126. {%- endif %}
  127. {%- for param in network.interface_params %}
  128. {{ set_param(param, interface) }}
  129. {%- endfor %}
  130. {%- if interface.type == 'bridge' %}
  131. - bridge: {{ interface_name }}
  132. - delay: 0
  133. - bypassfirewall: True
  134. - use:
  135. {%- for network in interface.use_interfaces %}
  136. - network: linux_interface_{{ network }}
  137. {%- endfor %}
  138. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  139. - require:
  140. {%- for network in interface.get('use_interfaces', []) %}
  141. - network: linux_interface_{{ network }}
  142. {%- endfor %}
  143. {%- for network in interface.get('use_ovs_ports', []) %}
  144. - cmd: ovs_port_up_{{ network }}
  145. {%- endfor %}
  146. {%- endif %}
  147. {%- if interface.type == 'bond' %}
  148. - slaves: {{ interface.slaves }}
  149. - mode: {{ interface.mode }}
  150. {%- endif %}
  151. {%- for network in interface.get('use_ovs_ports', []) %}
  152. remove_interface_{{ network }}_line1:
  153. file.replace:
  154. - name: /etc/network/interfaces
  155. - pattern: auto {{ network }}
  156. - repl: ""
  157. remove_interface_{{ network }}_line2:
  158. file.replace:
  159. - name: /etc/network/interfaces
  160. - pattern: iface {{ network }} inet manual
  161. - repl: ""
  162. {%- endfor %}
  163. {%- if interface.gateway is defined %}
  164. linux_system_network:
  165. network.system:
  166. - enabled: {{ interface.enabled }}
  167. - hostname: {{ network.fqdn }}
  168. {%- if interface.gateway is defined %}
  169. - gateway: {{ interface.gateway }}
  170. - gatewaydev: {{ interface_name }}
  171. {%- endif %}
  172. - nozeroconf: True
  173. - nisdomain: {{ system.domain }}
  174. - require_reboot: True
  175. {%- endif %}
  176. {%- endif %}
  177. {%- endif %}
  178. {%- if interface.wireless is defined %}
  179. {%- if grains.os_family == 'Arch' %}
  180. linux_network_packages:
  181. pkg.installed:
  182. - pkgs: {{ network.pkgs }}
  183. /etc/netctl/network_{{ interface.wireless.essid }}:
  184. file.managed:
  185. - source: salt://linux/files/wireless
  186. - mode: 755
  187. - template: jinja
  188. - require:
  189. - pkg: linux_network_packages
  190. - defaults:
  191. interface_name: {{ interface_name }}
  192. switch_profile_{{ interface.wireless.essid }}:
  193. cmd.run:
  194. - name: netctl switch-to network_{{ interface.wireless.essid }}
  195. - cwd: /root
  196. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  197. - require:
  198. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  199. enable_profile_{{ interface.wireless.essid }}:
  200. cmd.run:
  201. - name: netctl enable network_{{ interface.wireless.essid }}
  202. - cwd: /root
  203. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  204. - require:
  205. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  206. {%- endif %}
  207. {%- endif %}
  208. {%- endif %}
  209. {%- if interface.route is defined %}
  210. linux_network_{{ interface_name }}_routes:
  211. network.routes:
  212. - name: {{ interface_name }}
  213. - routes:
  214. {%- for route_name, route in interface.route.iteritems() %}
  215. - name: {{ route_name }}
  216. ipaddr: {{ route.address }}
  217. netmask: {{ route.netmask }}
  218. gateway: {{ route.gateway }}
  219. {%- endfor %}
  220. {%- endif %}
  221. {%- endfor %}
  222. {%- if network.bridge != 'none' %}
  223. linux_interfaces_final_include:
  224. file.prepend:
  225. - name: /etc/network/interfaces
  226. - text: |
  227. source /etc/network/interfaces.d/*
  228. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  229. source /etc/network/interfaces.u/*
  230. {%- endif %}
  231. {%- endif %}
  232. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  233. NetworkManager:
  234. service.dead:
  235. - enable: false
  236. {%- endif %}
  237. {%- if network.tap_custom_txqueuelen is defined %}
  238. /etc/udev/rules.d/60-net-txqueue.rules:
  239. file.managed:
  240. - source: salt://linux/files/60-net-txqueue.rules
  241. - mode: 755
  242. - template: jinja
  243. {%- endif %}