Created
April 13, 2018 18:16
-
-
Save dertin/a57c4d8e910f85c63267e18136a00f2d to your computer and use it in GitHub Desktop.
Upload the code from a local project to an EC2 instance in Amazon AWS
This file contains hidden or 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 | |
# Upload the code from a local project to an EC2 instance in Amazon AWS | |
# It will be necessary to adapt the script to the needs and requirements of your project | |
# Use at your own risk | |
# Configuration | |
readonly DEPLOY_NAME=DeployTesting | |
readonly DEPLOY_SOURCE=~/Projects/testing | |
readonly KEY_PAIRS=~/.ssh/keypairs.pem | |
readonly REMOTE_IP=100.100.100.100 | |
readonly REMOTE_USER=admin | |
readonly REMOTE_TARGET=/var/www/mysite.com/htdocs/ | |
readonly REMOTE_HOME=/home/admin/ | |
# Init | |
cd ~ | |
# Remote connection check | |
ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} date | |
# Compress the project files in local | |
rm -f ~/${DEPLOY_NAME}-ArtifactCode.zip && cd ${DEPLOY_SOURCE} && zip -r ~/${DEPLOY_NAME}-ArtifactCode.zip ./* && cd ~ | |
if [ ! -f ~/${DEPLOY_NAME}-ArtifactCode.zip ] | |
then | |
echo '********************************' | |
echo "${DEPLOY_NAME} Failed to create the ArtifactCode on the local host! - Canceled deployment" | |
echo '********************************' | |
exit 1 | |
fi | |
# Verify that there is no .lock file in the remote server | |
lock=`ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "[ -f ${REMOTE_HOME}${DEPLOY_NAME}.lock ] && echo '1' || echo '0'"` | |
if [ $lock -eq 1 ] | |
then | |
echo "${DEPLOY_NAME} Locked! - Canceled deployment" | |
exit 1 | |
fi | |
# Create the .lock file in the remote server | |
ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "touch ${REMOTE_HOME}${DEPLOY_NAME}.lock" | |
# Folders are prepared in the remote server | |
timestamps=$(date +%s); | |
ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "rm -rf ${REMOTE_HOME}${DEPLOY_NAME}" | |
ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "mkdir ${REMOTE_HOME}${DEPLOY_NAME}" | |
ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "mkdir ${REMOTE_HOME}${DEPLOY_NAME}/${timestamps}" | |
# Sent the compressed file with the source code to the remote server | |
scp -i ${KEY_PAIRS} ~/${DEPLOY_NAME}-ArtifactCode.zip ${REMOTE_USER}@${REMOTE_IP}:${REMOTE_HOME}${DEPLOY_NAME}/ | |
# Verify file ArtifactCode.zip | |
artifactCode=`ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "[ -f ${REMOTE_HOME}${DEPLOY_NAME}/${DEPLOY_NAME}-ArtifactCode.zip ] && echo '1' || echo '0'"` | |
if [ $artifactCode -eq 0 ] | |
then | |
echo '********************************' | |
echo "${DEPLOY_NAME} The source code was not found! - Canceled deployment" | |
echo '********************************' | |
exit 1 | |
fi | |
# Created the installation script | |
rm -f ~/startDeploy${DEPLOY_NAME}.sh | |
touch ~/startDeploy${DEPLOY_NAME}.sh | |
cat << EOF > ~/startDeploy${DEPLOY_NAME}.sh | |
#!/bin/bash | |
# Unzip the source code file in the remote server | |
unzip ${REMOTE_HOME}${DEPLOY_NAME}/${DEPLOY_NAME}-ArtifactCode.zip -d ${REMOTE_HOME}${DEPLOY_NAME}/${timestamps}/ | |
# Delete the current files in the destination folder and move the new source code files to the destination on the remote | |
sudo rm -rf ${REMOTE_TARGET}* | |
sudo mv ${REMOTE_HOME}${DEPLOY_NAME}/${timestamps}/* ${REMOTE_TARGET} | |
# Grant permissions in the remote server | |
sudo chown -R root:www-data ${REMOTE_TARGET} | |
sudo find ${REMOTE_TARGET} -type d -exec chmod 755 {} \; | |
sudo find ${REMOTE_TARGET} -type f -exec chmod 644 {} \; | |
# Permissions and custom commands | |
sudo chmod 660 ${REMOTE_TARGET}error.log | |
sudo chmod -R 770 ${REMOTE_TARGET}Particle/Core/tmp | |
# Delete the .lock file | |
rm -f ${REMOTE_HOME}${DEPLOY_NAME}.lock | |
echo 'Finished Deployment - ${DEPLOY_NAME}' | |
EOF | |
if [ ! -f ~/startDeploy${DEPLOY_NAME}.sh ] | |
then | |
echo '********************************' | |
echo "${DEPLOY_NAME} Failed to create the installation script for the deploy on the local host! - Canceled deployment" | |
echo '********************************' | |
exit 1 | |
fi | |
# Sending and executing the script in the remote server | |
scp -i ${KEY_PAIRS} ~/startDeploy${DEPLOY_NAME}.sh ${REMOTE_USER}@${REMOTE_IP}:${REMOTE_HOME}${DEPLOY_NAME}/ | |
ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "chmod +x ${REMOTE_HOME}${DEPLOY_NAME}/startDeploy${DEPLOY_NAME}.sh; ${REMOTE_HOME}${DEPLOY_NAME}/startDeploy${DEPLOY_NAME}.sh"; | |
# Verify that there is no .lock file in the remote server | |
lock=`ssh -i ${KEY_PAIRS} ${REMOTE_USER}@${REMOTE_IP} "[ -f ${REMOTE_HOME}${DEPLOY_NAME}.lock ] && echo '1' || echo '0'"` | |
if [ $lock -eq 1 ] | |
then | |
echo '********************************' | |
echo "${DEPLOY_NAME} Locked! - The deploy has been blocked. Surely the installation script did not end correctly." | |
echo '********************************' | |
exit 1 | |
else | |
echo '********************************' | |
echo '********** Good luck! **********' | |
echo '********************************' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment