//Check to ensure that we have a valid version of docker before deploying func (p *Docker) checkVersion() error { daemonCmd := exec.Command("docker", "version", "--format", "'{{.Server.ApiVersion}}'") daemonOut, err := utils.CheckCommandOutput(daemonCmd, true) if err != nil { return err } clientCmd := exec.Command("docker", "version", "--format", "'{{.Client.ApiVersion}}'") clientOut, err := utils.CheckCommandOutput(clientCmd, true) if err != nil { return err } daemonString := strings.Trim(string(daemonOut), " '\n") clientString := strings.Trim(string(clientOut), " '\n") daemonVersion, err := strconv.ParseFloat(string(daemonString), 64) if err != nil { logrus.Errorf("Could not determine docker daemon version: %v\n", err) return errors.New("Unknown docker daemon version\n") } clientVersion, err := strconv.ParseFloat(string(clientString), 64) if err != nil { logrus.Errorf("Could not determine docker client version: %v\n", err) return errors.New("Unknown docker client version\n") } logrus.Debugf("Docker client is version %v and Docker daemon is version %v\n", clientVersion, daemonVersion) if clientVersion > daemonVersion { logrus.Errorf("Docker client is newer than daemon: please upgrade your daemon to a newer version\n") return errors.New("Docker daemon out of date") } return nil }
//Pulls the container with the given image name from docker func Pull(image string) error { dockerPull := exec.Command(CONTAINTER, "pull", image) if _, err := utils.CheckCommandOutput(dockerPull, false); err != nil { return err } return nil }
//Creates a container with the given name from the image provided func Create(containerName, image string) error { dockerCreate := exec.Command(CONTAINTER, "create", "--name", containerName, image, "nop") if _, err := utils.CheckCommandOutput(dockerCreate, false); err != nil { return err } return nil }
//Removes the container with the specified name func Remove(containerName string) error { dockerRm := exec.Command(CONTAINTER, "rm", containerName) if _, err := utils.CheckCommandOutput(dockerRm, true); err != nil { return err } return nil }
//Copies the data from the container to another func Copy(containerName, copyFrom, copyTo string) error { cpField := fmt.Sprintf("%s:%s", containerName, constants.APP_ENT_PATH) dockerCp := exec.Command(CONTAINTER, "cp", cpField, copyTo) if _, err := utils.CheckCommandOutput(dockerCp, false); err != nil { return err } return nil }
//Issues a command to kubectl. //Path is the path of the kubernets file to pass to kubectl func (p *Kubernetes) kubectlCmd(path string) error { if p.Namespace == "" { p.Namespace = constants.DEFAULT_NAMESPACE } path = utils.SanitizePath(path) namespaceFlag := fmt.Sprintf("--namespace=%s", p.Namespace) kubeCmd := exec.Command(p.KubeCtl, "create", "-f", path, namespaceFlag) //In a dry run, we don't actually execute the commands, so we log and return here if p.DryRun() { logrus.Infof("DRY RUN: %s\n", kubeCmd.Args) return nil } if _, err := utils.CheckCommandOutput(kubeCmd, false); err != nil { return err } return nil }
//Removes the kubernetes pod func (p *Kubernetes) deletePod(artifactPath string) error { if !utils.PathExists(artifactPath) { logrus.Errorf("No valid artifact could be found at %s", artifactPath) return errors.New("Path does not exist") } namespaceFlag := fmt.Sprintf("--namespace=%s", p.Namespace) deleteCmd := exec.Command(p.KubeCtl, "delete", "-f", artifactPath, namespaceFlag) //In a dry run, we don't actually execute the commands, so we log and return here if p.DryRun() { logrus.Infof("DRY RUN: %s\n", deleteCmd.Args) return nil } if _, err := utils.CheckCommandOutput(deleteCmd, false); err != nil { return err } return nil }
//Resets the replication controller with the given name func (p *Kubernetes) resetReplica(name string) error { if name == "" { logrus.Errorf("No pod name provided in call to reset replication controller.") return errors.New("No pod name provided") } namespaceFlag := fmt.Sprintf("--namespace=%s", p.Namespace) resizeCmd := exec.Command(p.KubeCtl, "resize", "rc", name, "--replicas=0", namespaceFlag) //In a dry run, we don't actually execute the commands, so we log and return here if p.DryRun() { logrus.Infof("DRY RUN: %s\n", resizeCmd.Args) return nil } if _, err := utils.CheckCommandOutput(resizeCmd, false); err != nil { logrus.Errorf("Error reseting kubernetes replication controller") return err } return nil }