I want to say at once that the purpose of this post is not to provide a universal solution to the problem of deploying code on the server, but to give an example,
one of the solutions to this problem. Whether this solution is suitable for your case is up to you .
So, the task of uploading code to the servers is one of the most frequent routine tasks in a programmer’s work. Very well this problem is consecrated
here . But still, many people solve it with the help of stupid copying of selected files via FTP, which conception leads to confusion and errors. Below I offer a more or less universal bash script that will automate the task.
The script was written for your needs, and they are probably different from yours, so use it not as a ready-made solution, but as an example, and be ready to file it with a file for your needs.
To upload the code to the server, first you need to commit all changes to svn. After that you can run the script. The script will do this:
1. unload project code from SVN
2.Zapakuet code in the archive
3. Using scp upload archive to server
4.Raspak it on the server
')
Note:ssh, . ssh-keygen, public key/private key. , , google , .
deploy.sh has a number of parameters, information on which you can get by running the program with the -h key
# sh deploy.sh -h
Code deployer: version 1.2
: sh deploy.sh [-y 0 | -y 1] [-d path/to/source] [-H your.server.ru] [-u username]
: sh deploy.sh -y 0 -H your.server.ru -d /tmp -u progger
SVN_PATH, , SVN .
SVN: your.svnserver.ru/svn/repos/ypurproject/trunk
:
y - envos: 0 - , 1 - () ( : 0)
u - SSH ( : )
d - ( : /opt/hosting/your.server.ru)
H - SSH ( : your.server.ru)
h -
By default, the unloading program is already configured, and the easiest way to use it is simply to run it without parameters. If you need to unload the envos code too, use the –y switch
# sh deploy.sh -y 1
In this case, the upload will take a little longer. During the unloading of the code, deploy.sh will inform you about what it is doing now.
Script code:#!/bin/bash
#
HOST= 'your.server.ru'
SVN_PATH= 'https://your.svnserver.ru/svn/repos/yourptoject/trunk/'
DOC_ROOT= "/opt/hosting/$HOST"
UPLOAD_ENVOS=0; # envos: 0 - , 1 - ()
USER= ''
bold= '\E[1m'
ebold= '\E[0m'
while getopts ":y:u:d:H:h" optname
do
case $optname in
"y" )
UPLOAD_ENVOS= "$OPTARG"
;;
"d" )
DOC_ROOT= "$OPTARG"
;;
"u" )
USER= "$OPTARG"
;;
"H" )
HOST= "$OPTARG"
;;
"h" )
echo -e "${bold}Yourproject code deployer: version 1.2 $ebold"
echo ": sh deploy.sh [-y 0 | -y 1] [-d path/to/source] [-H your.server.ru] [-u username]"
echo -e "${bold}: sh deploy.sh -y 0 -H your.server.ru -d /tmp -u progger $ebold"
echo ""
echo " SVN_PATH, , SVN .
SVN: $SVN_PATH"
echo ""
echo -e "${bold}: $ebold"
echo " y - envos: 0 - , 1 - () ( : $UPLOAD_ENVOS)"
echo " u - SSH ( : $USER)"
echo " d - ( : $DOC_ROOT)"
echo " H - SSH ( : $HOST)"
echo " h - "
echo ""
exit 0;
;;
*)
echo "Unknown parameter or option error with option - $OPTARG"
exit 1;
;;
esac
done
if [ $USER ]
then
SSH_HOST= "$USER@$HOST"
else
SSH_HOST=$HOST
fi
#rm -rf ./yourproject-tmp
echo "* $SSH_HOST"
echo '* SVN...'
svn export --force $SVN_PATH ./yourproject-tmp > /dev/ null
if [ $? -ne 0 ]
then
exit 1;
fi;
echo '* ...'
tar -czf yourproject.tar.gz yourproject-tmp
if [ $? -ne 0 ]
then
exit 1;
fi;
echo '* ...'
scp ./yourproject.tar.gz $SSH_HOST:$DOC_ROOT
if [ $? -ne 0 ]
then
exit 1;
fi;
echo '* e...'
ssh $SSH_HOST "cd $DOC_ROOT; tar -xzf yourproject.tar.gz 2> /dev/null && rm -rf $DOC_ROOT/op && mv $DOC_ROOT/yourproject-tmp $DOC_ROOT/op && chmod -R a+w $DOC_ROOT/op"
if [ $? -ne 0 ]
then
exit 1;
fi;
# envos ( )
if [ $UPLOAD_ENVOS -eq 1 ]
then
echo '* envos ( )...'
echo '* envos...'
tar -czf envos.tar.gz envos
if [ $? -ne 0 ]
then
exit 1;
fi;
echo ' * envos ...'
scp ./envos.tar.gz $SSH_HOST:$DOC_ROOT
if [ $? -ne 0 ]
then
exit 1;
fi;
echo ' * envos ...'
ssh $SSH_HOST "rm -rf $DOC_ROOT/envos && cd $DOC_ROOT/; tar -xzf envos.tar.gz 2> /dev/null"
if [ $? -ne 0 ]
then
exit 1;
fi;
fi
#
echo '* ...'
rm -rf ./yourproject-tmp
echo '* !'
* This source code was highlighted with Source Code Highlighter .
Example output:# sh deploy.sh
* The code will be uploaded to your.server.ru server
* Uploading code from SVN ...
* Create an archive ...
* Copy the archive to the server ...
yourproject.tar.gz 100% 269KB 89.6KB / s 00:03
* Unpack the archive on the server ...
* Clean ...
* Done!
In case of an error at any of the stages, the execution of the command will be interrupted.
I want to draw your attention to the -y option, which manages the
uploading of Envos.Framework . Long thought, remove this code or not. Decided to keep. Many of you probably use your favorite application framework, whose code does not need to be permanently unloaded. Often these frameworks are not small, and unloading can take extra time. The script takes this into account, but if you use another framework, you will need to rewrite the code a bit.
Well, in conclusion, I want to say that I am not a guru of bash, and I will be grateful for tips on improving the script code. On the other hand, I see some ways of optimization myself, but in such volumes of code, I think that taking away, for example, the verification code for the previous operation, into the function is hunting for fleas. But, of course, if the script develops and becomes more complex, such refactoring will become useful.
If you are still using FTP to upload your code, it’s time to think :)