Saltstack Official Linux Formula
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

460 rindas
13KB

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