github.com/verrazzano/verrazzano@v1.7.1/tools/psr/psrctl/pkg/manifest/manifest.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package manifest
     5  
     6  import (
     7  	"os"
     8  	"os/user"
     9  	"path/filepath"
    10  
    11  	"github.com/verrazzano/verrazzano/tools/psr"
    12  )
    13  
    14  // PsrManifests contains information related to the manifests, along with the temp
    15  // directory path.
    16  type PsrManifests struct {
    17  	RootTmpDir        string
    18  	WorkerChartAbsDir string
    19  	UseCasesAbsDir    string
    20  	ScenarioAbsDir    string
    21  }
    22  
    23  var Manifests *PsrManifests
    24  
    25  // InitGlobalManifests extracts the manifests in the binary and writes them to a temp file.
    26  // The package level Manifests var is set if this function succeeds.
    27  // The caller is expected to call CleanupManifests when they are no longer needed
    28  func InitGlobalManifests() error {
    29  	tmpDir, err := createPsrTempDir()
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	man, err := newPsrManifests(tmpDir)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	Manifests = &man
    39  	return nil
    40  }
    41  
    42  // CleanupManifests deletes the manifests that were copied to a temp dir
    43  func CleanupManifests() {
    44  	os.RemoveAll(Manifests.RootTmpDir)
    45  }
    46  
    47  // createPsrTempDir creates a temp dir to hold the manifests files
    48  func createPsrTempDir() (string, error) {
    49  	u, err := user.Current()
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  
    54  	// Use homedir for temp files since root might own temp dir on OSX and we get
    55  	// errors trying to create temp files
    56  	hidden := filepath.Join(u.HomeDir, ".psr-temp")
    57  	err = os.Mkdir(hidden, 0700)
    58  	if err != nil && !os.IsExist(err) {
    59  		return "", err
    60  	}
    61  
    62  	topDir, err := os.MkdirTemp(hidden, "psr")
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	return topDir, nil
    67  }
    68  
    69  // newPsrManifests creates a new PsrManifests structure
    70  func newPsrManifests(tmpRootDir string) (PsrManifests, error) {
    71  	copyManifestsDir(tmpRootDir)
    72  
    73  	man := PsrManifests{
    74  		RootTmpDir:        tmpRootDir,
    75  		WorkerChartAbsDir: filepath.Join(tmpRootDir, "charts/worker"),
    76  		UseCasesAbsDir:    filepath.Join(tmpRootDir, "usecases"),
    77  		ScenarioAbsDir:    filepath.Join(tmpRootDir, "scenarios"),
    78  	}
    79  	return man, nil
    80  }
    81  
    82  // copyManifestsDir copies the embedded manifests to a directory
    83  func copyManifestsDir(rootDir string) error {
    84  	err := writeDirDeep(rootDir, "manifests")
    85  	if err != nil {
    86  		return err
    87  	}
    88  	return nil
    89  }
    90  
    91  // writeDirDeep writes the embedded manifests files to a temp directory,
    92  // retaining the same directory structure as the source directory tree
    93  func writeDirDeep(destDir string, embeddedParent string) error {
    94  	dirEntries, err := psr.GetEmbeddedManifests().ReadDir(embeddedParent)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	for _, d := range dirEntries {
    99  		if d.IsDir() {
   100  			dir := filepath.Join(destDir, d.Name())
   101  			err := os.Mkdir(dir, 0700)
   102  			if err != nil && !os.IsExist(err) {
   103  				return err
   104  			}
   105  			embeddedChild := filepath.Join(embeddedParent, d.Name())
   106  			if err := writeDirDeep(dir, embeddedChild); err != nil {
   107  				return err
   108  			}
   109  			continue
   110  		}
   111  		// Write the file
   112  		inEmbeddedPath := filepath.Join(embeddedParent, d.Name())
   113  		f, err := psr.GetEmbeddedManifests().ReadFile(inEmbeddedPath)
   114  		if err != nil {
   115  			return err
   116  		}
   117  		outPath := filepath.Join(destDir, d.Name())
   118  		err = os.WriteFile(outPath, f, 0600)
   119  		if err != nil {
   120  			return err
   121  		}
   122  	}
   123  	return nil
   124  }
   125  
   126  func ResetManifests() {
   127  	Manifests = nil
   128  }