Created
February 16, 2016 14:17
-
-
Save donniev/4a006781af544fdda1a5 to your computer and use it in GitHub Desktop.
Creating consul template file for nginx conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
declare -a rArray=$( yaml2json docker-compose.yml |jq ".nginx.links" |awk -F: 'BEGIN{out="\"nginx\""}/:/{out=out$1"\" ";ct++}END{print("("out")")}'|sed 's/,)/\)/') | |
echo $rArray | |
function preRow(){ | |
for i in "${rArray[@]}" | |
do | |
echo '(or ' | |
done | |
} | |
function startRow(){ | |
for i in "${rArray[@]}" | |
do | |
echo "(eq .Name \"$i\"))" | |
done | |
} | |
function getPre(){ | |
cat <<EOF | |
upstream {{.Name}} {\n | |
{{range service .Name}}\n | |
server {{.Address}}:{{.Port}};\n | |
{{end}}\n | |
}\n | |
EOF | |
} | |
function getDefaultHeader(){ | |
cat << EOF | |
server {\n | |
listen 80;\n | |
server_name _;\n | |
location /health.txt {\n | |
add_header content-type "text/html";\n | |
alias /usr/share/nginx/html/index.html;\n | |
}\n | |
EOF | |
} | |
function getDefaultServer(){ | |
cat << EOF | |
location /{{.Name}}/ {\n | |
proxy_pass http://{{.Name}}/;\n | |
proxy_redirect off;\n | |
proxy_http_version 1.1;\n | |
proxy_set_header Upgrade \$http_upgrade;\n | |
proxy_set_header Connection "upgrade";\n | |
proxy_set_header Host \$host;\n | |
proxy_set_header X-Forwarded-Host \$host;\n | |
proxy_set_header X-Forwarded-Server \$host;\n | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n | |
proxy_set_header X-Real-IP \$remote_addr;\n | |
}\n | |
location = /{{.Name}} {\n | |
return 302 /{{.Name}}/;\n | |
}\n | |
EOF | |
} | |
function getDefaultFooter(){ | |
cat <<EOF | |
{{end}}\n | |
}\n | |
EOF | |
} | |
function getSingleService(){ | |
cat <<EOF | |
server {\n | |
server_name {{plugin "node" "/application/index.js" "--task" "serverAlias" "--service" .Name}};\n | |
listen 80 ;\n | |
location / {\n | |
proxy_redirect off;\n | |
proxy_http_version 1.1;\n | |
proxy_set_header Upgrade \$http_upgrade;\n | |
proxy_set_header Connection "upgrade";\n | |
proxy_set_header Host \$host;\n | |
proxy_set_header X-Forwarded-Host \$host;\n | |
proxy_set_header X-Forwarded-Server \$host;\n | |
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n | |
proxy_set_header X-Real-IP \$remote_addr;\n | |
proxy_pass http://{{.Name}}/;\n | |
}\n | |
location = "" {\n | |
return 302 /;\n | |
}\n | |
}\n | |
EOF | |
} | |
echo '{{range services }}'>nginx/default.ctmpl | |
needspost=$(echo '{{if '$(preRow) $(startRow) '}}') | |
echo $needspost >>nginx/default.ctmpl | |
echo -e $(getPre)>>nginx/default.ctmpl | |
echo '{{end}}'>>nginx/default.ctmpl | |
echo '{{end}}'>>nginx/default.ctmpl | |
echo -e $(getDefaultHeader)>>nginx/default.ctmpl | |
echo '{{range services }}'>>nginx/default.ctmpl | |
echo $needspost >>nginx/default.ctmpl | |
echo -e $(getDefaultServer)>>nginx/default.ctmpl | |
echo '{{end}}'>>nginx/default.ctmpl | |
echo -e $(getDefaultFooter)>>nginx/default.ctmpl | |
echo '{{range services }}'>>nginx/default.ctmpl | |
echo $needspost >>nginx/default.ctmpl | |
echo -e $(getSingleService)>>nginx/default.ctmpl | |
echo '{{end}}'>>nginx/default.ctmpl | |
echo '{{end}}'>>nginx/default.ctmpl | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment