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.

449 line
12KB

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