Skip to content

Automatically pull generator and dashboard before using #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cli/commands/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (cmd *Dashboard) Commands() []cli.Command {
}
}

func (cmd *Dashboard) Run(c *cli.Context) error {
func (cmd *Dashboard) Run(ctx *cli.Context) error {
if cmd.machine.IsRunning() {
cmd.out.Info.Println("Launching Dashboard")
cmd.LaunchDashboard(cmd.machine)
Expand All @@ -41,8 +41,20 @@ func (cmd *Dashboard) LaunchDashboard(machine Machine) {
exec.Command("docker", "stop", "outrigger-dashboard").Run()
exec.Command("docker", "rm", "outrigger-dashboard").Run()

dockerApiVersion, _ := util.GetDockerServerApiVersion(cmd.machine.Name)
image := "outrigger/dashboard:latest"

// The check for whether the image is older than 30 days is not currently used.
_, seconds, err := util.ImageOlderThan(image, 86400*30)
if err == nil {
cmd.out.Verbose.Printf("Local copy of the image '%s' was originally published %0.2f days ago.", image, seconds/86400)
}

cmd.out.Verbose.Printf("Attempting to update %s", image)
if err := util.StreamCommand(exec.Command("docker", "pull", image)); err != nil {
cmd.out.Verbose.Println("Failed to update dashboard image. Will use local cache if available.")
}

dockerApiVersion, _ := util.GetDockerServerApiVersion(cmd.machine.Name)
args := []string{
"run",
"-d",
Expand Down
55 changes: 45 additions & 10 deletions cli/commands/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ func (cmd *ProjectCreate) Commands() []cli.Command {
Description: "The type is the generator to run with args passed to that generator. If using flag arguments use -- before specifying type and arguments.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "image",
Usage: "Docker image to use if default outrigger/generator is not desired",
Name: "image",
Usage: "Docker image to use if default outrigger/generator is not desired.",
EnvVar: "RIG_PROJECT_CREATE_IMAGE",
},
cli.BoolFlag{
Name: "no-update",
Usage: "Prevent automatic update of designated generator docker image.",
EnvVar: "RIG_PROJECT_CREATE_NO_UPDATE",
},
},
Before: cmd.Before,
Expand All @@ -41,20 +47,49 @@ func (cmd *ProjectCreate) Create(ctx *cli.Context) error {
image = "outrigger/generator"
}

argsMessage := " with no arguments"
if ctx.Args().Present() {
argsMessage = fmt.Sprintf(" with arguments: %s", strings.Join(ctx.Args(), " "))
}

if cmd.machine.IsRunning() {
cmd.out.Verbose.Printf("Executing container %s%s", image, argsMessage)
cmd.RunGenerator(ctx, cmd.machine, image)
} else {
cmd.out.Error.Fatalf("Machine '%s' is not running.", cmd.machine.Name)
}

return nil
}

func (cmd *ProjectCreate) RunGenerator(ctx *cli.Context, machine Machine, image string) error {
machine.SetEnv()

// The check for whether the image is older than 30 days is not currently used.
_, seconds, err := util.ImageOlderThan(image, 86400*30)
if err == nil {
cmd.out.Verbose.Printf("Local copy of the image '%s' was originally published %0.2f days ago.", image, seconds/86400)
}

// If there was an error it implies no previous instance of the image is available
// or that docker operations failed and things will likely go wrong anyway.
if err == nil && !ctx.Bool("no-update") {
cmd.out.Verbose.Printf("Attempting to update %s", image)
if err := util.StreamCommand(exec.Command("docker", "pull", image)); err != nil {
cmd.out.Verbose.Println("Failed to update generator image. Will use local cache if available.")
}
} else if err == nil && ctx.Bool("no-update") {
cmd.out.Verbose.Printf("Automatic generator image update suppressed by --no-update option.")
}

cwd, err := os.Getwd()
if err != nil {
cmd.out.Error.Printf("Couldn't determine current working directory: %s", err)
os.Exit(1)
}

argsMessage := " with no arguments"
if ctx.Args().Present() {
argsMessage = fmt.Sprintf(" with arguments: %s", strings.Join(ctx.Args(), " "))
}
cmd.out.Verbose.Printf("Executing container %s%s", image, argsMessage)

// keep passed in args as distinct elements or they will be treated as
// a single argument containing spaces when the container gets them
// Keep passed in args as distinct elements or they will be treated as
// a single argument containing spaces when the container gets them.
args := []string{
"container",
"run",
Expand Down
23 changes: 23 additions & 0 deletions cli/util/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package util

import (
"os/exec"
"strings"
"time"
)

func ImageOlderThan(image string, elapsed_seconds float64) (bool, float64, error) {
output, err := exec.Command("docker", "inspect", "--format", "{{.Created}}", image).Output()
if err != nil {
return false, 0, err
}

datestring := strings.TrimSpace(string(output))
datetime, err := time.Parse(time.RFC3339, datestring)
if err != nil {
return false, 0, err
}

seconds := time.Since(datetime).Seconds()
return seconds > elapsed_seconds, seconds, nil
}