Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Commit

Permalink
Refactor bootstrapping workspace dir management (issue #90)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbanos committed Mar 20, 2019
1 parent 8a22cbe commit 5607bf0
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions bootstrapping/bootstrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,41 @@ func handleSysSignals(cancelFunc context.CancelFunc) {
cancelFunc()
}

// Returns the full path to the tmp directory joined with lock management file name
func getProcessLockFilePath() string {
return filepath.Join(os.TempDir(), string(os.PathSeparator), ProcessLockFile)
// Returns the full path to the tmp directory joined with pid management file name
func lockFilePath() string {
return filepath.Join(workspaceDir(), ProcessLockFile)
}

func workspaceDir() string {
return filepath.Join(os.TempDir(), "imco")
}

// Returns the full path to the tmp directory
func generateWorkspaceDir() (string, error) {
dir := filepath.Join(os.TempDir(), "imco")
func generateWorkspaceDir() error {
dir := workspaceDir()
dirInfo, err := os.Stat(dir)
if err != nil {
err := os.Mkdir(dir, 0777)
if err != nil {
return "", err
return err
}
} else {
if !dirInfo.Mode().IsDir() {
return "", fmt.Errorf("%s exists but is not a directory", dir)
return fmt.Errorf("%s exists but is not a directory", dir)
}
}
return dir, nil
return nil
}

// Start the bootstrapping process
func start(c *cli.Context) error {
log.Debug("start")

lockFile, err := singleinstance.CreateLockFile(getProcessLockFilePath())
err := generateWorkspaceDir()
if err != nil {
return err
}
lockFile, err := singleinstance.CreateLockFile(lockFilePath())
if err != nil {
return err
}
Expand Down Expand Up @@ -181,7 +189,7 @@ func stop(c *cli.Context) error {
log.Debug("cmdStop")

formatter := format.GetFormatter()
if err := utils.StopProcess(getProcessLockFilePath()); err != nil {
if err := utils.StopProcess(lockFilePath()); err != nil {
formatter.PrintFatal("cannot stop the bootstrapping process", err)
}

Expand All @@ -202,15 +210,15 @@ func applyPolicyfiles(ctx context.Context, bootstrappingSvc *blueprint.Bootstrap
formatter.PrintError("couldn't receive bootstrapping data", err)
return err
}
dir, err := generateWorkspaceDir()
err = generateWorkspaceDir()
if err != nil {
formatter.PrintError("couldn't generated workspace directory", err)
return err
}
bsProcess := &bootstrappingProcess{
startedAt: time.Now().UTC(),
thresholdLines: thresholdLines,
directoryPath: dir,
directoryPath: workspaceDir(),
appliedPolicyfileRevisionIDs: make(map[string]string),
}

Expand Down Expand Up @@ -422,4 +430,4 @@ func completeBootstrappingSequence(bsProcess *bootstrappingProcess) error {
allPolicyfilesSuccessfullyApplied = true
}
return nil
}
}

0 comments on commit 5607bf0

Please sign in to comment.