|
|
|
@ -15,6 +15,84 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DeploymentStatus int
|
|
|
|
|
|
|
|
|
|
// Note: Entries must only be appended to this list.
|
|
|
|
|
const (
|
|
|
|
|
StatusUnknown DeploymentStatus = iota
|
|
|
|
|
StatusAttach
|
|
|
|
|
StatusCommit
|
|
|
|
|
StatusCopy
|
|
|
|
|
StatusCreate
|
|
|
|
|
StatusDestroy
|
|
|
|
|
StatusDetach
|
|
|
|
|
StatusDie
|
|
|
|
|
StatusExecCreate
|
|
|
|
|
StatusExecDetach
|
|
|
|
|
StatusExecDie
|
|
|
|
|
StatusExecStart
|
|
|
|
|
StatusExport
|
|
|
|
|
StatusHealthStatus
|
|
|
|
|
StatusKill
|
|
|
|
|
StatusOOM
|
|
|
|
|
StatusPause
|
|
|
|
|
StatusRename
|
|
|
|
|
StatusResize
|
|
|
|
|
StatusRestart
|
|
|
|
|
StatusStart
|
|
|
|
|
StatusStop
|
|
|
|
|
StatusTop
|
|
|
|
|
StatusUnpause
|
|
|
|
|
StatusUpdate
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var DeploymentStatusLabels = map[string]DeploymentStatus{
|
|
|
|
|
"attach": StatusAttach,
|
|
|
|
|
"commit": StatusCommit,
|
|
|
|
|
"copy": StatusCopy,
|
|
|
|
|
"create": StatusCreate,
|
|
|
|
|
"destroy": StatusDestroy,
|
|
|
|
|
"detach": StatusDetach,
|
|
|
|
|
"die": StatusDie,
|
|
|
|
|
"exec_create": StatusExecCreate,
|
|
|
|
|
"exec_detach": StatusExecDetach,
|
|
|
|
|
"exec_die": StatusExecDie,
|
|
|
|
|
"exec_start": StatusExecStart,
|
|
|
|
|
"export": StatusExport,
|
|
|
|
|
"health_status": StatusHealthStatus,
|
|
|
|
|
"kill": StatusKill,
|
|
|
|
|
"oom": StatusOOM,
|
|
|
|
|
"pause": StatusPause,
|
|
|
|
|
"rename": StatusRename,
|
|
|
|
|
"resize": StatusResize,
|
|
|
|
|
"restart": StatusRestart,
|
|
|
|
|
"start": StatusStart,
|
|
|
|
|
"stop": StatusStop,
|
|
|
|
|
"top": StatusTop,
|
|
|
|
|
"unpause": StatusUnpause,
|
|
|
|
|
"update": StatusUpdate,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var RecordedDeploymentStatuses = []DeploymentStatus{
|
|
|
|
|
StatusCreate,
|
|
|
|
|
StatusDestroy,
|
|
|
|
|
StatusDie,
|
|
|
|
|
StatusKill,
|
|
|
|
|
StatusOOM,
|
|
|
|
|
StatusRestart,
|
|
|
|
|
StatusStart,
|
|
|
|
|
StatusStop,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParseDeploymentStatus(status string) DeploymentStatus {
|
|
|
|
|
return DeploymentStatusLabels[status]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeploymentEvent struct {
|
|
|
|
|
Time int64
|
|
|
|
|
Status DeploymentStatus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Deployment struct {
|
|
|
|
|
ID int
|
|
|
|
|
|
|
|
|
@ -30,6 +108,8 @@ type Deployment struct {
|
|
|
|
|
Ports []int
|
|
|
|
|
|
|
|
|
|
Worker *Worker
|
|
|
|
|
|
|
|
|
|
Events []DeploymentEvent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var replacementPort = regexp.MustCompile(`(HOSTALGIA_PORT_[A-Z])`)
|
|
|
|
@ -117,6 +197,19 @@ func (d *Deployment) deploy() error {
|
|
|
|
|
}
|
|
|
|
|
} else if !fileInfo.IsDir() {
|
|
|
|
|
return fmt.Errorf("invalid output directory: %s", d.Dir())
|
|
|
|
|
} else {
|
|
|
|
|
_, err = os.Stat(path.Join(d.Dir(), "docker-compose.yml"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
|
log.Fatalf("failed to check for existing docker-compose.yml: %s", err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("Stopping deployment %s...", d.Label())
|
|
|
|
|
_, _, err = DockerCompose(d.Dir(), []string{"stop"})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("failed to stop running deployment: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = d.interpolateAndCopy(path.Join(festoonPath, "docker-compose.yml"), path.Join(d.Dir(), "docker-compose.yml"))
|
|
|
|
@ -158,17 +251,17 @@ func (d *Deployment) deploy() error {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stdOut, stdErr, err := DockerCompose(d.Dir(), []string{"up", "-d"})
|
|
|
|
|
log.Printf("Starting deployment %s...", d.Label())
|
|
|
|
|
_, stdErr, err := DockerCompose(d.Dir(), []string{"up", "-d"})
|
|
|
|
|
if bytes.Contains(stdErr, []byte(fmt.Sprintf("%s is up-to-date", d.Label()))) {
|
|
|
|
|
log.Printf("Warning: %s was already up", d.Label())
|
|
|
|
|
d.Events = append(d.Events, DeploymentEvent{
|
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
|
Status: StatusAttach,
|
|
|
|
|
})
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to bring deployment up: %s", err)
|
|
|
|
|
}
|
|
|
|
|
log.Printf("docker compose stdOut: %s", stdOut)
|
|
|
|
|
log.Printf("docker compose stdErr: %s", stdErr)
|
|
|
|
|
|
|
|
|
|
log.Println("deployment UP!")
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -212,7 +305,31 @@ func (d *Deployment) handleEvents() {
|
|
|
|
|
if l == 10 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
log.Println("Container status", string(b[5:l-5]))
|
|
|
|
|
|
|
|
|
|
statusString := string(b[5 : l-5])
|
|
|
|
|
|
|
|
|
|
status := ParseDeploymentStatus(statusString)
|
|
|
|
|
if status == StatusUnknown {
|
|
|
|
|
log.Printf("Warning: Deployment %s has unknown status %s", d.Label(), statusString)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var recordEvent bool
|
|
|
|
|
for _, recordStatus := range RecordedDeploymentStatuses {
|
|
|
|
|
if status == recordStatus {
|
|
|
|
|
recordEvent = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !recordEvent {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event := DeploymentEvent{
|
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
|
Status: status,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.Events = append(d.Events, event)
|
|
|
|
|
}
|
|
|
|
|
if scanner.Err() != nil {
|
|
|
|
|
log.Fatal("scanner error", scanner.Err())
|
|
|
|
|