Skip to content

Instantly share code, notes, and snippets.

@mila
Created October 14, 2016 10:56
Show Gist options
  • Save mila/3ddba42a2e60057251470ed768ac6720 to your computer and use it in GitHub Desktop.
Save mila/3ddba42a2e60057251470ed768ac6720 to your computer and use it in GitHub Desktop.
Installs generated Debian package to remote system
#!/bin/bash
#
# Install generated Debian package to remote system
#
# Uploads packages directly to the target servers ignoring
# any repositories and installs them using `dpkg -i`.
#
# Restarts System V / Systemd services after successful installation.
#
set -e
if [[ ! -e debian/files ]]; then
>&2 echo "File debian/files does not exist. Build Debian package before deployment."
exit 1
fi
PACKAGES=( $(cut -d ' ' -f 1 debian/files) )
SERVICES=($(ls debian/*.service | sed 's~debian/\(.*\)\.service~\1~'))
if [[ $# -eq 0 ]] ; then
>&2 echo "Usage: $0 SERVER..."
>&2 echo
>&2 echo Packages: ${PACKAGES[@]}
>&2 echo Services: ${SERVICES[@]}
exit 2
fi
while [[ $# -gt 0 ]]; do
SERVER="$1"
echo "Server: ${SERVER}"
echo
(
echo "Uploading packages..."
scp ${PACKAGES[@]/#/../} "root@${SERVER}:/tmp/"
echo "Uploaded packages."
echo
)
(
echo "Installing packages..."
ssh "root@${SERVER}" "cd /tmp/; dpkg -i ${PACKAGES[@]}"
echo "Installed packages."
echo
)
(
for SERVICE in ${SERVICES[@]}
do
echo "Restarting service ${SERVICE}..."
ssh "root@${SERVER}" "service '${SERVICE}' restart"
echo "Restarted service ${SERVICE}."
echo
done
)
echo "Server ${SERVER} done."
echo
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment