Automated SaltStack Master/Minion testrun on Ubuntu Linux
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.

170 lines
4.6 KiB

6 years ago
  1. #!/bin/bash
  2. # Salt Master/Minion preconfiguration script - Apache set-up
  3. # Copyright (C) 2018 Pekka Helenius
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ###########################################################
  18. # - This script creates default apache configuration for Salt minion running on localhost
  19. # - This script is meant to be run on Salt master
  20. # - Use as a part of the main shell script. Individual usage not recommended
  21. #
  22. # NOTE: Not exactly using Salt pillar configuration template
  23. #
  24. ###########################################################
  25. if [ $(id -u) -eq 0 ]; then
  26. if [ ! -f /srv/salt/top.sls ]; then
  27. cat <<PILLAR_TOP > /srv/salt/top.sls
  28. base:
  29. PILLAR_TOP
  30. fi
  31. if [ $(grep "\- apache" /srv/salt/top.sls | wc -l) -eq 0 ]; then
  32. tee -a /srv/salt/top.sls <<PILLAR_TOP_ADD > /dev/null
  33. 'defaultMinion':
  34. - apache
  35. PILLAR_TOP_ADD
  36. fi
  37. cat <<SAMPLE_SITE_CONF > /srv/salt/apache/samplesite.conf
  38. <VirtualHost *:80>
  39. ServerName {{ servername }}
  40. ServerAlias {{ serveralias }}
  41. ServerAdmin webmaster@localhost
  42. DocumentRoot {{ ('/var/www/html/' + grains_id + '/') }}
  43. ErrorLog ${APACHE_LOG_DIR}/error.log
  44. CustomLog ${APACHE_LOG_DIR}/access.log combined
  45. </VirtualHost>
  46. SAMPLE_SITE_CONF
  47. cat << DEFAULT_SITE > /srv/salt/apache/defaultsite.html
  48. Default site
  49. DEFAULT_SITE
  50. cat <<APACHE_DATA > /srv/salt/apache/init.sls
  51. {% set grains_id = grains['id'].lower() %}
  52. {% set grains_os = grains['os'].lower() %}
  53. {% set servername = grains_os + '.' + grains_id + '.com' %}
  54. {% set serveralias = 'www.' + grains_os + '.' + grains_id + '.com' %}
  55. {% if grains_os == 'ubuntu' %}
  56. {% set apache_pkgname = 'apache2' %}
  57. {% set apache_confdir = 'apache2' %}
  58. {% set apache_servicename = 'apache2.service' %}
  59. {% set curl_pkgname = 'curl' %}
  60. {# TODO: support for more OSes #}
  61. {% endif %}
  62. apache_install:
  63. pkg.installed:
  64. - pkgs:
  65. - {{ apache_pkgname }}
  66. - {{ curl_pkgname }}
  67. sample_page_conf:
  68. file.managed:
  69. - name: /etc/{{ apache_confdir }}/sites-available/{{ grains_id }}.conf
  70. - source:
  71. - salt://apache/samplesite.conf
  72. - mode: 0644
  73. - user: root
  74. - group: root
  75. - template: jinja
  76. - context:
  77. servername: {{ servername }}
  78. serveralias: {{ serveralias }}
  79. grains_id: {{ grains_id }}
  80. - require:
  81. - pkg: apache_install
  82. enable_sample_page:
  83. cmd.run:
  84. - name: 'a2ensite {{ grains_id }}.conf'
  85. - require:
  86. - file: sample_page_conf
  87. remove_default_index:
  88. file.managed:
  89. - name: /var/www/html/index.html
  90. - source: salt://apache/defaultsite.html
  91. - require:
  92. - pkg: apache_install
  93. sample_page_content:
  94. file.managed:
  95. - mode: 0644
  96. - user: root
  97. - group: root
  98. - makedirs: True
  99. - name: {{ ('/var/www/html/' + grains_id + '/index.html') }}
  100. - source: salt://apache/data/sampleindex.html
  101. - require:
  102. - cmd: enable_sample_page
  103. sample_page_js_content:
  104. file.managed:
  105. - mode: 0644
  106. - user: root
  107. - group: root
  108. - makedirs: True
  109. - name: {{ ('/var/www/html/' + grains_id + '/sampleindex_functions.js') }}
  110. - source: salt://apache/data/sampleindex_functions.js
  111. - require:
  112. - file: sample_page_content
  113. add_vhost_domain:
  114. file.append:
  115. - name: /etc/hosts
  116. - text: 127.0.0.1 {{ servername }}
  117. - require:
  118. - file: sample_page_content
  119. restart_apache:
  120. service.running:
  121. - name: {{ apache_servicename }}
  122. - watch:
  123. - file: add_vhost_domain
  124. - cmd: enable_sample_page
  125. test_page:
  126. http.query:
  127. - name: {{ 'http://' + servername }}
  128. - status: 200
  129. - require:
  130. - service: restart_apache
  131. APACHE_DATA
  132. if [ ! -f /srv/salt/apache/init.sls ] || \
  133. [ ! -f /srv/salt/top.sls ] || \
  134. [ ! -f /srv/salt/apache/samplesite.conf ] || \
  135. [ ! -f /srv/salt/apache/data/sampleindex.html ] || \
  136. [ ! -f /srv/salt/apache/data/sampleindex_functions.js ]; then
  137. echo "Salt files missing. Aborting."
  138. exit 1
  139. else
  140. echo -e "\e[1m**Salt -- state.apply output**\n\e[0m"
  141. salt 'defaultMinion' state.apply apache --state-output terse
  142. fi
  143. else
  144. echo "Run this script as root (or with sudo)"
  145. fi