Saltstack Official Linux Formula
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

544 lines
16KB

  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 | json }}
  19. {%- else %}
  20. - pkgs: {{ network.bridge_pkgs | json }}
  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.append:
  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_bond' %}
  130. ovs_bond_{{ interface_name }}:
  131. cmd.run:
  132. - name: ovs-vsctl add-bond {{ interface.bridge }} {{ interface_name }} {{ interface.slaves }} bond_mode={{ interface.mode }}
  133. - unless: ovs-vsctl show | grep -A 2 'Port.*{{ interface_name }}.'
  134. - require:
  135. - ovs_bridge_{{ interface.bridge }}_present
  136. ovs_bond_persistent_{{ interface_name }}:
  137. file.append:
  138. - name: /etc/network/interfaces.u/ifcfg-{{ interface.bridge }}
  139. - makedirs: True
  140. - source: salt://linux/files/ovs_port
  141. - template: jinja
  142. - context:
  143. port_name: {{ interface_name }}
  144. port: {{ interface|yaml }}
  145. - require:
  146. - ovs_bridge_{{ interface.bridge }}
  147. {%- elif interface.type == 'ovs_port' %}
  148. {%- if interface.get('port_type','internal') == 'patch' %}
  149. ovs_port_{{ interface_name }}_present:
  150. openvswitch_port.present:
  151. - name: {{ interface_name }}
  152. - bridge: {{ interface.bridge }}
  153. - require:
  154. {%- if dpdk_enabled and network.interface.get(interface.bridge, {}).get('type', 'ovs_bridge') == 'dpdk_ovs_bridge' %}
  155. - cmd: linux_network_dpdk_bridge_interface_{{ interface.bridge }}
  156. {%- else %}
  157. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}_present
  158. {%- endif %}
  159. ovs_port_set_type_{{ interface_name }}:
  160. cmd.run:
  161. - name: ovs-vsctl{%- if network.ovs_nowait %} --no-wait{%- endif %} set interface {{ interface_name }} type=patch
  162. - unless: ovs-vsctl show | grep -A 1 'Interface {{ interface_name }}' | grep patch
  163. ovs_port_set_peer_{{ interface_name }}:
  164. cmd.run:
  165. - name: ovs-vsctl{%- if network.ovs_nowait %} --no-wait{%- endif %} set interface {{ interface_name }} options:peer={{ interface.peer }}
  166. - unless: ovs-vsctl show | grep -A 2 'Interface {{ interface_name }}' | grep {{ interface.peer }}
  167. {% if interface.tag is defined %}
  168. ovs_port_set_tag_{{ interface_name }}:
  169. cmd.run:
  170. - name: ovs-vsctl{%- if network.ovs_nowait %} --no-wait{%- endif %} set port {{ interface_name }} tag={{ interface.tag }}
  171. - unless: ovs-vsctl get Port {{ interface_name }} tag | grep -Fx {{ interface.tag }}
  172. {%- endif %}
  173. {%- else %}
  174. linux_interfaces_include_{{ interface_name }}:
  175. file.prepend:
  176. - name: /etc/network/interfaces
  177. - text: |
  178. source /etc/network/interfaces.d/*
  179. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  180. source /etc/network/interfaces.u/*
  181. ovs_port_{{ interface_name }}:
  182. file.managed:
  183. - name: /etc/network/interfaces.u/ifcfg-{{ interface_name }}
  184. - makedirs: True
  185. - source: salt://linux/files/ovs_port
  186. - defaults:
  187. port: {{ interface|yaml }}
  188. port_name: {{ interface_name }}
  189. auto: ""
  190. iface_inet: ""
  191. - template: jinja
  192. ovs_port_up_{{ interface_name }}:
  193. cmd.run:
  194. - name: ifup {{ interface_name }}
  195. - require:
  196. - file: ovs_port_{{ interface_name }}
  197. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}_present
  198. - file: linux_interfaces_final_include
  199. {%- endif %}
  200. {%- else %}
  201. linux_interface_{{ interface_name }}:
  202. network.managed:
  203. - enabled: {{ interface.enabled }}
  204. - name: {{ interface_name }}
  205. - type: {{ interface.type }}
  206. {%- if interface.address is defined %}
  207. {%- if grains.os_family == 'Debian' %}
  208. - proto: {{ interface.get('proto', 'static') }}
  209. {% endif %}
  210. {%- if grains.os_family == 'RedHat' %}
  211. {%- if interface.get('proto', 'none') == 'manual' %}
  212. - proto: 'none'
  213. {%- else %}
  214. - proto: {{ interface.get('proto', 'none') }}
  215. {%- endif %}
  216. {% endif %}
  217. - ipaddr: {{ interface.address }}
  218. - netmask: {{ interface.netmask }}
  219. {%- else %}
  220. - proto: {{ interface.get('proto', 'dhcp') }}
  221. {%- endif %}
  222. # IPv6
  223. {%- if interface.enable_ipv6 is defined %}
  224. - enable_ipv6: {{ interface.enable_ipv6 }}
  225. {%- if interface.ipv6ipaddr is defined %}
  226. {%- if grains.os_family == 'Debian' %}
  227. - ipv6proto: {{ interface.get('ipv6proto', 'static') }}
  228. {%- endif %}
  229. - ipv6ipaddr: {{ interface.ipv6ipaddr }}
  230. - ipv6gateway: {{ interface.ipv6gateway }}
  231. - ipv6netmask: {{ interface.ipv6netmask }}
  232. {%- endif %}
  233. {%- endif %}
  234. {%- if interface.type == 'slave' %}
  235. - master: {{ interface.master }}
  236. {%- endif %}
  237. {%- if interface.name_servers is defined %}
  238. - dns: {{ interface.name_servers }}
  239. {%- endif %}
  240. {%- if interface.metric is defined and grains.os_family == 'Debian' %}
  241. - metric: {{ interface.metric }}
  242. {%- endif %}
  243. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  244. {%- if interface.wireless.security == "wpa" %}
  245. - wpa-ssid: {{ interface.wireless.essid }}
  246. - wpa-psk: {{ interface.wireless.key }}
  247. {%- else %}
  248. - wireless-ssid: {{ interface.wireless.essid }}
  249. - wireless-psk: {{ interface.wireless.key }}
  250. {%- endif %}
  251. {%- endif %}
  252. {%- if pillar.linux.network.noifupdown is defined %}
  253. - noifupdown: {{ pillar.linux.network.noifupdown }}
  254. {%- endif %}
  255. {%- for param in network.interface_params %}
  256. {{ set_param(param, interface) }}
  257. {%- endfor %}
  258. {%- if interface.require_interfaces is defined %}
  259. - require:
  260. {%- for netif in interface.get('require_interfaces', []) %}
  261. - network: linux_interface_{{ netif }}
  262. {%- endfor %}
  263. {%- for network in interface.get('use_ovs_ports', []) %}
  264. - cmd: ovs_port_up_{{ network }}
  265. {%- endfor %}
  266. {%- endif %}
  267. {%- if interface.type == 'bridge' %}
  268. - bridge: {{ interface_name }}
  269. - delay: 0
  270. - bypassfirewall: True
  271. - use:
  272. {%- for network in interface.use_interfaces %}
  273. - network: linux_interface_{{ network }}
  274. {%- endfor %}
  275. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  276. - require:
  277. {%- for network in interface.get('use_interfaces', []) %}
  278. - network: linux_interface_{{ network }}
  279. {%- endfor %}
  280. {%- for network in interface.get('use_ovs_ports', []) %}
  281. - cmd: ovs_port_up_{{ network }}
  282. {%- endfor %}
  283. {%- endif %}
  284. {%- if interface.type == 'bond' %}
  285. - slaves: {{ interface.slaves }}
  286. - mode: {{ interface.mode }}
  287. {%- endif %}
  288. {%- if salt['grains.get']('saltversion') < '2017.7' %}
  289. # TODO(ddmitriev): Remove this 'if .. endif' block completely when
  290. # switched to salt version 2017.7 that has the same functionality.
  291. {%- if interface.type == 'bond' and interface.enabled == True %}
  292. linux_bond_interface_{{ interface_name }}:
  293. cmd.run:
  294. - name: ifenslave {{ interface_name }} {{ interface.slaves }}
  295. - require:
  296. - network: linux_interface_{{ interface_name }}
  297. - onchanges:
  298. - network: linux_interface_{{ interface_name }}
  299. {%- for network in interface.slaves.split() %}
  300. - network: linux_interface_{{ network }}
  301. {%- endfor %}
  302. {%- endif %}
  303. {%- endif %}
  304. {%- for network in interface.get('use_ovs_ports', []) %}
  305. remove_interface_{{ network }}_line1:
  306. file.replace:
  307. - name: /etc/network/interfaces
  308. - pattern: auto {{ network }}$
  309. - repl: ""
  310. remove_interface_{{ network }}_line2:
  311. file.replace:
  312. - name: /etc/network/interfaces
  313. - pattern: iface {{ network }} inet manual
  314. - repl: ""
  315. {%- endfor %}
  316. {%- if interface.gateway is defined and network.resolv is not defined %}
  317. linux_system_network:
  318. network.system:
  319. - enabled: {{ interface.enabled }}
  320. - hostname: {{ network.fqdn }}
  321. {%- if interface.gateway is defined %}
  322. - gateway: {{ interface.gateway }}
  323. - gatewaydev: {{ interface_name }}
  324. {%- endif %}
  325. - nozeroconf: True
  326. - nisdomain: {{ system.domain }}
  327. - require_reboot: True
  328. {%- endif %}
  329. {%- endif %}
  330. {%- endif %}
  331. {%- if interface.wireless is defined %}
  332. {%- if grains.os_family == 'Arch' %}
  333. linux_network_packages:
  334. pkg.installed:
  335. - pkgs: {{ network.pkgs | json }}
  336. /etc/netctl/network_{{ interface.wireless.essid }}:
  337. file.managed:
  338. - source: salt://linux/files/wireless
  339. - mode: 755
  340. - template: jinja
  341. - require:
  342. - pkg: linux_network_packages
  343. - defaults:
  344. interface_name: {{ interface_name }}
  345. switch_profile_{{ interface.wireless.essid }}:
  346. cmd.run:
  347. - name: netctl switch-to network_{{ interface.wireless.essid }}
  348. - cwd: /root
  349. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  350. - require:
  351. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  352. enable_profile_{{ interface.wireless.essid }}:
  353. cmd.run:
  354. - name: netctl enable network_{{ interface.wireless.essid }}
  355. - cwd: /root
  356. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  357. - require:
  358. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  359. {%- endif %}
  360. {%- endif %}
  361. {%- endif %}
  362. {%- if interface.route is defined %}
  363. linux_network_{{ interface_name }}_routes:
  364. network.routes:
  365. - name: {{ interface_name }}
  366. - routes:
  367. {%- for route_name, route in interface.route.items() %}
  368. - name: {{ route_name }}
  369. ipaddr: {{ route.address }}
  370. netmask: {{ route.netmask }}
  371. {%- if route.gateway is defined %}
  372. gateway: {{ route.gateway }}
  373. {%- endif %}
  374. {%- endfor %}
  375. {%- if interface.noifupdown is defined %}
  376. - require_reboot: {{ interface.noifupdown }}
  377. {%- endif %}
  378. {%- endif %}
  379. {%- if interface.type in ('eth','ovs_port') %}
  380. {%- if interface.get('ipflush_onchange', False) %}
  381. linux_interface_ipflush_onchange_{{ interface_name }}:
  382. cmd.run:
  383. - name: "/sbin/ip address flush dev {{ interface_name }}"
  384. {%- if interface.type == 'eth' %}
  385. - onchanges:
  386. - network: linux_interface_{{ interface_name }}
  387. {%- elif interface.type == 'ovs_port' %}
  388. - onchanges:
  389. - file: ovs_port_{{ interface_name }}
  390. {%- endif %}
  391. {%- if interface.get('restart_on_ipflush', False) %}
  392. linux_interface_restart_on_ipflush_{{ interface_name }}:
  393. cmd.run:
  394. - name: "ifdown {{ interface_name }}; ifup {{ interface_name }};"
  395. - onchanges:
  396. - cmd: linux_interface_ipflush_onchange_{{ interface_name }}
  397. {%- endif %}
  398. {%- endif %}
  399. {%- endif %}
  400. {%- endfor %}
  401. {%- if network.bridge != 'none' %}
  402. linux_interfaces_final_include:
  403. file.prepend:
  404. - name: /etc/network/interfaces
  405. - text: |
  406. source /etc/network/interfaces.d/*
  407. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  408. source /etc/network/interfaces.u/*
  409. linux_interfaces_final_include_no_requisite:
  410. file.prepend:
  411. - name: /etc/network/interfaces
  412. - text: |
  413. source /etc/network/interfaces.d/*
  414. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  415. source /etc/network/interfaces.u/*
  416. {%- endif %}
  417. {%- endif %}
  418. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  419. NetworkManager:
  420. service.dead:
  421. - enable: false
  422. {%- endif %}
  423. {%- if network.tap_custom_txqueuelen is defined %}
  424. /etc/udev/rules.d/60-net-txqueue.rules:
  425. file.managed:
  426. - source: salt://linux/files/60-net-txqueue.rules
  427. - mode: 755
  428. - template: jinja
  429. - defaults:
  430. tap_custom_txqueuelen: {{ network.tap_custom_txqueuelen }}
  431. udev_reload_rules:
  432. cmd.run:
  433. - name: "/bin/udevadm control --reload-rules"
  434. - onchanges:
  435. - file: /etc/udev/rules.d/60-net-txqueue.rules
  436. udev_retrigger:
  437. cmd.run:
  438. - name: "/bin/udevadm trigger --attr-match=subsystem=net"
  439. - onchanges:
  440. - udev_reload_rules
  441. {%- endif %}