Beispiel #1
0
func Stamp(sous *core.Sous, args []string) {
	target := "app"
	if len(args) == 0 {
		cli.Fatalf("sous stamp requires at least one argument (a docker label)")
	}
	_, context := sous.AssembleTargetContext(target)
	if context.BuildNumber() == 0 {
		cli.Fatalf("no builds yet; sous stamp operates on your last successful build of the app target")
	}

	tag := context.DockerTag()
	run := docker.NewRun(tag)
	run.AddLabels(parseLabels(args))
	run.StdoutFile = "/dev/null"
	run.StderrFile = "/dev/null"
	container, err := run.Background().Start()
	if err != nil {
		cli.Fatalf("Failed to start container for labeling: %s", err)
	}
	if err := container.KillIfRunning(); err != nil {
		cli.Fatalf("Failed to kill labelling container %s: %s", container, err)
	}
	cid := container.CID()
	if err := docker.Commit(cid, tag); err != nil {
		cli.Fatalf("Failed to commit labelled container %s: %s", container, err)
	}
	cli.Successf("Successfully added labels to %s; remember to push.", tag)
}
Beispiel #2
0
func Contracts(sous *core.Sous, args []string) {
	contractsFlags.Parse(args)
	args = contractsFlags.Args()
	timeout := *timeoutFlag
	targetName := "app"
	if len(args) != 0 {
		targetName = args[0]
	}
	core.RequireGit()
	core.RequireDocker()

	if *dockerImage != "" {
		cli.Fatalf("-image flag not yet implemented")
	}

	target, context := sous.AssembleTargetContext(targetName)

	sous.RunTarget(target, context)

	cli.Logf("=> Running Contracts")
	cli.Logf(`=> **TIP:** Open another terminal in this directory and type **sous logs -f**`)

	taskHost := core.DivineTaskHost()
	port0, err := ports.GetFreePort()
	if err != nil {
		cli.Fatalf("Unable to get free port: %s", err)
	}

	dr := docker.NewRun(context.DockerTag())
	dr.AddEnv("PORT0", strconv.Itoa(port0))
	dr.AddEnv("TASK_HOST", taskHost)
	dr.StdoutFile = context.FilePath("stdout")
	dr.StderrFile = context.FilePath("stderr")
	container, err := dr.Background().Start()
	if err != nil {
		cli.Fatalf("Unable to start container: %s", err)
	}
	cli.AddCleanupTask(func() error {
		return container.KillIfRunning()
	})

	failed := 0
	for _, c := range theContracts {
		cli.Logf(`===> CHECKING CONTRACT: "%s"`, c.Name)
		cli.Logf(`===> Description: %s`, c.Desc(dr))
		if c.Tips != nil {
			cli.Logf("===> **TIPS for this contract:**")
			cli.LogBulletList("     -", c.Tips(dr))
		}
		failed += within(timeout, func() bool {
			return c.Premise(dr)
		})
	}

	if failed != 0 {
		cli.Fatalf("%d contracts failed.", failed)
	}

	cli.Success()
}
Beispiel #3
0
func BuildPath(sous *core.Sous, args []string) {
	target := "app"
	if len(args) != 0 {
		target = args[0]
	}
	_, context := sous.AssembleTargetContext(target)
	fmt.Println(path.Resolve(path.BaseDir(context.BaseDir())))
	cli.Success()
}
Beispiel #4
0
func Dockerfile(sous *core.Sous, args []string) {
	targetName := "app"
	if len(args) != 0 {
		targetName = args[0]
	}
	target, context := sous.AssembleTargetContext(targetName)
	cli.Outf(sous.Dockerfile(target, context).Render())
	cli.Success()
}
Beispiel #5
0
func Clean(sous *core.Sous, args []string) {
	_, context := sous.AssembleTargetContext("app")
	cleanContainersSucceeded := cleanContainers(sous, context)
	cleanImagesSucceeded := cleanImages(sous, context)
	if cleanContainersSucceeded && cleanImagesSucceeded {
		cli.Success()
	}
	cli.Fatal()
}
Beispiel #6
0
func Image(sous *core.Sous, args []string) {
	target := "app"
	if len(args) != 0 {
		target = args[0]
	}
	_, context := sous.AssembleTargetContext(target)
	if context.BuildNumber() == 0 {
		cli.Fatalf("no builds yet")
	}
	cli.Outf(context.DockerTag())
	cli.Success()
}
Beispiel #7
0
func cleanContainers(sous *core.Sous, context *core.Context) bool {
	success := true
	for _, c := range sous.LsContainers(context) {
		if err := c.ForceRemove(); err != nil {
			cli.Logf("Failed to remove container %s (%s)", c.Name(), c.CID())
			success = false
		} else {
			cli.Logf("Removed container %s (%s)", c.Name(), c.CID())
		}
	}
	return success
}
Beispiel #8
0
func cleanImages(sous *core.Sous, context *core.Context) bool {
	success := true
	for _, i := range sous.LsImages(context) {
		if err := i.Remove(); err != nil {
			cli.Logf("Failed to remove image %s:%s", i.Name, i.Tag)
			success = false
		} else {
			cli.Logf("Removed image %s:%s", i.Name, i.Tag)
		}
	}
	return success
}
Beispiel #9
0
func Logs(sous *core.Sous, args []string) {
	logsFlags.Parse(args)
	args = logsFlags.Args()
	target := "app"
	if len(args) != 0 {
		target = args[0]
	}
	_, context := sous.AssembleTargetContext(target)

	out := makeTail(context.FilePath("stdout"), *follow, *lines, os.Stdout)
	err := makeTail(context.FilePath("stderr"), *follow, *lines, os.Stderr)

	if err := out.Start(); err != nil {
		cli.Fatalf("Unable to begin tailing %s", context.FilePath("stdout"))
	}
	if err := err.Start(); err != nil {
		cli.Fatalf("Unable to begin tailing %s", context.FilePath("stderr"))
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)

	if *follow {
		<-c
		out.Process.Signal(os.Interrupt)
		err.Process.Signal(os.Interrupt)
	}

	errs := []string{}
	if err := out.Wait(); err != nil {
		errs = append(errs, err.Error())
	}
	if err := err.Wait(); err != nil {
		errs = append(errs, err.Error())
	}
	if !*follow {
		if len(errs) != 0 {
			cli.Fatalf("Done with errors: %s", strings.Join(errs, ", "))
		}
	}
	cli.Success()
}
Beispiel #10
0
func Push(sous *core.Sous, args []string) {
	target := "app"
	if len(args) != 0 {
		target = args[0]
	}
	core.RequireGit()
	core.RequireDocker()
	if err := git.AssertCleanWorkingTree(); err != nil {
		cli.Warn("Dirty working tree: %s", err)
	}

	_, context := sous.AssembleTargetContext(target)

	tag := context.DockerTag()
	if !docker.ImageExists(tag) {
		cli.Fatalf("No built image available; try building first")
	}
	docker.Push(tag)
	name := context.CanonicalPackageName()
	cli.Successf("Successfully pushed %s v%s as %s", name, context.BuildVersion, context.DockerTag())
}
Beispiel #11
0
func Ls(sous *core.Sous, args []string) {
	//globalFlag := lsFlags.Bool("g", false, "global: list files in all projects sous has built")
	//lsFlags.Parse(args)
	//global := *globalFlag
	args = lsFlags.Args()
	if len(args) != 0 {
		cli.Fatalf("sous ls does not accept any arguments")
	}
	_, context := sous.AssembleTargetContext("app")
	cli.Outf(" ===> Images")
	images := sous.LsImages(context)
	if len(images) == 0 {
		cli.Logf("  no images for this project")
	}
	for _, image := range images {
		cli.Logf("  %s:%s", image.Name, image.Tag)
	}
	cli.Outf(" ===> Containers")
	containers := sous.LsContainers(context)
	if len(containers) == 0 {
		cli.Logf("  no containers for this project")
	}
	for _, container := range containers {
		cli.Logf("  %s (%s)", container.Name(), container.CID())
	}
	cli.Success()
}
Beispiel #12
0
func Build(sous *core.Sous, args []string) {
	targetName := "app"
	if len(args) != 0 {
		targetName = args[0]
	}
	core.RequireGit()
	core.RequireDocker()
	if err := git.AssertCleanWorkingTree(); err != nil {
		cli.Warn("Dirty working tree: %s", err)
	}

	target, context := sous.AssembleTargetContext(targetName)

	built, _ := sous.RunTarget(target, context)

	if !built {
		cli.Successf("Already built: %s", context.DockerTag())
	}

	name := context.CanonicalPackageName()
	cli.Successf("Successfully built %s v%s as %s", name, context.BuildVersion, context.DockerTag())
}
Beispiel #13
0
func Run(sous *core.Sous, args []string) {
	targetName := "app"
	if len(args) != 0 {
		targetName = args[0]
	}
	core.RequireGit()
	core.RequireDocker()

	target, context := sous.AssembleTargetContext(targetName)
	runner, ok := target.(core.ContainerTarget)
	if !ok {
		cli.Fatalf("Target %s does not support running.", target.Name())
	}

	rebuilt, _ := sous.RunTarget(target, context)
	dr, _ := sous.RunContainerTarget(runner, context, rebuilt)
	if exitCode := dr.ExitCode(); exitCode != 0 {
		cli.Logf("Docker container exited with code %d", exitCode)
		cli.Exit(exitCode)
	}
	cli.Success()
}