go>> harp>> 返回
项目作者: bom-d-van

项目描述 :
A Go application deployment tool.
高级语言: Go
项目地址: git://github.com/bom-d-van/harp.git
创建时间: 2015-01-19T01:29:19Z
项目社区:https://github.com/bom-d-van/harp

开源协议:MIT License

下载


harp

Harp mades Go application deployment simpler. The strategy is binary deployment. It manages logs, restart, kill, rollback, and migrations of Go applications. Harp is made to run on local environment for small to middle-sized projects. Run harp on a dedicated build server is also viable.

What harp does

Harp simply builds your application and upload it to your server. It brings you a complete solution for deploying common applications. It syncs, restarts, kills, and deploys your applications.

The best way to learn what harp does and helps is to use it. (In test directory, there are docker files and harp configurations you can play with)

System requirements

Local: Harps works on Mac OS X, Linux, Windows isn’t tested.

Server: Harps works on Linux servers.

Third-party requirements: tar, rsync on both server and local.

Server access using SSH

Harp is using passwordless login with ssh-agent to access your servers. You can find some help from here:

http://www.linuxproblem.org/art_9.html

You can add your key in ssh-agent by:

  1. ssh-add ~/.ssh/id_rsa # or other path to your private key
  2. ssh-add -l

Examples

  1. # Init harp.json
  2. harp init
  3. # Configure your servers and apps in harp.json. see section Configuration below.
  4. harp -s dev deploy
  5. # Or
  6. harp -s prod deploy
  7. # Restart server
  8. harp -s prod restart
  9. # Shut down server
  10. harp -s prod kill
  11. # Inspect server build info
  12. harp -s prod info
  13. # Rollback release
  14. harp -s prod rollback $version-tag
  15. # Tail server logs
  16. harp -s prod log
  17. # Specify config files
  18. harp -s prod -c config/harp.json deploy
  19. # Upload builds/migrations without executing it immediately
  20. harp -s prod -no-run deploy
  21. # Or shorter
  22. harp -s prod -nr deploy
  23. # Skip builds
  24. harp -s prod -no-build deploy
  25. # Skip file uploads
  26. harp -s prod -no-files deploy
  27. # More flags and usages
  28. harp -h
  29. # Run arbitrary Go program wihtout harp.json
  30. harp -server app@test.com:8022 -t run app.go # with default -goos=linux -goarch=amd64
  31. # Run shell commands or execute scripts with harp console (alias: sh, shell)
  32. harp -s prod console
  33. harp -s prod console < my-shell-script
  34. harp -s prod console <<<(ls)

Common Trouble Shootings

Too many open files

To fix this problem, you need to increase ulimit:

  1. ulimit -n 10240 // or any number that is large enough

The .harp directory

Harp creates a temporary directory called .harp in the current path where it is invoked. It will be removed after harp exits. Under rare circumstances, you can use harp clean to remove the directory manually. Also, it’s better include .harp in .gitignore or similar counterpart of your VCS tool.

Configuration

example:

  1. {
  2. // comments are supported in harp.json
  3. "GOOS": "linux", // for go build
  4. "GOARCH": "amd64", // for go build
  5. "App": {
  6. "Name": "app",
  7. "ImportPath": "github.com/bom-d-van/harp/test",
  8. // will be applied to all servers
  9. "Envs": {
  10. "var1": "value"
  11. },
  12. // these are included in all file Excludeds
  13. "DefaultExcludeds": [".git/", "tmp/", ".DS_Store", "node_modules/", "*.go"],
  14. "Files": [
  15. // files here could be a string or an object
  16. "github.com/bom-d-van/harp/test/files",
  17. {
  18. "Path": "github.com/bom-d-van/harp/test/file",
  19. "Excludeds": ["builds"]
  20. },
  21. {
  22. "Path": "github.com/bom-d-van/harp/test/file2",
  23. // These option will enable rsync --delete during deploy
  24. // You can check the effect with `harp inspect deploy`
  25. // Need to be careful with this option.
  26. //
  27. // Because it may cause some wanted results, so it's
  28. // disabled by default
  29. "Delete": true
  30. }
  31. ]
  32. },
  33. "Servers": {
  34. "prod": [{
  35. "ID": "pluto", // ID field could be used to specify server with `-server` flag
  36. "User": "app",
  37. // server specific env vars
  38. "Envs": {
  39. "var2": "value2"
  40. },
  41. "Host": "192.168.59.103",
  42. "Port": ":49155"
  43. }, {
  44. "User": "app",
  45. "Host": "192.168.59.104",
  46. "Port": ":49156"
  47. }],
  48. "dev": [{
  49. "User": "app",
  50. "Host": "192.168.59.102",
  51. "Port": ":49155"
  52. }]
  53. }
  54. }

