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.

220 line
6.0KB

  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. - names: {{ network.ovs_pkgs }}
  14. {%- else %}
  15. - names: {{ network.bridge_pkgs }}
  16. {%- endif %}
  17. {%- endif %}
  18. {%- if not network.network_manager %}
  19. {# TODO stop/disable/uninstall network manager #}
  20. {%- for interface_name, interface in network.interface.iteritems() %}
  21. {%- if interface.get('managed', True) %}
  22. {%- if grains.os_family in ['RedHat', 'Debian'] %}
  23. {%- if interface.type == 'bridge' and network.bridge == 'openvswitch' %}
  24. linux_interface_{{ interface_name }}:
  25. network.managed:
  26. - enabled: {{ interface.enabled }}
  27. - name: {{ interface_name }}
  28. - type: eth
  29. {%- if interface.address is defined %}
  30. - proto: {{ interface.get('proto', 'static') }}
  31. - ipaddr: {{ interface.address }}
  32. - netmask: {{ interface.netmask }}
  33. {%- else %}
  34. - proto: {{ interface.get('proto', 'dhcp') }}
  35. {%- endif %}
  36. {%- if interface.name_servers is defined %}
  37. - dns: {{ interface.name_servers }}
  38. {%- endif %}
  39. {%- for param in network.interface_params %}
  40. {{ set_param(param, interface) }}
  41. {%- endfor %}
  42. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  43. {%- if interface.wireless.security == "wpa" %}
  44. - wpa-ssid: {{ interface.wireless.essid }}
  45. - wpa-psk: {{ interface.wireless.key }}
  46. {%- else %}
  47. - wireless-ssid: {{ interface.wireless.essid }}
  48. - wireless-psk: {{ interface.wireless.key }}
  49. {%- endif %}
  50. {%- endif %}
  51. - require:
  52. - pkg: linux_network_bridge_pkgs
  53. {%- for network in interface.use_interfaces %}
  54. - network: linux_interface_{{ network }}
  55. {%- endfor %}
  56. linux_ovs_bridge_{{ interface_name }}:
  57. cmd.run:
  58. - name: ovs-vsctl add-br {{ interface_name }}
  59. - unless: ovs-vsctl show | grep 'Bridge {{ interface_name }}'
  60. - require:
  61. - network: linux_interface_{{ interface_name }}
  62. {%- for port in interface.use_interfaces %}
  63. linux_ovs_bridge_{{ interface_name }}_port_{{ port }}:
  64. cmd.run:
  65. - name: ovs-vsctl add-port {{ interface_name }} {{ port }}
  66. - unless: ovs-vsctl show | grep 'Interface "{{ interface_name }}"'
  67. - require:
  68. - cmd: linux_ovs_bridge_{{ interface_name }}
  69. {%- endfor %}
  70. {%- else %}
  71. linux_interface_{{ interface_name }}:
  72. network.managed:
  73. - enabled: {{ interface.enabled }}
  74. - name: {{ interface_name }}
  75. - type: {{ interface.type }}
  76. {%- if interface.address is defined %}
  77. - proto: {{ interface.get('proto', 'static') }}
  78. - ipaddr: {{ interface.address }}
  79. - netmask: {{ interface.netmask }}
  80. {%- else %}
  81. - proto: {{ interface.get('proto', 'dhcp') }}
  82. {%- endif %}
  83. {%- if interface.name_servers is defined %}
  84. - dns: {{ interface.name_servers }}
  85. {%- endif %}
  86. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  87. {%- if interface.wireless.security == "wpa" %}
  88. - wpa-ssid: {{ interface.wireless.essid }}
  89. - wpa-psk: {{ interface.wireless.key }}
  90. {%- else %}
  91. - wireless-ssid: {{ interface.wireless.essid }}
  92. - wireless-psk: {{ interface.wireless.key }}
  93. {%- endif %}
  94. {%- endif %}
  95. {%- for param in network.interface_params %}
  96. {{ set_param(param, interface) }}
  97. {%- endfor %}
  98. {%- if interface.type == 'bridge' %}
  99. - bridge: {{ interface_name }}
  100. - delay: 0
  101. - bypassfirewall: True
  102. - use:
  103. {%- for network in interface.use_interfaces %}
  104. - network: {{ network }}
  105. {%- endfor %}
  106. - ports: {% for network in interface.use_interfaces %}{{ network }} {% endfor %}
  107. - require:
  108. {%- for network in interface.use_interfaces %}
  109. - network: linux_interface_{{ network }}
  110. {%- endfor %}
  111. {%- endif %}
  112. {%- if interface.type == 'vlan' %}
  113. - use:
  114. {%- for network in interface.use_interfaces %}
  115. - network: {{ network }}
  116. {%- endfor %}
  117. - require:
  118. {%- for network in interface.use_interfaces %}
  119. - network: {{ network }}
  120. {%- endfor %}
  121. {%- endif %}
  122. {%- if interface.gateway is defined %}
  123. linux_system_network:
  124. network.system:
  125. - enabled: {{ interface.enabled }}
  126. - hostname: {{ network.fqdn }}
  127. {%- if interface.gateway is defined %}
  128. - gateway: {{ interface.gateway }}
  129. - gatewaydev: {{ interface_name }}
  130. {%- endif %}
  131. - nozeroconf: True
  132. - nisdomain: {{ system.domain }}
  133. - require_reboot: False
  134. {%- endif %}
  135. {%- endif %}
  136. {%- endif %}
  137. {%- if interface.wireless is defined %}
  138. {%- if grains.os_family == 'Arch' %}
  139. linux_network_packages:
  140. pkg.installed:
  141. - names: {{ network.pkgs }}
  142. /etc/netctl/network_{{ interface.wireless.essid }}:
  143. file.managed:
  144. - source: salt://linux/files/wireless
  145. - mode: 755
  146. - template: jinja
  147. - require:
  148. - pkg: linux_network_packages
  149. - defaults:
  150. interface_name: {{ interface_name }}
  151. switch_profile_{{ interface.wireless.essid }}:
  152. cmd.run:
  153. - name: netctl switch-to network_{{ interface.wireless.essid }}
  154. - cwd: /root
  155. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  156. - require:
  157. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  158. enable_profile_{{ interface.wireless.essid }}:
  159. cmd.run:
  160. - name: netctl enable network_{{ interface.wireless.essid }}
  161. - cwd: /root
  162. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  163. - require:
  164. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  165. {%- endif %}
  166. {%- endif %}
  167. {%- endif %}
  168. {%- if interface.route is defined %}
  169. linux_network_{{ interface_name }}_routes:
  170. network.routes:
  171. - name: {{ interface_name }}
  172. - routes:
  173. {%- for route_name, route in interface.route.iteritems() %}
  174. - name: {{ route_name }}
  175. ipaddr: {{ route.address }}
  176. netmask: {{ route.netmask }}
  177. gateway: {{ route.gateway }}
  178. {%- endfor %}
  179. {%- endif %}
  180. {%- endfor %}
  181. {%- endif %}
  182. {%- endif %}