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.

288 lines
7.8KB

  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: 'source /etc/network/interfaces.d/*'
  58. ovs_port_{{ interface_name }}:
  59. file.managed:
  60. - name: /etc/network/interfaces.d/ifcfg-{{ interface_name }}
  61. - source: salt://linux/files/ovs_port
  62. - defaults:
  63. port: {{ interface|yaml }}
  64. port_name: {{ interface_name }}
  65. - template: jinja
  66. ovs_port_{{ interface_name }}_line1:
  67. file.replace:
  68. - name: /etc/network/interfaces
  69. - pattern: auto {{ interface_name }}
  70. - repl: ""
  71. ovs_port_{{ interface_name }}_line2:
  72. file.replace:
  73. - name: /etc/network/interfaces
  74. - pattern: 'iface {{ interface_name }} inet .*'
  75. - repl: ""
  76. ovs_port_up_{{ interface_name }}:
  77. cmd.run:
  78. - name: ifup {{ interface_name }}
  79. - require:
  80. - file: ovs_port_{{ interface_name }}
  81. - file: ovs_port_{{ interface_name }}_line1
  82. - file: ovs_port_{{ interface_name }}_line2
  83. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  84. {%- endif %}
  85. {%- else %}
  86. linux_interface_{{ interface_name }}:
  87. network.managed:
  88. - enabled: {{ interface.enabled }}
  89. - name: {{ interface_name }}
  90. - type: {{ interface.type }}
  91. {%- if interface.address is defined %}
  92. {%- if grains.os_family == 'Debian' %}
  93. - proto: {{ interface.get('proto', 'static') }}
  94. {% endif %}
  95. {%- if grains.os_family == 'RedHat' %}
  96. {%- if interface.get('proto', 'none') == 'manual' %}
  97. - proto: 'none'
  98. {%- else %}
  99. - proto: {{ interface.get('proto', 'none') }}
  100. {%- endif %}
  101. {% endif %}
  102. - ipaddr: {{ interface.address }}
  103. - netmask: {{ interface.netmask }}
  104. {%- else %}
  105. - proto: {{ interface.get('proto', 'dhcp') }}
  106. {%- endif %}
  107. {%- if interface.type == 'slave' %}
  108. - master: {{ interface.master }}
  109. {%- endif %}
  110. {%- if interface.name_servers is defined %}
  111. - dns: {{ interface.name_servers }}
  112. {%- endif %}
  113. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  114. {%- if interface.wireless.security == "wpa" %}
  115. - wpa-ssid: {{ interface.wireless.essid }}
  116. - wpa-psk: {{ interface.wireless.key }}
  117. {%- else %}
  118. - wireless-ssid: {{ interface.wireless.essid }}
  119. - wireless-psk: {{ interface.wireless.key }}
  120. {%- endif %}
  121. {%- endif %}
  122. {%- for param in network.interface_params %}
  123. {{ set_param(param, interface) }}
  124. {%- endfor %}
  125. {%- if interface.type == 'bridge' %}
  126. - bridge: {{ interface_name }}
  127. - delay: 0
  128. - bypassfirewall: True
  129. - use:
  130. {%- for network in interface.use_interfaces %}
  131. - network: linux_interface_{{ network }}
  132. {%- endfor %}
  133. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  134. - require:
  135. {%- for network in interface.get('use_interfaces', []) %}
  136. - network: linux_interface_{{ network }}
  137. {%- endfor %}
  138. {%- for network in interface.get('use_ovs_ports', []) %}
  139. - cmd: ovs_port_up_{{ network }}
  140. {%- endfor %}
  141. {%- endif %}
  142. {%- if interface.type == 'bond' %}
  143. - slaves: {{ interface.slaves }}
  144. - mode: {{ interface.mode }}
  145. {%- endif %}
  146. {%- for network in interface.get('use_ovs_ports', []) %}
  147. remove_interface_{{ network }}_line1:
  148. file.replace:
  149. - name: /etc/network/interfaces
  150. - pattern: auto {{ network }}
  151. - repl: ""
  152. remove_interface_{{ network }}_line2:
  153. file.replace:
  154. - name: /etc/network/interfaces
  155. - pattern: iface {{ network }} inet manual
  156. - repl: ""
  157. {%- endfor %}
  158. {%- if interface.gateway is defined %}
  159. linux_system_network:
  160. network.system:
  161. - enabled: {{ interface.enabled }}
  162. - hostname: {{ network.fqdn }}
  163. {%- if interface.gateway is defined %}
  164. - gateway: {{ interface.gateway }}
  165. - gatewaydev: {{ interface_name }}
  166. {%- endif %}
  167. - nozeroconf: True
  168. - nisdomain: {{ system.domain }}
  169. - require_reboot: True
  170. {%- endif %}
  171. {%- endif %}
  172. {%- endif %}
  173. {%- if interface.wireless is defined %}
  174. {%- if grains.os_family == 'Arch' %}
  175. linux_network_packages:
  176. pkg.installed:
  177. - pkgs: {{ network.pkgs }}
  178. /etc/netctl/network_{{ interface.wireless.essid }}:
  179. file.managed:
  180. - source: salt://linux/files/wireless
  181. - mode: 755
  182. - template: jinja
  183. - require:
  184. - pkg: linux_network_packages
  185. - defaults:
  186. interface_name: {{ interface_name }}
  187. switch_profile_{{ interface.wireless.essid }}:
  188. cmd.run:
  189. - name: netctl switch-to network_{{ interface.wireless.essid }}
  190. - cwd: /root
  191. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  192. - require:
  193. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  194. enable_profile_{{ interface.wireless.essid }}:
  195. cmd.run:
  196. - name: netctl enable network_{{ interface.wireless.essid }}
  197. - cwd: /root
  198. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  199. - require:
  200. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  201. {%- endif %}
  202. {%- endif %}
  203. {%- endif %}
  204. {%- if interface.route is defined %}
  205. linux_network_{{ interface_name }}_routes:
  206. network.routes:
  207. - name: {{ interface_name }}
  208. - routes:
  209. {%- for route_name, route in interface.route.iteritems() %}
  210. - name: {{ route_name }}
  211. ipaddr: {{ route.address }}
  212. netmask: {{ route.netmask }}
  213. gateway: {{ route.gateway }}
  214. {%- endfor %}
  215. {%- endif %}
  216. {%- endfor %}
  217. {%- if network.bridge != 'none' %}
  218. linux_interfaces_final_include:
  219. file.prepend:
  220. - name: /etc/network/interfaces
  221. - text: 'source /etc/network/interfaces.d/*'
  222. {%- endif %}
  223. {%- endif %}
  224. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  225. NetworkManager:
  226. service.dead:
  227. - enable: false
  228. {%- endif %}