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.

456 lines
13KB

  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. {% if interface.tag is defined %}
  95. ovs_port_set_tag_{{ interface_name }}:
  96. cmd.run:
  97. - name: ovs-vsctl{%- if network.ovs_nowait %} --no-wait{%- endif %} set port {{ interface_name }} tag={{ interface.tag }}
  98. - unless: ovs-vsctl get Port {{ interface_name }} tag | grep -Fx {{ interface.tag }}
  99. {%- endif %}
  100. {%- else %}
  101. linux_interfaces_include_{{ interface_name }}:
  102. file.prepend:
  103. - name: /etc/network/interfaces
  104. - text: |
  105. source /etc/network/interfaces.d/*
  106. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  107. source /etc/network/interfaces.u/*
  108. ovs_port_{{ interface_name }}:
  109. file.managed:
  110. - name: /etc/network/interfaces.u/ifcfg-{{ interface_name }}
  111. - makedirs: True
  112. - source: salt://linux/files/ovs_port
  113. - defaults:
  114. port: {{ interface|yaml }}
  115. port_name: {{ interface_name }}
  116. - template: jinja
  117. ovs_port_{{ interface_name }}_line1:
  118. file.replace:
  119. - name: /etc/network/interfaces
  120. - pattern: auto {{ interface_name }}$
  121. - repl: ""
  122. ovs_port_{{ interface_name }}_line2:
  123. file.replace:
  124. - name: /etc/network/interfaces
  125. - pattern: 'iface {{ interface_name }} inet .*'
  126. - repl: ""
  127. ovs_port_up_{{ interface_name }}:
  128. cmd.run:
  129. - name: ifup {{ interface_name }}
  130. - require:
  131. - file: ovs_port_{{ interface_name }}
  132. - file: ovs_port_{{ interface_name }}_line1
  133. - file: ovs_port_{{ interface_name }}_line2
  134. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  135. - file: linux_interfaces_final_include
  136. {%- endif %}
  137. {%- else %}
  138. linux_interface_{{ interface_name }}:
  139. network.managed:
  140. - enabled: {{ interface.enabled }}
  141. - name: {{ interface_name }}
  142. - type: {{ interface.type }}
  143. {%- if interface.address is defined %}
  144. {%- if grains.os_family == 'Debian' %}
  145. - proto: {{ interface.get('proto', 'static') }}
  146. {% endif %}
  147. {%- if grains.os_family == 'RedHat' %}
  148. {%- if interface.get('proto', 'none') == 'manual' %}
  149. - proto: 'none'
  150. {%- else %}
  151. - proto: {{ interface.get('proto', 'none') }}
  152. {%- endif %}
  153. {% endif %}
  154. - ipaddr: {{ interface.address }}
  155. - netmask: {{ interface.netmask }}
  156. {%- else %}
  157. - proto: {{ interface.get('proto', 'dhcp') }}
  158. {%- endif %}
  159. {%- if interface.type == 'slave' %}
  160. - master: {{ interface.master }}
  161. {%- endif %}
  162. {%- if interface.name_servers is defined %}
  163. - dns: {{ interface.name_servers }}
  164. {%- endif %}
  165. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  166. {%- if interface.wireless.security == "wpa" %}
  167. - wpa-ssid: {{ interface.wireless.essid }}
  168. - wpa-psk: {{ interface.wireless.key }}
  169. {%- else %}
  170. - wireless-ssid: {{ interface.wireless.essid }}
  171. - wireless-psk: {{ interface.wireless.key }}
  172. {%- endif %}
  173. {%- endif %}
  174. {%- for param in network.interface_params %}
  175. {{ set_param(param, interface) }}
  176. {%- endfor %}
  177. {%- if interface.require_interfaces is defined %}
  178. - require:
  179. {%- for netif in interface.get('require_interfaces', []) %}
  180. - network: linux_interface_{{ netif }}
  181. {%- endfor %}
  182. {%- for network in interface.get('use_ovs_ports', []) %}
  183. - cmd: ovs_port_up_{{ network }}
  184. {%- endfor %}
  185. {%- endif %}
  186. {%- if interface.type == 'bridge' %}
  187. - bridge: {{ interface_name }}
  188. - delay: 0
  189. - bypassfirewall: True
  190. - use:
  191. {%- for network in interface.use_interfaces %}
  192. - network: linux_interface_{{ network }}
  193. {%- endfor %}
  194. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  195. - require:
  196. {%- for network in interface.get('use_interfaces', []) %}
  197. - network: linux_interface_{{ network }}
  198. {%- endfor %}
  199. {%- for network in interface.get('use_ovs_ports', []) %}
  200. - cmd: ovs_port_up_{{ network }}
  201. {%- endfor %}
  202. {%- endif %}
  203. {%- if interface.type == 'bond' %}
  204. - slaves: {{ interface.slaves }}
  205. - mode: {{ interface.mode }}
  206. {%- endif %}
  207. {%- if salt['grains.get']('saltversion') < '2017.7' %}
  208. # TODO(ddmitriev): Remove this 'if .. endif' block completely when
  209. # switched to salt version 2017.7 that has the same functionality.
  210. {%- if interface.type == 'bond' and interface.enabled == True %}
  211. linux_bond_interface_{{ interface_name }}:
  212. cmd.run:
  213. - name: ifenslave {{ interface_name }} {{ interface.slaves }}
  214. - require:
  215. - network: linux_interface_{{ interface_name }}
  216. - onchanges:
  217. - network: linux_interface_{{ interface_name }}
  218. {%- for network in interface.slaves.split() %}
  219. - network: linux_interface_{{ network }}
  220. {%- endfor %}
  221. {%- endif %}
  222. {%- endif %}
  223. {%- for network in interface.get('use_ovs_ports', []) %}
  224. remove_interface_{{ network }}_line1:
  225. file.replace:
  226. - name: /etc/network/interfaces
  227. - pattern: auto {{ network }}$
  228. - repl: ""
  229. remove_interface_{{ network }}_line2:
  230. file.replace:
  231. - name: /etc/network/interfaces
  232. - pattern: iface {{ network }} inet manual
  233. - repl: ""
  234. {%- endfor %}
  235. {%- if interface.gateway is defined %}
  236. linux_system_network:
  237. network.system:
  238. - enabled: {{ interface.enabled }}
  239. - hostname: {{ network.fqdn }}
  240. {%- if interface.gateway is defined %}
  241. - gateway: {{ interface.gateway }}
  242. - gatewaydev: {{ interface_name }}
  243. {%- endif %}
  244. - nozeroconf: True
  245. - nisdomain: {{ system.domain }}
  246. - require_reboot: True
  247. {%- endif %}
  248. {%- endif %}
  249. {%- endif %}
  250. {%- if interface.wireless is defined %}
  251. {%- if grains.os_family == 'Arch' %}
  252. linux_network_packages:
  253. pkg.installed:
  254. - pkgs: {{ network.pkgs }}
  255. /etc/netctl/network_{{ interface.wireless.essid }}:
  256. file.managed:
  257. - source: salt://linux/files/wireless
  258. - mode: 755
  259. - template: jinja
  260. - require:
  261. - pkg: linux_network_packages
  262. - defaults:
  263. interface_name: {{ interface_name }}
  264. switch_profile_{{ interface.wireless.essid }}:
  265. cmd.run:
  266. - name: netctl switch-to network_{{ interface.wireless.essid }}
  267. - cwd: /root
  268. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  269. - require:
  270. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  271. enable_profile_{{ interface.wireless.essid }}:
  272. cmd.run:
  273. - name: netctl enable network_{{ interface.wireless.essid }}
  274. - cwd: /root
  275. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  276. - require:
  277. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  278. {%- endif %}
  279. {%- endif %}
  280. {%- endif %}
  281. {%- if interface.route is defined %}
  282. linux_network_{{ interface_name }}_routes:
  283. network.routes:
  284. - name: {{ interface_name }}
  285. - routes:
  286. {%- for route_name, route in interface.route.items() %}
  287. - name: {{ route_name }}
  288. ipaddr: {{ route.address }}
  289. netmask: {{ route.netmask }}
  290. {%- if route.gateway is defined %}
  291. gateway: {{ route.gateway }}
  292. {%- endif %}
  293. {%- endfor %}
  294. {%- if interface.noifupdown is defined %}
  295. - require_reboot: {{ interface.noifupdown }}
  296. {%- endif %}
  297. {%- endif %}
  298. {%- if interface.type in ('eth','ovs_port') %}
  299. {%- if interface.get('ipflush_onchange', False) %}
  300. linux_interface_ipflush_onchange_{{ interface_name }}:
  301. cmd.run:
  302. - name: "/sbin/ip address flush dev {{ interface_name }}"
  303. {%- if interface.type == 'eth' %}
  304. - onchanges:
  305. - network: linux_interface_{{ interface_name }}
  306. {%- elif interface.type == 'ovs_port' %}
  307. - onchanges:
  308. - file: ovs_port_{{ interface_name }}
  309. {%- endif %}
  310. {%- if interface.get('restart_on_ipflush', False) %}
  311. linux_interface_restart_on_ipflush_{{ interface_name }}:
  312. cmd.run:
  313. - name: "ifdown {{ interface_name }}; ifup {{ interface_name }};"
  314. - onchanges:
  315. - cmd: linux_interface_ipflush_onchange_{{ interface_name }}
  316. {%- endif %}
  317. {%- endif %}
  318. {%- endif %}
  319. {%- endfor %}
  320. {%- if network.bridge != 'none' %}
  321. linux_interfaces_final_include:
  322. file.prepend:
  323. - name: /etc/network/interfaces
  324. - text: |
  325. source /etc/network/interfaces.d/*
  326. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  327. source /etc/network/interfaces.u/*
  328. linux_interfaces_final_include_no_requisite:
  329. file.prepend:
  330. - name: /etc/network/interfaces
  331. - text: |
  332. source /etc/network/interfaces.d/*
  333. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  334. source /etc/network/interfaces.u/*
  335. {%- endif %}
  336. {%- endif %}
  337. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  338. NetworkManager:
  339. service.dead:
  340. - enable: false
  341. {%- endif %}
  342. {%- if network.tap_custom_txqueuelen is defined %}
  343. /etc/udev/rules.d/60-net-txqueue.rules:
  344. file.managed:
  345. - source: salt://linux/files/60-net-txqueue.rules
  346. - mode: 755
  347. - template: jinja
  348. - defaults:
  349. tap_custom_txqueuelen: {{ network.tap_custom_txqueuelen }}
  350. udev_reload_rules:
  351. cmd.run:
  352. - name: "/bin/udevadm control --reload-rules"
  353. - onchanges:
  354. - file: /etc/udev/rules.d/60-net-txqueue.rules
  355. udev_retrigger:
  356. cmd.run:
  357. - name: "/bin/udevadm trigger --attr-match=subsystem=net"
  358. - onchanges:
  359. - udev_reload_rules
  360. {%- endif %}