#!/bin/bash VAR_DIR="/var/local/machinepi" TAG_DIR="/var/local/machinepi/tag" source "${TAG_DIR}/vars" function fetch { ssh -q -o StrictHostKeyChecking=no -i "${TAG_DIR}/ssh-key" -p "${MPI_MASTER_PORT}" -l "${MPI_MASTER_USER}" "${MPI_MASTER_HOST}" $@ } function load_client_hash { if [ -f "${VAR_DIR}/package.hash" ]; then cat "${VAR_DIR}/package.hash" else return 1 fi } function load_master_hash { fetch package hash } function load_package_hash { hash=$( sha256sum "${VAR_DIR}/package.tar.gz" ) echo "${hash:0:64}" } function update { local client_hash=$( load_client_hash ) local master_hash=$( load_master_hash ) if [ -z "$master_hash" ] || [ "$master_hash" = "$client_hash" ]; then return 0 fi if [ -f "${VAR_DIR}/package/teardown.sh" ]; then "${VAR_DIR}/package/teardown.sh" if [ "$?" -ne "0" ]; then echo "Unable to teardown old package!" return 1 fi fi rm -r "${VAR_DIR}/package" fetch package code > "${VAR_DIR}/package.tar.gz" if [ "$?" -ne "0" ]; then echo "Unable to fetch new package!" return 1 fi local package_hash=$( load_package_hash ) if [ "$master_hash" != "$package_hash" ]; then echo "Unable to validate new package hash!" return 1 fi tar -C "${VAR_DIR}" -xzf "${VAR_DIR}/package.tar.gz" if [ "$?" -ne "0" ]; then echo "Unable to extract new package!" return 1 fi rm "${VAR_DIR}/package.tar.gz" if [ -f "${VAR_DIR}/package/setup.sh" ] ; then "${VAR_DIR}/package/setup.sh" if [ "$?" -ne "0" ]; then echo "Unable to setup new package!" return 1 fi fi echo "$master_hash" > "${VAR_DIR}/package.hash" } update