//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 }
//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 }
//deletePod 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 }