2014-02-07 10:10:01 +00:00
|
|
|
package deploy
|
2014-02-23 13:19:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2014-09-08 06:08:35 +00:00
|
|
|
"github.com/drone/drone/plugin/condition"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/shared/build/buildfile"
|
2014-02-23 13:19:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SSH struct holds configuration data for deployment
|
|
|
|
// via ssh, deployment done by scp-ing file(s) listed
|
|
|
|
// in artifacts to the target host, and then run cmd
|
|
|
|
// remotely.
|
|
|
|
// It is assumed that the target host already
|
|
|
|
// add this repo public key in the host's `authorized_hosts`
|
|
|
|
// file. And the private key is already copied to `.ssh/id_rsa`
|
|
|
|
// inside the build container. No further check will be done.
|
|
|
|
type SSH struct {
|
|
|
|
|
|
|
|
// Target is the deployment host in this format
|
|
|
|
// user@hostname:/full/path <PORT>
|
|
|
|
//
|
|
|
|
// PORT may be omitted if its default to port 22.
|
|
|
|
Target string `yaml:"target,omitempty"`
|
|
|
|
|
|
|
|
// Artifacts is a list of files/dirs to be deployed
|
|
|
|
// to the target host. If artifacts list more than one file
|
|
|
|
// it will be compressed into a single tar.gz file.
|
|
|
|
// if artifacts contain:
|
|
|
|
// - GITARCHIVE
|
|
|
|
//
|
|
|
|
// other file listed in artifacts will be ignored, instead, we will
|
|
|
|
// create git archive from the current revision and deploy that file
|
|
|
|
// alone.
|
|
|
|
// If you need to deploy the git archive along with some other files,
|
|
|
|
// please use build script to create the git archive, and then list
|
|
|
|
// the archive name here with the other files.
|
|
|
|
Artifacts []string `yaml:"artifacts,omitempty"`
|
|
|
|
|
|
|
|
// Cmd is a single command executed at target host after the artifacts
|
|
|
|
// is deployed.
|
|
|
|
Cmd string `yaml:"cmd,omitempty"`
|
2014-09-08 06:08:35 +00:00
|
|
|
|
|
|
|
Condition *condition.Condition `yaml:"when,omitempty"`
|
2014-02-23 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write down the buildfile
|
|
|
|
func (s *SSH) Write(f *buildfile.Buildfile) {
|
|
|
|
host := strings.SplitN(s.Target, " ", 2)
|
|
|
|
if len(host) == 1 {
|
|
|
|
host = append(host, "22")
|
|
|
|
}
|
|
|
|
if _, err := strconv.Atoi(host[1]); err != nil {
|
|
|
|
host[1] = "22"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is artifact created?
|
|
|
|
artifact := false
|
|
|
|
|
|
|
|
for _, a := range s.Artifacts {
|
|
|
|
if a == "GITARCHIVE" {
|
|
|
|
artifact = createGitArchive(f)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 06:49:10 +00:00
|
|
|
if !artifact {
|
|
|
|
if len(s.Artifacts) > 1 {
|
|
|
|
artifact = compress(f, s.Artifacts)
|
|
|
|
} else if len(s.Artifacts) == 1 {
|
|
|
|
f.WriteEnv("ARTIFACT", s.Artifacts[0])
|
|
|
|
artifact = true
|
|
|
|
}
|
2014-02-23 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if artifact {
|
2014-05-06 20:25:42 +00:00
|
|
|
scpCmd := "scp -o StrictHostKeyChecking=no -P %s -r ${ARTIFACT} %s"
|
2014-02-23 13:19:00 +00:00
|
|
|
f.WriteCmd(fmt.Sprintf(scpCmd, host[1], host[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.Cmd) > 0 {
|
2014-10-24 14:19:13 +00:00
|
|
|
sshCmd := "ssh -o StrictHostKeyChecking=no -p %s %s \"%s\""
|
2014-02-23 13:19:00 +00:00
|
|
|
f.WriteCmd(fmt.Sprintf(sshCmd, host[1], strings.SplitN(host[0], ":", 2)[0], s.Cmd))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createGitArchive(f *buildfile.Buildfile) bool {
|
2014-03-11 06:49:10 +00:00
|
|
|
f.WriteEnv("COMMIT", "$(git rev-parse HEAD)")
|
|
|
|
f.WriteEnv("ARTIFACT", "${PWD##*/}-${COMMIT}.tar.gz")
|
2014-02-23 13:19:00 +00:00
|
|
|
f.WriteCmdSilent("git archive --format=tar.gz --prefix=${PWD##*/}/ ${COMMIT} > ${ARTIFACT}")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func compress(f *buildfile.Buildfile, files []string) bool {
|
|
|
|
cmd := "tar -cf ${ARTIFACT} %s"
|
2014-03-11 06:49:10 +00:00
|
|
|
f.WriteEnv("ARTIFACT", "${PWD##*/}.tar.gz")
|
2014-02-23 13:19:00 +00:00
|
|
|
f.WriteCmdSilent(fmt.Sprintf(cmd, strings.Join(files, " ")))
|
|
|
|
return true
|
|
|
|
}
|
2014-09-08 06:08:35 +00:00
|
|
|
|
|
|
|
func (s *SSH) GetCondition() *condition.Condition {
|
|
|
|
return s.Condition
|
|
|
|
}
|