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.

370 lines
10.0KB

  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 f in network.get('concat_iface_files', []) %}
  19. {%- if salt['file.file_exists'](f.src) %}
  20. append_{{ f.src }}_{{ f.dst }}:
  21. file.append:
  22. - name: {{ f.dst }}
  23. - source: {{ f.src }}
  24. remove_appended_{{ f.src }}:
  25. file.absent:
  26. - name: {{ f.src }}
  27. {%- endif %}
  28. {%- endfor %}
  29. {%- for f in network.get('remove_iface_files', []) %}
  30. remove_iface_file_{{ f }}:
  31. file.absent:
  32. - name: {{ f }}
  33. {%- endfor %}
  34. {%- for interface_name, interface in network.interface.iteritems() %}
  35. {%- set interface_name = interface.get('name', interface_name) %}
  36. {# it is not used for any interface with type preffix dpdk,eg. dpdk_ovs_port #}
  37. {%- if interface.get('managed', True) and not 'dpdk' in interface.type %}
  38. {%- if grains.os_family in ['RedHat', 'Debian'] %}
  39. {%- if interface.type == 'ovs_bridge' %}
  40. ovs_bridge_{{ interface_name }}:
  41. openvswitch_bridge.present:
  42. - name: {{ interface_name }}
  43. {# add linux network interface into OVS bridge #}
  44. {%- for int_name, int in network.interface.iteritems() %}
  45. {%- set int_name = int.get('name', int_name) %}
  46. {%- if int.ovs_bridge is defined and interface_name == int.ovs_bridge %}
  47. add_int_{{ int_name }}_to_ovs_bridge_{{ interface_name }}:
  48. cmd.run:
  49. - unless: ovs-vsctl show | grep {{ int_name }}
  50. - name: ovs-vsctl add-port {{ interface_name }} {{ int_name }}
  51. {%- endif %}
  52. {%- endfor %}
  53. {%- elif interface.type == 'ovs_port' %}
  54. {%- if interface.get('port_type','internal') == 'patch' %}
  55. ovs_port_{{ interface_name }}:
  56. openvswitch_port.present:
  57. - name: {{ interface_name }}
  58. - bridge: {{ interface.bridge }}
  59. - require:
  60. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  61. ovs_port_set_type_{{ interface_name }}:
  62. cmd.run:
  63. - name: ovs-vsctl set interface {{ interface_name }} type=patch
  64. - unless: ovs-vsctl show | grep -A 1 'Interface {{ interface_name }}' | grep patch
  65. ovs_port_set_peer_{{ interface_name }}:
  66. cmd.run:
  67. - name: ovs-vsctl set interface {{ interface_name }} options:peer={{ interface.peer }}
  68. - unless: ovs-vsctl show | grep -A 2 'Interface {{ interface_name }}' | grep {{ interface.peer }}
  69. {%- else %}
  70. linux_interfaces_include_{{ interface_name }}:
  71. file.prepend:
  72. - name: /etc/network/interfaces
  73. - text: |
  74. source /etc/network/interfaces.d/*
  75. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  76. source /etc/network/interfaces.u/*
  77. ovs_port_{{ interface_name }}:
  78. file.managed:
  79. - name: /etc/network/interfaces.u/ifcfg-{{ interface_name }}
  80. - makedirs: True
  81. - source: salt://linux/files/ovs_port
  82. - defaults:
  83. port: {{ interface|yaml }}
  84. port_name: {{ interface_name }}
  85. - template: jinja
  86. ovs_port_{{ interface_name }}_line1:
  87. file.replace:
  88. - name: /etc/network/interfaces
  89. - pattern: auto {{ interface_name }}
  90. - repl: ""
  91. ovs_port_{{ interface_name }}_line2:
  92. file.replace:
  93. - name: /etc/network/interfaces
  94. - pattern: 'iface {{ interface_name }} inet .*'
  95. - repl: ""
  96. ovs_port_up_{{ interface_name }}:
  97. cmd.run:
  98. - name: ifup {{ interface_name }}
  99. - require:
  100. - file: ovs_port_{{ interface_name }}
  101. - file: ovs_port_{{ interface_name }}_line1
  102. - file: ovs_port_{{ interface_name }}_line2
  103. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  104. - file: linux_interfaces_final_include
  105. {%- endif %}
  106. {%- else %}
  107. linux_interface_{{ interface_name }}:
  108. network.managed:
  109. - enabled: {{ interface.enabled }}
  110. - name: {{ interface_name }}
  111. - type: {{ interface.type }}
  112. {%- if interface.address is defined %}
  113. {%- if grains.os_family == 'Debian' %}
  114. - proto: {{ interface.get('proto', 'static') }}
  115. {% endif %}
  116. {%- if grains.os_family == 'RedHat' %}
  117. {%- if interface.get('proto', 'none') == 'manual' %}
  118. - proto: 'none'
  119. {%- else %}
  120. - proto: {{ interface.get('proto', 'none') }}
  121. {%- endif %}
  122. {% endif %}
  123. - ipaddr: {{ interface.address }}
  124. - netmask: {{ interface.netmask }}
  125. {%- else %}
  126. - proto: {{ interface.get('proto', 'dhcp') }}
  127. {%- endif %}
  128. {%- if interface.type == 'slave' %}
  129. - master: {{ interface.master }}
  130. {%- endif %}
  131. {%- if interface.name_servers is defined %}
  132. - dns: {{ interface.name_servers }}
  133. {%- endif %}
  134. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  135. {%- if interface.wireless.security == "wpa" %}
  136. - wpa-ssid: {{ interface.wireless.essid }}
  137. - wpa-psk: {{ interface.wireless.key }}
  138. {%- else %}
  139. - wireless-ssid: {{ interface.wireless.essid }}
  140. - wireless-psk: {{ interface.wireless.key }}
  141. {%- endif %}
  142. {%- endif %}
  143. {%- for param in network.interface_params %}
  144. {{ set_param(param, interface) }}
  145. {%- endfor %}
  146. {%- if interface.require_interfaces is defined %}
  147. - require:
  148. {%- for netif in interface.get('require_interfaces', []) %}
  149. - network: linux_interface_{{ netif }}
  150. {%- endfor %}
  151. {%- for network in interface.get('use_ovs_ports', []) %}
  152. - cmd: ovs_port_up_{{ network }}
  153. {%- endfor %}
  154. {%- endif %}
  155. {%- if interface.type == 'bridge' %}
  156. - bridge: {{ interface_name }}
  157. - delay: 0
  158. - bypassfirewall: True
  159. - use:
  160. {%- for network in interface.use_interfaces %}
  161. - network: linux_interface_{{ network }}
  162. {%- endfor %}
  163. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  164. - require:
  165. {%- for network in interface.get('use_interfaces', []) %}
  166. - network: linux_interface_{{ network }}
  167. {%- endfor %}
  168. {%- for network in interface.get('use_ovs_ports', []) %}
  169. - cmd: ovs_port_up_{{ network }}
  170. {%- endfor %}
  171. {%- endif %}
  172. {%- if interface.type == 'bond' %}
  173. - slaves: {{ interface.slaves }}
  174. - mode: {{ interface.mode }}
  175. {%- endif %}
  176. {%- if interface.get('ipflush_onchange', False) %}
  177. linux_interface_ipflush_onchange_{{ interface_name }}:
  178. cmd.run:
  179. - name: "/sbin/ip address flush dev {{ interface_name }}"
  180. - onchanges:
  181. - network: linux_interface_{{ interface_name }}
  182. {%- endif %}
  183. {%- if salt['grains.get']('saltversion') < '2017.7' %}
  184. # TODO(ddmitriev): Remove this 'if .. endif' block completely when
  185. # switched to salt version 2017.7 that has the same functionality.
  186. {%- if interface.type == 'bond' and interface.enabled == True %}
  187. linux_bond_interface_{{ interface_name }}:
  188. cmd.run:
  189. - name: ifenslave {{ interface_name }} {{ interface.slaves }}
  190. - require:
  191. - network: linux_interface_{{ interface_name }}
  192. - onchanges:
  193. - network: linux_interface_{{ interface_name }}
  194. {%- for network in interface.slaves.split() %}
  195. - network: linux_interface_{{ network }}
  196. {%- endfor %}
  197. {%- endif %}
  198. {%- endif %}
  199. {%- for network in interface.get('use_ovs_ports', []) %}
  200. remove_interface_{{ network }}_line1:
  201. file.replace:
  202. - name: /etc/network/interfaces
  203. - pattern: auto {{ network }}
  204. - repl: ""
  205. remove_interface_{{ network }}_line2:
  206. file.replace:
  207. - name: /etc/network/interfaces
  208. - pattern: iface {{ network }} inet manual
  209. - repl: ""
  210. {%- endfor %}
  211. {%- if interface.gateway is defined %}
  212. linux_system_network:
  213. network.system:
  214. - enabled: {{ interface.enabled }}
  215. - hostname: {{ network.fqdn }}
  216. {%- if interface.gateway is defined %}
  217. - gateway: {{ interface.gateway }}
  218. - gatewaydev: {{ interface_name }}
  219. {%- endif %}
  220. - nozeroconf: True
  221. - nisdomain: {{ system.domain }}
  222. - require_reboot: True
  223. {%- endif %}
  224. {%- endif %}
  225. {%- endif %}
  226. {%- if interface.wireless is defined %}
  227. {%- if grains.os_family == 'Arch' %}
  228. linux_network_packages:
  229. pkg.installed:
  230. - pkgs: {{ network.pkgs }}
  231. /etc/netctl/network_{{ interface.wireless.essid }}:
  232. file.managed:
  233. - source: salt://linux/files/wireless
  234. - mode: 755
  235. - template: jinja
  236. - require:
  237. - pkg: linux_network_packages
  238. - defaults:
  239. interface_name: {{ interface_name }}
  240. switch_profile_{{ interface.wireless.essid }}:
  241. cmd.run:
  242. - name: netctl switch-to network_{{ interface.wireless.essid }}
  243. - cwd: /root
  244. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  245. - require:
  246. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  247. enable_profile_{{ interface.wireless.essid }}:
  248. cmd.run:
  249. - name: netctl enable network_{{ interface.wireless.essid }}
  250. - cwd: /root
  251. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  252. - require:
  253. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  254. {%- endif %}
  255. {%- endif %}
  256. {%- endif %}
  257. {%- if interface.route is defined %}
  258. linux_network_{{ interface_name }}_routes:
  259. network.routes:
  260. - name: {{ interface_name }}
  261. - routes:
  262. {%- for route_name, route in interface.route.iteritems() %}
  263. - name: {{ route_name }}
  264. ipaddr: {{ route.address }}
  265. netmask: {{ route.netmask }}
  266. {%- if route.gateway is defined %}
  267. gateway: {{ route.gateway }}
  268. {%- endif %}
  269. {%- endfor %}
  270. {%- endif %}
  271. {%- endfor %}
  272. {%- if network.bridge != 'none' %}
  273. linux_interfaces_final_include:
  274. file.prepend:
  275. - name: /etc/network/interfaces
  276. - text: |
  277. source /etc/network/interfaces.d/*
  278. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  279. source /etc/network/interfaces.u/*
  280. {%- endif %}
  281. {%- endif %}
  282. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  283. NetworkManager:
  284. service.dead:
  285. - enable: false
  286. {%- endif %}
  287. {%- if network.tap_custom_txqueuelen is defined %}
  288. /etc/udev/rules.d/60-net-txqueue.rules:
  289. file.managed:
  290. - source: salt://linux/files/60-net-txqueue.rules
  291. - mode: 755
  292. - template: jinja
  293. {%- endif %}