Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/worker deploy #92

Open
wants to merge 3 commits into
base: feature/remote-worker
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion worker/deploy/ansible-playbook/deploy-worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
setup: bash -x setup.sh
tasks:
- name: "copy setup file"
copy: src="../setup.sh" dest="/home/ubuntu"
copy: src="../setup.sh" dest="."
- name: Setting up environment
shell: "{{ setup }}"
become: yes
Expand Down
3 changes: 0 additions & 3 deletions worker/deploy/hosts.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@ worker_ip_1=
remote_user=

ansible_ssh_private_key_file=

# Default
ansible_files_path=./ansible-playbook
79 changes: 61 additions & 18 deletions worker/deploy/install.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,78 @@
#!/bin/bash

readonly ANSIBLE_FILES_PATH_PATTERN="ansible_files_path"
readonly PRIVATE_KEY_FILE_PATH_PATTERN="ansible_ssh_private_key_file"
readonly DEPLOYED_WORKER_IP_PATTERN="worker_ip"
readonly REMOTE_USER_PATTERN="remote_user"
readonly DEPLOY_WORKER_YML_FILE="deploy-worker.yml"

readonly MY_PATH="`dirname \"$0\"`"
readonly MY_PATH="`( cd \"${MY_PATH}\" && pwd )`"
readonly ABSPATH=$(readlink -f $0)
readonly ABSDIR=$(dirname $ABSPATH)

if [ -z "$MY_PATH" ] ; then
if [ -z "$ABSDIR" ] ; then
# For some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1
fi

HOSTS_CONF_FILE="${MY_PATH}/hosts.conf"
readonly HOSTS_CONF_FILE="${ABSDIR}/hosts.conf"

ANSIBLE_FILES_PATH=$(grep "${ANSIBLE_FILES_PATH_PATTERN}" "${HOSTS_CONF_FILE}" | awk -F "=" '{print $2}')
ANSIBLE_HOSTS_FILE="${ANSIBLE_FILES_PATH}/hosts"
PRIVATE_KEY_FILE_PATH=$(grep "${PRIVATE_KEY_FILE_PATH_PATTERN}" "${HOSTS_CONF_FILE}" | awk -F "=" '{print $2}')
check_variables() {
for var in "$@"
do
if [ -z "${!var}" ]; then
echo "Error. The field ${var} was not set."
exit 1
fi
done
}

# Clears the worker machine addresses
sed -i '/\[worker-machine\]/,/\[worker-machine:vars\]/{//!d}' "${ANSIBLE_HOSTS_FILE}"
clean_machines_field() {
sed -i '/\[worker-machine\]/,/\[worker-machine:vars\]/{//!d}' "${1}"
}

# Fill worker address field of hosts file
grep "${DEPLOYED_WORKER_IP_PATTERN}" "${HOSTS_CONF_FILE}"| while read -r line ; do
DEPLOYED_WORKER_IP=$(echo ${line} | awk -F "=" '{print $2}')
sed -i "/\[worker-machine:vars\]/i ${DEPLOYED_WORKER_IP}" ${ANSIBLE_HOSTS_FILE}
done
add_machine() {
local ANSIBLE_HOSTS_FILE=$1
local MACHINE=$2
sed -i "/\[worker-machine:vars\]/i ${MACHINE}" ${ANSIBLE_HOSTS_FILE}
}

# Writes the path of the private key file in Ansible hosts file
sed -i "s#.*${PRIVATE_KEY_FILE_PATH_PATTERN}=.*#${PRIVATE_KEY_FILE_PATH_PATTERN}=${PRIVATE_KEY_FILE_PATH}#" ${ANSIBLE_HOSTS_FILE}
fill_machines_ip() {
local ANSIBLE_HOSTS_FILE=$1

(cd ${ANSIBLE_FILES_PATH} && ansible-playbook -vvv ${DEPLOY_WORKER_YML_FILE})
clean_machines_field ${ANSIBLE_HOSTS_FILE}

UNCOMMENTED_LINES=$(grep "^[^#;]" "${HOSTS_CONF_FILE}")
WORKER_MACHINES=$(grep "${DEPLOYED_WORKER_IP_PATTERN}" <<< ${UNCOMMENTED_LINES})

while read -r line ; do
WORKER_IP=$(echo ${line} | awk -F "=" '{print $2}')

if [ -z "${WORKER_IP}" ]; then
echo "Error. The field ${line} was not set."
exit 1
fi

add_machine ${ANSIBLE_HOSTS_FILE} ${WORKER_IP}
done <<< ${WORKER_MACHINES}
}

main() {
REMOTE_USER=$(grep "${REMOTE_USER_PATTERN}" "${HOSTS_CONF_FILE}" | awk -F "=" '{print $2}')
PRIVATE_KEY_FILE_PATH=$(grep "${PRIVATE_KEY_FILE_PATH_PATTERN}" "${HOSTS_CONF_FILE}" | awk -F "=" '{print $2}')

check_variables REMOTE_USER PRIVATE_KEY_FILE_PATH

ANSIBLE_FILES_PATH="${ABSDIR}/ansible-playbook"
ANSIBLE_HOSTS_FILE="${ANSIBLE_FILES_PATH}/hosts"
ANSIBLE_CFG_FILE="${ANSIBLE_FILES_PATH}/ansible.cfg"

fill_machines_ip ${ANSIBLE_HOSTS_FILE}

# Writes the path of the private key file in Ansible hosts file
sed -i "s#.*${PRIVATE_KEY_FILE_PATH_PATTERN}=.*#${PRIVATE_KEY_FILE_PATH_PATTERN}=${PRIVATE_KEY_FILE_PATH}#" ${ANSIBLE_HOSTS_FILE}
sed -i "s#.*${REMOTE_USER_PATTERN} =.*#${REMOTE_USER_PATTERN}=${REMOTE_USER}#" ${ANSIBLE_CFG_FILE}

(cd ${ANSIBLE_FILES_PATH} && ansible-playbook -vvv ${DEPLOY_WORKER_YML_FILE})
}

main