This is a creation in Article, where the information may have evolved or changed.
beego
is Golang write the application open source framework http://beego.me/, I use ' beego ' to write a project small project, Golang is the compilation language, need to compile after the deployment, each deployment to the service is a very troublesome thing. wrote a script to automatically package the deployment to the server to facilitate the release of the deployment.
##** Project Structure * *
First look at the structure of the Beego project:
beepkg|-- conf| `-- app.conf|-- controllers| `-- default.go|-- main.go|-- models|-- routers| `-- router.go|-- static| |-- css| |-- img| `-- js|-- tests| `-- default_test.go`-- views `-- index.tpl
Among them controllers
, views
. models
and other. Go files are packaged as executable beepkg.
Static files static
and conf
files are not compiled and need to be packaged and uploaded to the server.
Therefore, some files need to be ignored when packaging files:
1.go files that have been compiled into executable files
2. Locally generated temporary files
3. Some configuration files for the project
Therefore bee pack
, we need to ignore these files when we use packaging, the bee pack
three kinds of files are ignored by default when they are compressed after compilation:. Go:. Ds_store:.tmp. So we just need to ignore the other files.
Use a command to -exr
ignore a file with a regular expression:
Bee pack-be goos=linux-ba-exr= ' ^[0-9a-f]| [*.iml]$ '
Because of the use of the file storage session, the local directory will generate the session of the folder needs to be ignored packaging [0-9a-f]
, [*.iml]
is the IntelliJ generated project configuration file, when packaging needs to ignore these files.
##** Packaging Script * *
Three things need to be done to package the Beego project and sync to the service side:
1. Update the Code
2. Compiling the executable file, packaging the resources
3. Synchronizing to a compressed package to a server
4. Server decompression, restart the application
The first step is to update the code:
git reset HEAD --hardgit pull origin master
Then you need to compile the executable and package the resources:
bee pack -be GOOS=linux -ba -exr='^[0-9a-f]|[*.iml]$'
To synchronize compressed packets to the server:
scp .tar.gz jjz@192.168.1.10:/root/goapp/beepkg
The decompression and restart service needs to be performed on the server side and can be executed ssh
on the server side by executing the script:
ssh jjz@192.168.1.10 'bash -s' < restart.sh
# #Supervisor
The server can use the tar
command to extract files, but how to restart the application, on the server side can use the Supervisor
management Beego process, restart the application.
Supervisor
is a python -written process management tool that uses a process that needs to run all the time (Web services), but this process can be interrupted for a variety of reasons. When the process is interrupted and want to restart it automatically, this time, we need to use Supervisor
, here we are more to take advantage Supervisor
of the restart process function.
Install Supervisor:
Pip Install Supervisor
Initialize the configuration file:
Echo_supervisord_conf >/etc/supervisord.conf
To modify the configuration file /etc/supervisord.conf
add configuration:
;指定进程名称[program:beepkg]directory=/opt/goapp/beepkgcommand=/opt/goapp/beepkgautostart=trueuser=rootredirect_stderr=truestdout_logfile =/var/log/supervidrod/beepkg.log
Start Supervisor
:
Supervisord-c/etc/supervisord.conf
supervisorctl
commands can be used to enter the shell interface, management program
.
Like what:
#启动进程strat beepkg#停止进程stop beepkg#重启进程restart beepkg
Note If Supervisor
the configuration file changes, you must use it to make the configuration reload
modification of the configuration file effective.
An error was encountered during the modification of the configuration file
* Starting Supervisor daemon manager... Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord. For help, use /usr/bin/supervisord -h ...fail!
Workaround:
find / -name supervisor.sockunlink /tmp/supervisor.sock
##** Final Script * *
To unzip the file and restart the service script restart.sh
:
#! /bin/bash#默认进入的是登录用户的目录cd test/beetar -xzvf beepkg.tar.gz#remove conf of devrm -rf conf/app.confcp conf/app.conf.bat conf/app.confsupervisorctl restart beepkg
Local development Environment Deployment script deploy.sh
:
#! /bin/bashecho 'update code'git reset HEAD --hardgit pull origin masterecho 'pack'bee pack -be GOOS=linux -exr='^[0-9a-f]|[*.iml]$'echo 'upload'scp beepkg.tar.gz jjz@192.168.1.10:/root/goapp/beepkgecho 'restart'ssh jjz@192.168.1.10 'bash -s' < restart.sh