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.

308 satır
8.3KB

  1. {%- from "linux/map.jinja" import network with context %}
  2. {%- from "linux/map.jinja" import system with context %}
  3. {%- if network.enabled %}
  4. {%- macro set_param(param_name, param_dict) -%}
  5. {%- if param_dict.get(param_name, False) -%}
  6. - {{ param_name }}: {{ param_dict[param_name] }}
  7. {%- endif -%}
  8. {%- endmacro -%}
  9. {%- if network.bridge != 'none' %}
  10. linux_network_bridge_pkgs:
  11. pkg.installed:
  12. {%- if network.bridge == 'openvswitch' %}
  13. - pkgs: {{ network.ovs_pkgs }}
  14. {%- else %}
  15. - pkgs: {{ network.bridge_pkgs }}
  16. {%- endif %}
  17. {%- endif %}
  18. {%- for interface_name, interface in network.interface.iteritems() %}
  19. {%- set interface_name = interface.get('name', interface_name) %}
  20. {# it is not used for any interface with type preffix dpdk,eg. dpdk_ovs_port #}
  21. {%- if interface.get('managed', True) and not 'dpdk' in interface.type %}
  22. {%- if grains.os_family in ['RedHat', 'Debian'] %}
  23. {%- if interface.type == 'ovs_bridge' %}
  24. ovs_bridge_{{ interface_name }}:
  25. openvswitch_bridge.present:
  26. - name: {{ interface_name }}
  27. {# add linux network interface into OVS bridge #}
  28. {%- for int_name, int in network.interface.iteritems() %}
  29. {%- set int_name = int.get('name', int_name) %}
  30. {%- if int.ovs_bridge is defined and interface_name == int.ovs_bridge %}
  31. add_int_{{ int_name }}_to_ovs_bridge_{{ interface_name }}:
  32. cmd.run:
  33. - unless: ovs-vsctl show | grep {{ int_name }}
  34. - name: ovs-vsctl add-port {{ interface_name }} {{ int_name }}
  35. {%- endif %}
  36. {%- endfor %}
  37. {%- elif interface.type == 'ovs_port' %}
  38. {%- if interface.get('port_type','internal') == 'patch' %}
  39. ovs_port_{{ interface_name }}:
  40. openvswitch_port.present:
  41. - name: {{ interface_name }}
  42. - bridge: {{ interface.bridge }}
  43. - require:
  44. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  45. ovs_port_set_type_{{ interface_name }}:
  46. cmd.run:
  47. - name: ovs-vsctl set interface {{ interface_name }} type=patch
  48. - unless: ovs-vsctl show | grep -A 1 'Interface {{ interface_name }}' | grep patch
  49. ovs_port_set_peer_{{ interface_name }}:
  50. cmd.run:
  51. - name: ovs-vsctl set interface {{ interface_name }} options:peer={{ interface.peer }}
  52. - unless: ovs-vsctl show | grep -A 2 'Interface floating-to-prv' | grep {{ interface.peer }}
  53. {%- else %}
  54. linux_interfaces_include_{{ interface_name }}:
  55. file.prepend:
  56. - name: /etc/network/interfaces
  57. - text: |
  58. source /etc/network/interfaces.d/*
  59. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  60. source /etc/network/interfaces.u/*
  61. ovs_port_{{ interface_name }}:
  62. file.managed:
  63. - name: /etc/network/interfaces.u/ifcfg-{{ interface_name }}
  64. - makedirs: True
  65. - source: salt://linux/files/ovs_port
  66. - defaults:
  67. port: {{ interface|yaml }}
  68. port_name: {{ interface_name }}
  69. - template: jinja
  70. ovs_port_{{ interface_name }}_line1:
  71. file.replace:
  72. - name: /etc/network/interfaces
  73. - pattern: auto {{ interface_name }}
  74. - repl: ""
  75. ovs_port_{{ interface_name }}_line2:
  76. file.replace:
  77. - name: /etc/network/interfaces
  78. - pattern: 'iface {{ interface_name }} inet .*'
  79. - repl: ""
  80. ovs_port_up_{{ interface_name }}:
  81. cmd.run:
  82. - name: ifup {{ interface_name }}
  83. - require:
  84. - file: ovs_port_{{ interface_name }}
  85. - file: ovs_port_{{ interface_name }}_line1
  86. - file: ovs_port_{{ interface_name }}_line2
  87. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  88. {%- endif %}
  89. {%- else %}
  90. linux_interface_{{ interface_name }}:
  91. network.managed:
  92. - enabled: {{ interface.enabled }}
  93. - name: {{ interface_name }}
  94. - type: {{ interface.type }}
  95. {% if interface.mtu is defined %}
  96. - mtu: {{ interface.mtu }}
  97. {% endif %}
  98. {%- if interface.address is defined %}
  99. {%- if grains.os_family == 'Debian' %}
  100. - proto: {{ interface.get('proto', 'static') }}
  101. {% endif %}
  102. {%- if grains.os_family == 'RedHat' %}
  103. {%- if interface.get('proto', 'none') == 'manual' %}
  104. - proto: 'none'
  105. {%- else %}
  106. - proto: {{ interface.get('proto', 'none') }}
  107. {%- endif %}
  108. {% endif %}
  109. - ipaddr: {{ interface.address }}
  110. - netmask: {{ interface.netmask }}
  111. {%- else %}
  112. - proto: {{ interface.get('proto', 'dhcp') }}
  113. {%- endif %}
  114. {%- if interface.type == 'slave' %}
  115. - master: {{ interface.master }}
  116. {%- endif %}
  117. {%- if interface.name_servers is defined %}
  118. - dns: {{ interface.name_servers }}
  119. {%- endif %}
  120. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  121. {%- if interface.wireless.security == "wpa" %}
  122. - wpa-ssid: {{ interface.wireless.essid }}
  123. - wpa-psk: {{ interface.wireless.key }}
  124. {%- else %}
  125. - wireless-ssid: {{ interface.wireless.essid }}
  126. - wireless-psk: {{ interface.wireless.key }}
  127. {%- endif %}
  128. {%- endif %}
  129. {%- for param in network.interface_params %}
  130. {{ set_param(param, interface) }}
  131. {%- endfor %}
  132. {%- if interface.type == 'bridge' %}
  133. - bridge: {{ interface_name }}
  134. - delay: 0
  135. - bypassfirewall: True
  136. - use:
  137. {%- for network in interface.use_interfaces %}
  138. - network: linux_interface_{{ network }}
  139. {%- endfor %}
  140. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  141. - require:
  142. {%- for network in interface.get('use_interfaces', []) %}
  143. - network: linux_interface_{{ network }}
  144. {%- endfor %}
  145. {%- for network in interface.get('use_ovs_ports', []) %}
  146. - cmd: ovs_port_up_{{ network }}
  147. {%- endfor %}
  148. {%- endif %}
  149. {%- if interface.type == 'bond' %}
  150. - slaves: {{ interface.slaves }}
  151. - mode: {{ interface.mode }}
  152. {%- endif %}
  153. {%- for network in interface.get('use_ovs_ports', []) %}
  154. remove_interface_{{ network }}_line1:
  155. file.replace:
  156. - name: /etc/network/interfaces
  157. - pattern: auto {{ network }}
  158. - repl: ""
  159. remove_interface_{{ network }}_line2:
  160. file.replace:
  161. - name: /etc/network/interfaces
  162. - pattern: iface {{ network }} inet manual
  163. - repl: ""
  164. {%- endfor %}
  165. {%- if interface.gateway is defined %}
  166. linux_system_network:
  167. network.system:
  168. - enabled: {{ interface.enabled }}
  169. - hostname: {{ network.fqdn }}
  170. {%- if interface.gateway is defined %}
  171. - gateway: {{ interface.gateway }}
  172. - gatewaydev: {{ interface_name }}
  173. {%- endif %}
  174. - nozeroconf: True
  175. - nisdomain: {{ system.domain }}
  176. - require_reboot: True
  177. {%- endif %}
  178. {%- endif %}
  179. {%- endif %}
  180. {%- if interface.wireless is defined %}
  181. {%- if grains.os_family == 'Arch' %}
  182. linux_network_packages:
  183. pkg.installed:
  184. - pkgs: {{ network.pkgs }}
  185. /etc/netctl/network_{{ interface.wireless.essid }}:
  186. file.managed:
  187. - source: salt://linux/files/wireless
  188. - mode: 755
  189. - template: jinja
  190. - require:
  191. - pkg: linux_network_packages
  192. - defaults:
  193. interface_name: {{ interface_name }}
  194. switch_profile_{{ interface.wireless.essid }}:
  195. cmd.run:
  196. - name: netctl switch-to network_{{ interface.wireless.essid }}
  197. - cwd: /root
  198. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  199. - require:
  200. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  201. enable_profile_{{ interface.wireless.essid }}:
  202. cmd.run:
  203. - name: netctl enable network_{{ interface.wireless.essid }}
  204. - cwd: /root
  205. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  206. - require:
  207. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  208. {%- endif %}
  209. {%- endif %}
  210. {%- endif %}
  211. {%- if interface.route is defined %}
  212. linux_network_{{ interface_name }}_routes:
  213. network.routes:
  214. - name: {{ interface_name }}
  215. - routes:
  216. {%- for route_name, route in interface.route.iteritems() %}
  217. - name: {{ route_name }}
  218. ipaddr: {{ route.address }}
  219. netmask: {{ route.netmask }}
  220. gateway: {{ route.gateway }}
  221. {%- endfor %}
  222. {%- endif %}
  223. {%- endfor %}
  224. {%- if network.bridge != 'none' %}
  225. linux_interfaces_final_include:
  226. file.prepend:
  227. - name: /etc/network/interfaces
  228. - text: |
  229. source /etc/network/interfaces.d/*
  230. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  231. source /etc/network/interfaces.u/*
  232. {%- endif %}
  233. {%- endif %}
  234. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  235. NetworkManager:
  236. service.dead:
  237. - enable: false
  238. {%- endif %}
  239. {%- if network.tap_custom_txqueuelen is defined %}
  240. /etc/udev/rules.d/60-net-txqueue.rules:
  241. file.managed:
  242. - source: salt://linux/files/60-net-txqueue.rules
  243. - mode: 755
  244. - template: jinja
  245. {%- endif %}