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.

509 lines
14KB

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