Vendor Support

harp doesn’t have built-in vendor support. To upload vendor files, you could still use its import path releative to your $GOPATH. e.g.:

  1. "Files": [
  2. "github.com/org/repo/vendor/my/pkg",
  3. ...
  4. ]

Usages

How to specify server or server sets:

Using the configuration above as example, server set means the key in Servers object value, i.e. prod, dev.
While server is elemnt in server set arrays, you can specify it by {User}@{Host}{Port}.

  1. # deploy prod servers
  2. harp -s prod deploy
  3. # deploy dev servers
  4. harp -s dev deploy
  5. # deploy only one prod server:
  6. harp -server app@192.168.59.102:49155 deploy

Migration / Run a Go package/file on remote server

You can specify server or server sets on which your migration need to be executed.

Simple:

  1. harp -server app@192.168.59.103:49153 run migration.go

With env and arguments:

  1. harp -server app@192.168.59.103:49153 run "AppEnv=prod migration2.go -arg1 val1 -arg2 val2"

Multiple migrations:

  1. harp -server app@192.168.59.103:49153 run migration.go "AppEnv=prod migration2.go -arg1 val1 -arg2 val2"

Note: Harp saved the current migration files in $HOME/harp/{{.App.Name}}/migrations.tar.gz. You can uncompress it and execute the binary manually if you prefer or on special occasions.

Rollback

By default harp will save three most recent releases in $HOME/harp/{{.App.Name}}/releases directory. The current release is the newest release in the releases list.

  1. # list all releases
  2. harp -s prod rollback ls
  3. # rollback
  4. harp -s prod rollback 15-06-14-11:29:14

And there is also a rollback.sh script in $HOME/harp/{{.App.Name}} that you can use to rollback release.

You can change how many releases you want to keep by RollbackCount or disable rollback by NoRollback in harp file.

  1. {
  2. "GOOS": "linux", // for go build
  3. "GOARCH": "amd64", // for go build
  4. "NoRollback": true,
  5. "RollbackCount": 10,
  6. "App": {
  7. ...
  8. },
  9. ...
  10. }

Note: rollback depends on harp.json, if Files or other configs are changed, rollback might not work.

Build Args Specification

Harp supports go build tool arguments specification.

  1. "App": {
  2. "Name": "app",
  3. "BuildArgs": "-tags daemon"
  4. }

You can also override or ad-hoc specify build args from command line as follows:

  1. harp -s prod -build-args '-tags client' deploy

Note: currently migration using build args from cli are not supported yet.

Build Override

Harp allows you to override default build command.

Add BuildCmd option in App as bellow:

  1. "App": {
  2. "Name": "app",
  3. "BuildCmd": "docker run -t -v $GOPATH:/home/app golang /bin/sh -c 'cd /home/app/src/github.com/bom-d-van/harp; GOPATH=/home/app /usr/local/go/bin/go build -o /home/app/src/github.com/bom-d-van/harp/%s %s'"
  4. }

The first %s represents the harp temporary output, and the second %s represents a import path or a file path for some migrations. An example output in test/harp2.json is:

  1. # for normal builds
  2. docker run -t -v $GOPATH:/home/app golang /bin/sh -c 'GOPATH=/home/app /usr/local/go/bin/go build -o \$GOPATH/src/github.com/bom-d-van/harp/.harp/app github.com/bom-d-van/harp/test'
  3. # for migrations/runs
  4. docker run -t -v $GOPATH:/home/app golang /bin/sh -c 'GOPATH=/home/app /usr/local/go/bin/go build -o \$GOPATH/src/github.com/bom-d-van/harp/.harp/migrations/migration3.go github.com/bom-d-van/harp/test/migration3'

NOTE: Build override doesn’t support non-package migrations. i.e. every migration under build override has to be a legal Go package.

