2015-05-13 01:32:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/samalba/dockerclient"
|
2015-05-17 20:51:42 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
2015-05-13 01:32:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
clone = flag.Bool("clone", false, "")
|
|
|
|
build = flag.Bool("build", false, "")
|
|
|
|
publish = flag.Bool("publish", false, "")
|
|
|
|
deploy = flag.Bool("deploy", false, "")
|
|
|
|
notify = flag.Bool("notify", false, "")
|
2015-05-16 02:35:33 +00:00
|
|
|
debug = flag.Bool("debug", false, "")
|
2015-05-13 01:32:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-05-16 02:35:33 +00:00
|
|
|
if *debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2015-05-13 01:32:46 +00:00
|
|
|
ctx, err := parseContext()
|
|
|
|
if err != nil {
|
2015-05-16 02:35:33 +00:00
|
|
|
log.Errorln("Error launching build container.", err)
|
2015-05-13 01:32:46 +00:00
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
createClone(ctx)
|
|
|
|
|
|
|
|
// creates the Docker client, connecting to the
|
|
|
|
// linked Docker daemon
|
|
|
|
docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
|
|
|
|
if err != nil {
|
2015-05-16 02:35:33 +00:00
|
|
|
log.Errorln("Error connecting to build server.", err)
|
2015-05-13 01:32:46 +00:00
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a wrapper Docker client that uses an ambassador
|
|
|
|
// container to create a pod-like environment.
|
|
|
|
client, err := newClient(docker)
|
|
|
|
if err != nil {
|
2015-05-16 02:35:33 +00:00
|
|
|
log.Errorln("Error starting build server pod", err)
|
2015-05-13 01:32:46 +00:00
|
|
|
os.Exit(1)
|
2015-06-08 00:26:25 +00:00
|
|
|
return
|
2015-05-13 01:32:46 +00:00
|
|
|
}
|
|
|
|
ctx.client = client
|
|
|
|
defer client.Destroy()
|
|
|
|
|
|
|
|
// performs some initial parsing and pre-processing steps
|
|
|
|
// prior to executing our build tasks.
|
|
|
|
err = setup(ctx)
|
|
|
|
if err != nil {
|
2015-05-16 02:35:33 +00:00
|
|
|
log.Errorln("Error processing .drone.yml file.", err)
|
2015-05-13 01:32:46 +00:00
|
|
|
client.Destroy()
|
|
|
|
os.Exit(1)
|
2015-06-08 00:26:25 +00:00
|
|
|
return
|
2015-05-13 01:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var execs []execFunc
|
|
|
|
if *clone {
|
|
|
|
execs = append(execs, execClone)
|
|
|
|
}
|
|
|
|
if *build {
|
|
|
|
execs = append(execs, execSetup)
|
|
|
|
execs = append(execs, execCompose)
|
|
|
|
execs = append(execs, execBuild)
|
|
|
|
}
|
|
|
|
if *publish {
|
|
|
|
execs = append(execs, execPublish)
|
|
|
|
}
|
|
|
|
if *deploy {
|
|
|
|
execs = append(execs, execDeploy)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through and execute each step.
|
|
|
|
for i, exec_ := range execs {
|
|
|
|
code, err := exec_(ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("00%d Error executing build\n", i+1)
|
|
|
|
fmt.Println(err)
|
|
|
|
code = 255
|
|
|
|
}
|
|
|
|
if code != 0 {
|
2015-06-19 00:36:52 +00:00
|
|
|
ctx.Job.ExitCode = code
|
2015-05-13 01:32:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally execute notification steps.
|
|
|
|
if *notify {
|
|
|
|
execNotify(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Destroy()
|
2015-06-19 00:36:52 +00:00
|
|
|
os.Exit(ctx.Job.ExitCode)
|
2015-05-13 01:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createClone(c *Context) error {
|
|
|
|
c.Clone = &common.Clone{
|
|
|
|
Netrc: c.Netrc,
|
|
|
|
Keypair: c.Keys,
|
|
|
|
Remote: c.Repo.Clone,
|
|
|
|
Origin: c.Repo.Clone,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Clone.Origin = c.Repo.Clone
|
|
|
|
c.Clone.Remote = c.Repo.Clone
|
2015-07-13 07:11:33 +00:00
|
|
|
c.Clone.Sha = c.Build.Commit.Sha
|
|
|
|
c.Clone.Ref = c.Build.Commit.Ref
|
|
|
|
c.Clone.Branch = c.Build.Commit.Branch
|
|
|
|
// TODO do we still need this? it should be set by the remote
|
2015-05-13 01:32:46 +00:00
|
|
|
if strings.HasPrefix(c.Clone.Branch, "refs/heads/") {
|
|
|
|
c.Clone.Branch = c.Clone.Branch[11:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO we should also pass the SourceSha, SourceBranch, etc
|
|
|
|
// to the clone object for merge requests from bitbucket, gitlab, et al
|
|
|
|
// if len(c.Commit.PullRequest) != 0 {
|
|
|
|
// }
|
|
|
|
|
|
|
|
url_, err := url.Parse(c.Repo.Link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Clone.Dir = filepath.Join("/drone/src", url_.Host, c.Repo.FullName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseContext() (*Context, error) {
|
|
|
|
c := &Context{}
|
|
|
|
for i, arg := range os.Args {
|
|
|
|
if arg == "--" && len(os.Args) != i+1 {
|
|
|
|
buf := bytes.NewBufferString(os.Args[i+1])
|
|
|
|
err := json.NewDecoder(buf).Decode(c)
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := json.NewDecoder(os.Stdin).Decode(c)
|
|
|
|
return c, err
|
|
|
|
}
|