diff --git a/README.md b/README.md index 3e2b691..dbca67a 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -This repository is used to build playbooks based on files that contain role name and version number. \ No newline at end of file +This repository is used to build playbooks based on files that contain role name and version number. + +Usage: + +First, define a branch to use in /ansible/branch. +Next, download just the file build_ansible.sh with the following command: +```curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/prod/build_ansible.sh" -H "accept: application/raw" -o /ansible/build_ansible.sh``` + Note: the prod version of this script can be used. If there is a branch mismatch, this script will download the correct version and restart +Finally, run build_ansible.sh \ No newline at end of file diff --git a/build_ansible.sh b/build_ansible.sh new file mode 100644 index 0000000..e12e66b --- /dev/null +++ b/build_ansible.sh @@ -0,0 +1,120 @@ +#!/bin/bash +# ensure we are running the latest version of the script from the correct branch +if [ -f /ansible/branch ]; +then + branch=`cat /ansible/branch` +else + branch="prod" +fi + +#determine if we need to download the latest version +regrab=false +if [ -f /ansible/current ]; then + latest=`cat /ansible/current` + echo $latest + l_branch=`echo "$latest" | grep -Po ".+:" | grep -Po "[^:]+"` + l_time=`echo "$latest" | grep -Po ":.+" | grep -Po "[^:]+"` + if [ ! "$l_branch" = "$branch" ]; then + regrab=true + else + c_time=`date +%s` + if [ `expr $c_time - $l_time` -gt 120 ]; then + regrab=true + fi + fi +else + regrab=true +fi + +if $regrab;then + # download latest version + curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/build_ansible.sh" -H "accept: application/raw" -o /tmp/build_ansible.sh + + # verify that download was sucessful + count=`grep -P "(#!/bin/bash)|(end of build_ansible.sh)" /tmp/build_ansible.sh | wc -l` + if [ "$count" != "2" ];then + echo "failed to grab latest build_ansible.sh file, exiting" + exit + fi + + # update build_ansible file and update branch/timestamp info in current + cp /tmp/build_ansible.sh /ansible/build_ansible.sh + echo ${branch}:$c_time > /ansible/current + + # verify the parent script is not build_ansible.sh (to prevent the possibility of recurring until the system crashes in the case of an issue) + me=$(basename "$0") + parent=$(ps -o args= $PPID) + if [[ "$PARENT" != *"$ME"* ]];then + /bin/bash /ansible/build_ansible.sh + fi + exit +fi + +fqdn=`hostnamectl hostname` +curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/playbook_templates/$fqdn" -H "accept: application/raw" -o /tmp/$fqdn + +# verify file was downloaded successfully +count=`grep -P "end of file" /tmp/$fqdn | wc -l` +if [ "$count" != "2" ];then +echo "failed to grab current playbook build file for $fqdn, exiting" +exit +fi + +# build playbook, retrieve roles and add variables to roles +mkdir /ansible/roles/ +mkdir -p /ansible/new/roles +mkdir -p /ansible/prev/roles +# delete roles from previous attempt to update them +rm -r /ansible/new/roles/* + +# create base playbook +echo "- hosts: $fqdn + become: true + + roles:" > /ansible/new/$fqdn.yml + +lines=`cat /tmp/$fqdn` +for line in $lines +do + elements=() + for element in $(echo $line | tr ";" "\n"); do elements+=("$e") + case ${elements[0]} in + "role") + # format role:role name:branch/tag to use + # clone role repo from specific tag/branch w/o history + git clone -b ${elements[2]} --depth 1 https://git.actcur.com/actcur-ansible/role-${element[1]}.git:/ansible/new/${element[0]} + + # verify branch was cloned successfully + count=`ls /new/$role/ | wc -l` + if [[ $count -lt 9 ]];then + echo "issue grabbing role $role - dir/file count less than 9, exiting" + exit + fi + mv /tmp/roles/${element[0]} /ansible/new/roles + echo " - nginx-ssl" >> /ansible/new/$fqdn.yml + + # retrieve var file if it exists + curl -k "https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/var_files/${elements[0]}/$fqdn" -H "accept: application/raw" -o /ansible/new/roles/vars/main.yml + + # verifty file was downloaded successfully + count=`grep -P "(---)|(end of file)" /tmp/build_ansible.sh | wc -l` + if [ "$count" != "2" ];then + echo "issue grabbing vars file for ${element[0]} role on $fqdn - either file is invalid or download filed, exiting" + exit + fi + ;; + "#*") + # this is a comment, ignore it + ;; + *) + echo "skipping invalid entry in playbook build file for $fqdn" + echo "entry: $line" + ;; + esac +done +rm -r /ansible/prev/* +mv /ansible/$fqdn.yml /ansible/prev/ +mv /ansible/roles/ /ansible/prev +mv /ansible/new /ansible + +# end of build_ansible.sh \ No newline at end of file diff --git a/playbook_templates/privtorrents.actcur.com b/playbook_templates/privtorrents.actcur.com new file mode 100644 index 0000000..9c28172 --- /dev/null +++ b/playbook_templates/privtorrents.actcur.com @@ -0,0 +1,5 @@ +role:deluge:branch:testing +role:nginx-ssl:branch:testing +role:certbot:branch:testing + +# end of file \ No newline at end of file diff --git a/var_files/nginx-ssl/privtorrents.actcur.com b/var_files/nginx-ssl/privtorrents.actcur.com new file mode 100644 index 0000000..79a90ff --- /dev/null +++ b/var_files/nginx-ssl/privtorrents.actcur.com @@ -0,0 +1,7 @@ +--- +# vars file for nginx-ssl on privtorrents.actcur.com + +privtorrents.actcur.com: + port: 8112 + +# end of file \ No newline at end of file