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.

210 lines
5.7KB

  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.gateway is defined %}
  113. linux_system_network:
  114. network.system:
  115. - enabled: {{ interface.enabled }}
  116. - hostname: {{ network.fqdn }}
  117. {%- if interface.gateway is defined %}
  118. - gateway: {{ interface.gateway }}
  119. - gatewaydev: {{ interface_name }}
  120. {%- endif %}
  121. - nozeroconf: True
  122. - nisdomain: {{ system.domain }}
  123. - require_reboot: False
  124. {%- endif %}
  125. {%- endif %}
  126. {%- endif %}
  127. {%- if interface.wireless is defined %}
  128. {%- if grains.os_family == 'Arch' %}
  129. linux_network_packages:
  130. pkg.installed:
  131. - names: {{ network.pkgs }}
  132. /etc/netctl/network_{{ interface.wireless.essid }}:
  133. file.managed:
  134. - source: salt://linux/files/wireless
  135. - mode: 755
  136. - template: jinja
  137. - require:
  138. - pkg: linux_network_packages
  139. - defaults:
  140. interface_name: {{ interface_name }}
  141. switch_profile_{{ interface.wireless.essid }}:
  142. cmd.run:
  143. - name: netctl switch-to network_{{ interface.wireless.essid }}
  144. - cwd: /root
  145. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  146. - require:
  147. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  148. enable_profile_{{ interface.wireless.essid }}:
  149. cmd.run:
  150. - name: netctl enable network_{{ interface.wireless.essid }}
  151. - cwd: /root
  152. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  153. - require:
  154. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  155. {%- endif %}
  156. {%- endif %}
  157. {%- endif %}
  158. {%- if interface.route is defined %}
  159. linux_network_{{ interface_name }}_routes:
  160. network.routes:
  161. - name: {{ interface_name }}
  162. - routes:
  163. {%- for route_name, route in interface.route.iteritems() %}
  164. - name: {{ route_name }}
  165. ipaddr: {{ route.address }}
  166. netmask: {{ route.netmask }}
  167. gateway: {{ route.gateway }}
  168. {%- endfor %}
  169. {%- endif %}
  170. {%- endfor %}
  171. {%- endif %}
  172. {%- endif %}