Skip to content

Commit

Permalink
Merge branch 'modify_install.go' into puppetlabs-toy-chestGH-136/main…
Browse files Browse the repository at this point in the history
…/install_template_from_git
  • Loading branch information
petergmurphy committed Sep 28, 2021
2 parents ceeccd4 + 913ed06 commit 7e6fa89
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
24 changes: 15 additions & 9 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type InstallCommand struct {
InstallPath string
Force bool
PctInstaller pct.PctInstallerI
GitUri string
GitUri string
}

type InstallCommandI interface {
Expand All @@ -41,7 +41,14 @@ func (ic *InstallCommand) CreateCommand() *cobra.Command {
}

func (ic *InstallCommand) executeInstall(cmd *cobra.Command, args []string) error {
templateInstallationPath, err := ic.PctInstaller.Install(ic.TemplatePkgPath, ic.InstallPath, ic.Force)
var templateInstallationPath string = ""
var err error = nil
if ic.GitUri != "" { // For cloning a template
templateInstallationPath, err = ic.PctInstaller.InstallClone(ic.GitUri, ic.InstallPath, ic.Force)
} else { // For downloading and/or locally installing a template
templateInstallationPath, err = ic.PctInstaller.Install(ic.TemplatePkgPath, ic.InstallPath, ic.Force)
}

if err != nil {
return err
}
Expand All @@ -64,18 +71,17 @@ func (ic *InstallCommand) setInstallPath() error {
}

func (ic *InstallCommand) preExecute(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
if len(args) < 1 && ic.GitUri == "" {
return fmt.Errorf("Path to template package (tar.gz) should be first argument")
}

if len(args) == 1 {
ic.TemplatePkgPath = args[0]
return ic.setInstallPath()
}

if len(args) > 1 {
return fmt.Errorf("Incorrect number of arguments; path to template package (tar.gz) should be first argument")
}

return nil
if len(args) == 1 {
ic.TemplatePkgPath = args[0]
}

return ic.setInstallPath()
}
4 changes: 4 additions & 0 deletions internal/pkg/mock/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func (p *PctInstaller) Install(templatePkg string, targetDir string, force bool)

return filepath.Clean("/unit/test/path"), nil
}

func (p *PctInstaller) InstallClone(templatePkg string, targetDir string, force bool) (string, error) {
return "WIP", fmt.Errorf("Work in Progress")
}
69 changes: 64 additions & 5 deletions internal/pkg/pct/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"

Expand All @@ -26,6 +27,7 @@ type PctInstaller struct {

type PctInstallerI interface {
Install(templatePkg string, targetDir string, force bool) (string, error)
InstallClone(templatePkg string, targetDir string, force bool) (string, error)
}

func (p *PctInstaller) Install(templatePkg string, targetDir string, force bool) (string, error) {
Expand Down Expand Up @@ -78,30 +80,87 @@ func (p *PctInstaller) InstallTarGz(templatePkg string, targetDir string, force
}


folderPath, err := p.unTarGz(templatePkg, tempDir)
if err != nil {
return "", err
}

// determine the properties of the template
info, err := p.readConfig(filepath.Join(untarPath, "pct-config.yml"))
info, err := p.readConfig(filepath.Join(folderPath, "pct-config.yml"))
if err != nil {
return "", fmt.Errorf("Invalid config: %v", err.Error())
}

namespacedPath, err := p.setupTemplateNamespace(targetDir, info, untarPath, force)
namespacedPath, err := p.setupTemplateNamespace(targetDir, info, folderPath, force)
if err != nil {
return "", fmt.Errorf("Unable to install in namespace: %v", err.Error())
}

return namespacedPath, nil
}

func (p *PctInstaller) InstallDownload(templatePkg string, targetDir string, force bool) (string, error) {
func (p *PctInstaller) InstallClone(gitUri string, targetDir string, force bool) (string, error) {
// Validate git URI
log.Info().Msgf("Git URI: ", gitUri)
if !strings.HasPrefix(gitUri, "http") || !strings.HasSuffix(gitUri, ".git") {
return "", fmt.Errorf("Could not clone git repo: Invalid URI provided")
}

}
// Create temp folder
tempDir, err := p.AFS.TempDir("", "")
defer func() {
err := p.AFS.Remove(tempDir)
log.Debug().Msgf("Failed to remove temp dir: %v", err)
}()
if err != nil {
return "", fmt.Errorf("Could not create tempdir to gunzip template: %v", err)
}

// Clone git repository to temp folder
folderPath, err := p.cloneTemplate(gitUri, tempDir)
if err != nil {
return "", fmt.Errorf("Could not clone git repository: %v", err)
}

// Read config to determine template properties
info, err := p.readConfig(filepath.Join(folderPath, "pct-config.yml"))
if err != nil {
return "", fmt.Errorf("Invalid config: %v", err.Error())
}

func (p *PctInstaller) InstallClone(templatePkg string, targetDir string, force bool) (string, error) {
// Create namespaced directory and move contents of temp folder to it
namespacedPath, err := p.setupTemplateNamespace(targetDir, info, folderPath, force)
if err != nil {
return "", fmt.Errorf("Unable to install in namespace: %v", err.Error())
}

return namespacedPath, nil
}

func (p *PctInstaller) cloneTemplate(gitUri string, tempDir string) (string, error) {
clonePath := filepath.Join(tempDir, "temp")
command := exec.Command("git", "clone", gitUri, clonePath)
err := command.Run()
if err != nil {
return "", err
}
return clonePath, nil
}

func (p *PctInstaller) unTarGz(templatePkg string, tempDir string) (string, error) {
// gunzip the tar.gz to created tempdir
tarfile, err := p.Gunzip.Gunzip(templatePkg, tempDir)
if err != nil {
return "", fmt.Errorf("Could not extract TAR from GZIP (%v): %v", templatePkg, err)
}

// untar the above archive to the temp dir
untarPath, err := p.Tar.Untar(tarfile, tempDir)
if err != nil {
return "", fmt.Errorf("Could not UNTAR template (%v): %v", templatePkg, err)
}
return untarPath, nil
}

func (p *PctInstaller) readConfig(configFile string) (info PuppetContentTemplateInfo, err error) {
fileBytes, err := p.AFS.ReadFile(configFile)
Expand Down

0 comments on commit 7e6fa89

Please sign in to comment.