Example #1
0
/*
	Stop all running containers
*/
func StopAll() {
	cmdPsStr := "docker ps -a -q"
	output := util.RunCmd(exec.Command("/bin/sh", "-c", cmdPsStr), false)

	if len(output) > 0 {
		cmdStr := "docker stop $(docker ps -a -q)"
		fmt.Printf("%s\n", cmdStr)
		containerIds := util.RunCmd(exec.Command("/bin/sh", "-c", cmdStr), false)
		for _, id := range strings.Split(string(containerIds), "\r") {
			fmt.Printf(" - stopped container ids\n%s\n", strings.TrimSpace(string(id)))
		}
	}
}
Example #2
0
/*
	Stop specified container(s)
*/
func Stop(containers []Container) {
	for _, container := range containers {
		if IsImageRunning(container.Image.Base) {
			fmt.Printf("Stopping image %s\n", container.Image.String())
			cmdStr := fmt.Sprintf("docker stop %s", container.ContainerId)
			fmt.Printf(" - %s\n", cmdStr)
			containerId := util.RunCmd(exec.Command("/bin/sh", "-c", cmdStr), false)
			fmt.Printf(" - stopped container id %s\n", strings.TrimSpace(string(containerId)))
		}
	}
}
Example #3
0
func BuildListOfRunningContainers() []Container {
	rawOutput := string(util.RunCmd(exec.Command("docker", "ps"), false))
	rawRows := strings.Split(rawOutput, "\n")

	if len(rawRows) == 0 {
		return []Container{}
	} else {
		//create Container struct(s) from raw command line output
		containerData := rawRows[1 : len(rawRows)-1]
		containers := make([]Container, len(containerData))

		for idx, image := range containerData {
			imageFields := strings.Fields(image)

			if len(imageFields) > 0 {
				containers[idx] = Container{ContainerId: strings.TrimSpace(imageFields[0]), Image: MakeImage(strings.TrimSpace(imageFields[1]))}
			}
		}
		return containers
	}
}
Example #4
0
func GetLatestLocalVersion(image string) string {
	max := "0.0.0"
	var semanticVersion = regexp.MustCompile("^[0-9]+\\.[0-9]+\\.[0-9]+$")

	rawOutput := string(util.RunCmd(exec.Command("docker", "images", fmt.Sprintf("%s", image)), false))
	rawRows := strings.Split(rawOutput, "\n")

	if len(rawRows) == 0 {
		fmt.Printf("No image found matching repository name [%s]", image)
		os.Exit(1)
	} else {
		//create Image struct(s) from raw command line output
		imageData := rawRows[1 : len(rawRows)-1]
		//images := make([]Image, len(imageData))

		for _, image := range imageData {
			imageFields := strings.Fields(image)

			if len(imageFields) > 0 {
				tag := strings.TrimSpace(imageFields[1])
				if semanticVersion.MatchString(tag) {
					if version.CompareSimple(tag, max) == 1 {
						max = tag
					}
				}
			}
		}

		//Flow rule: if no latest semantic version was found, default to 'latest'
		if max == "0.0.0" {
			max = "latest"
		}

		return max
	}

	return "latest"
}
Example #5
0
func GetContainerIdFromImageName(image string) Container {
	containerId := string(util.RunCmd(exec.Command("docker", "ps", "-q", fmt.Sprintf("--filter=\"ancestor=%s\"", image)), false))
	container := Container{containerId, MakeImage(image)}
	return container
}