Build override is useful doing cross compilation for cgo-involved projects, e.g. using Mac OS X building Linux binaries by docker or any other tools etc.

Note: Harps is saving temporary build output and files in $(pwd)/.harp. Therefore harp expects build output appears in directory $(pwd)/.harp/{{app name}} where you evoke harp command (i.e. pwd). And $(pwd)/.harp/migrations/{{migration name}} for migrations.

Script Override

harp supports you to override its default deploy script. Add configuration like bellow:

  1. "App": {
  2. "Name": "app",
  3. "DeployScript": "path-to-your-script-template",
  4. "RestartScript": "path-to-your-script-template",
  5. "MigrationScript": "path-to-your-script-template"
  6. },

Deploy and Restart Script

The script could be a text/template.Template, into which harp pass a data as bellow:

  1. map[string]interface{}{
  2. "App": harp.App,
  3. "Server": harp.Server,
  4. "SyncFiles": syncFilesScript,
  5. "RestartServer": restartScript,
  6. }
  7. type App struct {
  8. Name string
  9. ImportPath string
  10. NoRelMatch bool
  11. DefaultExcludeds []string
  12. Files []File
  13. Args []string
  14. Envs map[string]string
  15. BuildCmd string
  16. BuildArgs string
  17. KillSig string
  18. // Default: 1MB
  19. FileWarningSize int64
  20. DeployScript string
  21. RestartScript string
  22. }
  23. type Server struct {
  24. ID string
  25. Envs map[string]string
  26. Home string
  27. GoPath string
  28. LogDir string
  29. User string
  30. Host string
  31. Port string
  32. Set string // aka, Type
  33. client *ssh.Client
  34. Config *Config
  35. // Proxy is for ssh bastion host/ProxyCommand/middleman
  36. Proxy *Server
  37. }
  38. func (Server) AppRoot() string

A default deploy script is:

  1. set -e
  2. {{.SyncFiles}}
  3. {{.SaveRelease}}
  4. {{.RestartServer}}

Similarly, restart script could be override too. And its default template is:

  1. set -e
  2. {{.RestartServer}}

You can inspect your script by evoking command: harp -s prod inspect deploy or harp -s prod inspect restart.

Migration Script

Migration script template data is:

  1. map[string]interface{}{
  2. "Server": harp.Server,
  3. "App": harp.App,
  4. "DefaultScript": string,
  5. }

The default migration template contains only the default script. So for now you can only rewrite migration in this fashion:

  1. set -e
  2. # custom scripts
  3. {{.DefaultScript}}
  4. # custom scripts

Server Informations

You can use harp info to retrieve build and deploy information about the current running server. For example:

  1. harp info
  2. ===== app@192.168.59.103:49153
  3. Go Version: go version go1.4.2 darwin/amd64
  4. GOOS: linux
  5. GOARCH: amd64
  6. Git Checksum: f8eb715f33c36d8ec018fe116491a01540106fc8
  7. Composer: bom_d_van
  8. Build At: 2015-07-06 21:28:55.359181899 +0800 CST

Note: Composer means deployer.

You can specify your composer name by saving your name in a file named .harp-composer.

Scripts saved on servers

Harp saves a few scripts on yoru servers after deploy. It could be found in $HOME/harp/$APP_Name/.

These scripts could be used as Monitor integration:

  • kill.sh: kill the application;
  • restart.sh: restart the application;
  • rollback.sh: rollback the application: need to specify version (directory names in releases folder).

Initialize Go cross compilation (For Go <= 1.5)

If you need to initialize cross compilation environment, harp has a simple commend to help you:

  1. harp xc

Note: before using harp xc, you need to enable cross compilation by installing go from source, details could be found here: https://golang.org/doc/install/source

Console (sh, shell)

Run shell commands or execute scripts with harp console (alias: sh, shell)

  1. # start a repl to execute shell commands
  2. harp -s prod console
  3. # run a script
  4. harp -s prod console < my-shell-script
  5. # another example
  6. harp -s prod console <<<(ls)

Note: harp console doesn’t support blocking calls like tail -f file.log.

Deployment history

harp saves a one-line deployment/restart/kill/rollback log in ~/harp/$APP_Name/log/history.log.