//cleanWorkDirectory removes the .workdir directory once the graph has been deployed. func (b *Base) cleanWorkDirectory() { workDirectory := filepath.Join(b.Target(), constants.WORKDIR) if utils.PathExists(workDirectory) { logrus.Debugf("Cleaning up work directory at %s\n", workDirectory) os.RemoveAll(workDirectory) } }
//CheckComponentArtifacts will verify that valid artifacts exist for the specified provider //The specified provider must be a member of the given component func (b *Base) checkProviderArtifact(c Component, provider string, checkedProviders *[]string) { logrus.Debugf("Provider : %v", provider) //If the provider has already been checked, skip it if providerAlreadyChecked(checkedProviders, provider) { return } if artifacts, ok := c.Artifacts[provider]; ok { //Iterate through each individual artifact entry for each provider for _, artifactEntry := range artifacts { //If the entry has a path field, check it for validity if artifactEntry.Path != "" { fullPath := filepath.Join(b.Target(), utils.SanitizePath(artifactEntry.Path)) if utils.PathExists(fullPath) && utils.PathIsFile(fullPath) { logrus.Infof("Artifact %s: OK.", fullPath) } else { logrus.Errorf("Artifact %s: MISSING.", fullPath) } } //For this artfiact to be 'fully checked', //we need to verify that the inherited providers (if any) are valid as well b.checkInheritence(c, provider, artifactEntry.Repo.Inherit, checkedProviders) } *checkedProviders = append(*checkedProviders, provider) } }
//Determines the path of kubectl on the host func (p *Kubernetes) findKubeCtlPath() (string, error) { if p.DryRun() { return "/usr/bin/kubectl", nil } //Insert some additional test paths to try p.addCLIPaths( "/usr/bin/kubectl", "/usr/local/bin/kubectl", ) for _, path := range p.CLIPath() { //Add /host as a prefix if InContainer set to true if p.InContainer { path = filepath.Join("/host", path) } if utils.PathExists(path) { logrus.Debugf("Found kubectl at %s", path) return path, nil } } logrus.Errorf("Unable to find valid kubectl installation. Tried the following locations: %v", p.CLIPath()) return "", errors.New("No kubectl installation found") }
//Returns an *os.File pointing to the answers file. If one does not exist, it is created and returned. func (b *Base) createAnswersFile(sampleAnswersPath string) (*os.File, error) { if utils.PathExists(sampleAnswersPath) { return os.OpenFile(sampleAnswersPath, os.O_APPEND|os.O_WRONLY, 0600) } sampleAnswersFile, err := os.Create(sampleAnswersPath) if err != nil { logrus.Fatalf("Unable to create sample answers file: %s", err) return nil, err } return sampleAnswersFile, nil }
func (b *Base) setAnswersDir(answersDir string) error { if !utils.PathExists(answersDir) { if answersDir != "" { logrus.Warnf("Invalid answers directory provided: '%s'. Using '%s' instead", answersDir, b.Target()) } b.answersDir = b.Target() return errors.New("Using target path as answers directory") } b.answersDir = answersDir return nil }
//LoadAnswers Unmarshals the answers from the answers.conf file into the base AnswersData func (b *Base) LoadAnswers() error { //if a directory was provided... fp := b.AnswersDir() if utils.PathIsDirectory(fp) { //Construct the full path by combining with the ANSWERS_FILE constant fp = filepath.Join(fp, constants.ANSWERS_FILE) if !utils.PathExists(fp) { return errors.New("Failed to read answers from path") } } //..try to parse the file if !utils.PathExists(fp) { return errors.New("Bad answers filepath") } p := parser.NewParser(fp) err := p.Unmarshal(&b.AnswersData) if err != nil { return errors.New("Failed to parse file") } return nil }
//Generates a new work directory //Returns a path to the directory func makeWorkDirectory(targetPath string) (string, error) { workdir := filepath.Join(targetPath, constants.WORKDIR) //If the .workdir directory does not exist in targetPath, make it. if !utils.PathExists(workdir) { logrus.Debugf("Making workdir in %s", targetPath) err := os.MkdirAll(workdir, 0700) if err != nil { logrus.Fatalf("Failed to make work directory in %s", targetPath) return "", errors.New("Failed to make work directory") } } return workdir, nil }
//Unmarshal arses a markup file into the given interface object func (p *Parser) Unmarshal(result interface{}) error { if !utils.PathExists(p.markupFile) { return errors.New("File does not exist") } f, err := ioutil.ReadFile(p.markupFile) if err != nil { return err } if isJSON(f) { return json.Unmarshal(f, result) } return yaml.Unmarshal(f, result) }
func (b *Base) setTargetPath(target string) error { //If no target is specified or if the user specifies a '.', //then use the current working directory if target == "" || !utils.PathExists(target) { cwd, err := os.Getwd() if err != nil { logrus.Fatalf("Failed to get working directory") return errors.New("Failed to set target path") } b.targetPath = cwd return nil } b.targetPath = target return nil }
//ReadMainFile will read the Nulecule file and fill the MainfileData field func (b *Base) ReadMainFile() error { //Check for valid path targetFile := filepath.Join(b.Target(), constants.MAIN_FILE) if !utils.PathExists(targetFile) { logrus.Fatalf("Could not find %s file in %s", constants.MAIN_FILE, b.Target()) return errors.New("File does not exist") } //Attempt to parse p := parser.NewParser(targetFile) err := p.Unmarshal(b.MainfileData) if err != nil { logrus.Errorf("Error parsing Nulecule file: %v", err) return err } return nil }
//Deploy the Docker Provider func (p *Docker) Deploy() error { //Iterate through artifact entries for the docker provider for _, artifact := range p.Artifacts() { //Form the absolute path of the artifact base := filepath.Base(artifact) fullPath := filepath.Join(p.WorkDirectory(), base) if !utils.PathExists(fullPath) { logrus.Errorf("No such docker artifact: %v\n", fullPath) return errors.New("No such docker artifact path") } err := p.dockerCmd(fullPath) if 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 }
//checkInstallOverwrite returns a nil error if no nulecule app exists in the target directory. //If a nulecule app does exist in the target directory, it checks if it has a matching app ID and //if it does, allows the installation process to continue. If a different nulecule app has been found, //it warns the user that they are overwriting an existing app and returns an error status func checkInstallOverwrite(tmpTarget, dstTarget string) error { if !utils.PathExists(filepath.Join(dstTarget, constants.MAIN_FILE)) { logrus.Debugf("No existing app found in target directory, installation can continue") return nil } //Parse the nulecule file in the target directory targetBase := New(dstTarget, "") targetBase.ReadMainFile() targetID := targetBase.MainfileData.ID //Parse the nulecule file in the tmp directory tmpBase := New(tmpTarget, "") tmpBase.ReadMainFile() tmpID := tmpBase.MainfileData.ID if targetID != tmpID { logrus.Fatalf("You are trying to overwrite existing app %s with app %s - clear or change current directory.", targetID, tmpID) return errors.New("Conflicting app install path") } return nil }