2015-05-15 05:12:50 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/drone/drone-plugin-go/plugin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Docker struct {
|
2015-09-04 01:17:51 +00:00
|
|
|
|
Storage string `json:"storage_driver"`
|
|
|
|
|
Registry string `json:"registry"`
|
|
|
|
|
Insecure bool `json:"insecure"`
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
Password string `json:"password"`
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
Auth string `json:"auth"`
|
|
|
|
|
Repo string `json:"repo"`
|
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
|
File string `json:"file"`
|
|
|
|
|
Dns []string `json:"dns"`
|
2015-05-15 05:12:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2015-09-02 22:30:42 +00:00
|
|
|
|
workspace := plugin.Workspace{}
|
|
|
|
|
build := plugin.Build{}
|
2015-05-15 05:12:50 +00:00
|
|
|
|
vargs := Docker{}
|
|
|
|
|
|
2015-09-02 22:30:42 +00:00
|
|
|
|
plugin.Param("workspace", &workspace)
|
|
|
|
|
plugin.Param("build", &build)
|
2015-05-15 05:12:50 +00:00
|
|
|
|
plugin.Param("vargs", &vargs)
|
2015-09-02 22:30:42 +00:00
|
|
|
|
plugin.MustParse()
|
2015-05-15 05:12:50 +00:00
|
|
|
|
|
2015-05-15 06:58:43 +00:00
|
|
|
|
// Set the storage driver
|
|
|
|
|
if len(vargs.Storage) == 0 {
|
2015-05-15 07:14:39 +00:00
|
|
|
|
vargs.Storage = "aufs"
|
2015-05-15 06:58:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-15 06:20:26 +00:00
|
|
|
|
stop := func() {
|
|
|
|
|
cmd := exec.Command("start-stop-daemon", "--stop", "--pidfile", "/var/run/docker.pid")
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
trace(cmd)
|
|
|
|
|
cmd.Run()
|
|
|
|
|
}
|
|
|
|
|
defer stop()
|
|
|
|
|
|
2015-05-15 05:12:50 +00:00
|
|
|
|
// Starts the Docker daemon
|
|
|
|
|
go func() {
|
2015-05-15 06:20:26 +00:00
|
|
|
|
|
2015-09-04 07:28:10 +00:00
|
|
|
|
args := []string{"/usr/bin/docker", "-d", "-s", vargs.Storage}
|
2015-09-03 05:14:57 +00:00
|
|
|
|
|
|
|
|
|
if vargs.Insecure && len(vargs.Registry) != 0 {
|
|
|
|
|
args = append(args, "--insecure-registry", vargs.Registry)
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 01:17:51 +00:00
|
|
|
|
for _, value := range vargs.Dns {
|
|
|
|
|
args = append(args, "--dns", value)
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 07:28:10 +00:00
|
|
|
|
cmd := exec.Command("/usr/bin/dockerlaunch", args...)
|
|
|
|
|
if os.Getenv("DOCKER_LAUNCH_DEBUG") == "true" {
|
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
} else {
|
|
|
|
|
cmd.Stdout = ioutil.Discard
|
|
|
|
|
cmd.Stderr = ioutil.Discard
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-15 06:20:26 +00:00
|
|
|
|
trace(cmd)
|
2015-05-15 05:12:50 +00:00
|
|
|
|
cmd.Run()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Sleep for a few seconds
|
2015-09-04 07:28:10 +00:00
|
|
|
|
time.Sleep(35 * time.Second)
|
2015-05-15 05:12:50 +00:00
|
|
|
|
|
|
|
|
|
// Set the Registry value
|
|
|
|
|
if len(vargs.Registry) == 0 {
|
|
|
|
|
vargs.Registry = "https://index.docker.io/v1/"
|
2015-09-04 01:17:51 +00:00
|
|
|
|
} else {
|
|
|
|
|
vargs.Repo = fmt.Sprintf("%s/%s", vargs.Registry, vargs.Repo)
|
2015-05-15 05:12:50 +00:00
|
|
|
|
}
|
|
|
|
|
// Set the Dockerfile path
|
|
|
|
|
if len(vargs.File) == 0 {
|
|
|
|
|
vargs.File = "."
|
|
|
|
|
}
|
|
|
|
|
// Set the Tag value
|
2015-09-03 05:14:57 +00:00
|
|
|
|
if len(vargs.Tag) == 0 {
|
2015-05-15 05:12:50 +00:00
|
|
|
|
vargs.Tag = "latest"
|
|
|
|
|
}
|
|
|
|
|
vargs.Repo = fmt.Sprintf("%s:%s", vargs.Repo, vargs.Tag)
|
|
|
|
|
|
2015-05-15 16:56:27 +00:00
|
|
|
|
// Login to Docker
|
2015-09-03 05:14:57 +00:00
|
|
|
|
if len(vargs.Username) != 0 {
|
|
|
|
|
cmd := exec.Command("docker", "login", "-u", vargs.Username, "-p", vargs.Password, "-e", vargs.Email, vargs.Registry)
|
|
|
|
|
cmd.Dir = workspace.Path
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
stop()
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Printf("A username was not specified. Assuming anoynmous publishing.\n")
|
2015-05-15 05:12:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 00:18:07 +00:00
|
|
|
|
// Docker environment info
|
2015-09-03 05:14:57 +00:00
|
|
|
|
cmd := exec.Command("docker", "version")
|
2015-05-16 00:18:07 +00:00
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
trace(cmd)
|
|
|
|
|
cmd.Run()
|
|
|
|
|
cmd = exec.Command("docker", "info")
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
trace(cmd)
|
|
|
|
|
cmd.Run()
|
|
|
|
|
|
2015-05-15 16:56:27 +00:00
|
|
|
|
// Build the container
|
|
|
|
|
cmd = exec.Command("docker", "build", "--pull=true", "--rm=true", "-t", vargs.Repo, vargs.File)
|
2015-09-02 22:30:42 +00:00
|
|
|
|
cmd.Dir = workspace.Path
|
2015-05-15 05:12:50 +00:00
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
2015-05-15 16:56:27 +00:00
|
|
|
|
trace(cmd)
|
2015-09-03 05:14:57 +00:00
|
|
|
|
err := cmd.Run()
|
2015-05-15 05:12:50 +00:00
|
|
|
|
if err != nil {
|
2015-05-15 06:20:26 +00:00
|
|
|
|
stop()
|
2015-05-15 05:12:50 +00:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push the container
|
|
|
|
|
cmd = exec.Command("docker", "push", vargs.Repo)
|
2015-09-02 22:30:42 +00:00
|
|
|
|
cmd.Dir = workspace.Path
|
2015-05-15 05:12:50 +00:00
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
trace(cmd)
|
|
|
|
|
err = cmd.Run()
|
|
|
|
|
if err != nil {
|
2015-05-15 06:20:26 +00:00
|
|
|
|
stop()
|
2015-05-15 05:12:50 +00:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Trace writes each command to standard error (preceded by a ‘$ ’) before it
|
|
|
|
|
// is executed. Used for debugging your build.
|
|
|
|
|
func trace(cmd *exec.Cmd) {
|
|
|
|
|
fmt.Println("$", strings.Join(cmd.Args, " "))
|
|
|
|
|
}
|
2015-05-16 00:18:07 +00:00
|
|
|
|
|
|
|
|
|
// authorize is a helper function that authorizes the Docker client
|
|
|
|
|
// by manually creating the Docker authentication file.
|
|
|
|
|
func authorize(d *Docker) error {
|
|
|
|
|
var path = "/root/.dockercfg" // TODO should probably use user.Home() for good measure
|
|
|
|
|
var data = fmt.Sprintf(dockerconf, d.Registry, d.Auth, d.Email)
|
|
|
|
|
return ioutil.WriteFile(path, []byte(data), 0644)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dockerconf = `
|
|
|
|
|
{
|
|
|
|
|
"%s": {
|
|
|
|
|
"auth": "%s",
|
|
|
|
|
"email": "%s"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`
|