Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-349) Move install logic from config_processor
Browse files Browse the repository at this point in the history
Prior to this commit, the private processor included the ProcessConfig
public function which called the private functions readConfig and
setupTemplateNamespace to place a package in the right folder structure
so that it is recognized as an installed package.

This is an install function placed in the config processor.

This commit replaces the ProcessConfig function in the
config_processor.ConfigProcessorI interface with GetConfigMetadata,
which returns the new ConfigMetadata struct also newly defined in
config_processor. This enables a consumer of a ConfigProcessorI
implementation to retrieve the metadata needed to install a package
generically. The prior implementation conflated configuration processing
with install logic.

This commit adds a new public function to the install package,
InstallFromConfig. This function combines the logic from the removed
ProcessConfig and setupTemplateNamespace functions from the private
config processor, genericising it in the process.

This commit extends the install.Installer struct to take a config file
name, so that no hard coding of the config file name is needed in the
package; this makes it generically reusable regardless of config file.
  • Loading branch information
petergmurphy committed Feb 16, 2022
1 parent b3ea9d2 commit 51f0502
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 49 deletions.
61 changes: 15 additions & 46 deletions internal/pkg/pct_config_processor/pct_config_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package pct_config_processor
import (
"bytes"
"fmt"
"path/filepath"

"github.com/puppetlabs/pdkgo/internal/pkg/pct"
"github.com/puppetlabs/pdkgo/pkg/config_processor"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
Expand All @@ -14,23 +14,27 @@ type PctConfigProcessor struct {
AFS *afero.Afero
}

func (p *PctConfigProcessor) ProcessConfig(sourceDir, targetDir string, force bool) (string, error) {
// Read config to determine template properties
info, err := p.readConfig(filepath.Join(sourceDir, "pct-config.yml"))
func (p *PctConfigProcessor) GetConfigMetadata(configFile string) (metadata config_processor.ConfigMetadata, err error) {
configInfo, err := p.ReadConfig(configFile)
if err != nil {
return "", fmt.Errorf("Invalid config: %v", err.Error())
return metadata, err
}

// Create namespaced directory and move contents of temp folder to it
namespacedPath, err := p.setupTemplateNamespace(targetDir, info, sourceDir, force)
err = p.CheckConfig(configFile)
if err != nil {
return "", fmt.Errorf("Unable to install in namespace: %v", err.Error())
return metadata, err
}
return namespacedPath, nil

metadata = config_processor.ConfigMetadata{
Author: configInfo.Template.Author,
Id: configInfo.Template.Id,
Version: configInfo.Template.Version,
}
return metadata, nil
}

func (p *PctConfigProcessor) CheckConfig(configFile string) error {
info, err := p.readConfig(configFile)
info, err := p.ReadConfig(configFile)
if err != nil {
return err
}
Expand All @@ -55,7 +59,7 @@ func (p *PctConfigProcessor) CheckConfig(configFile string) error {
return nil
}

func (p *PctConfigProcessor) readConfig(configFile string) (info pct.PuppetContentTemplateInfo, err error) {
func (p *PctConfigProcessor) ReadConfig(configFile string) (info pct.PuppetContentTemplateInfo, err error) {
fileBytes, err := p.AFS.ReadFile(configFile)
if err != nil {
return info, err
Expand All @@ -75,38 +79,3 @@ func (p *PctConfigProcessor) readConfig(configFile string) (info pct.PuppetConte

return info, err
}

func (p *PctConfigProcessor) setupTemplateNamespace(targetDir string, info pct.PuppetContentTemplateInfo, untarPath string, force bool) (string, error) {
// author/id/version
templatePath := filepath.Join(targetDir, info.Template.Author, info.Template.Id)

err := p.AFS.MkdirAll(templatePath, 0750)
if err != nil {
return "", err
}

namespacePath := filepath.Join(targetDir, info.Template.Author, info.Template.Id, info.Template.Version)

// finally move to the full path
err = p.AFS.Rename(untarPath, namespacePath)
if err != nil {
// if a template already exists
if !force {
// error unless forced
return "", fmt.Errorf("Template already installed (%s)", namespacePath)
} else {
// remove the exiting template
err = p.AFS.RemoveAll(namespacePath)
if err != nil {
return "", fmt.Errorf("Unable to overwrite existing template: %v", err)
}
// perform the move again
err = p.AFS.Rename(untarPath, namespacePath)
if err != nil {
return "", fmt.Errorf("Unable to force install: %v", err)
}
}
}

return namespacePath, err
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func main() {
ConfigProcessor: &pct_config_processor.PctConfigProcessor{
AFS: &afs,
},
ConfigFileName: "pct-config.yml",
},
AFS: &afs,
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/config_processor/config_processor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package config_processor

type ConfigProcessorI interface {
ProcessConfig(sourceDir, targetDir string, force bool) (string, error)
GetConfigMetadata(configFile string) (metadata ConfigMetadata, err error)
CheckConfig(configFile string) error
}

type ConfigMetadata struct {
Id string
Author string
Version string
}
48 changes: 46 additions & 2 deletions pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -32,6 +33,7 @@ type Installer struct {
HTTPClient httpclient.HTTPClientI
Exec exec_runner.ExecI
ConfigProcessor config_processor.ConfigProcessorI
ConfigFileName string
}

type InstallerI interface {
Expand Down Expand Up @@ -76,7 +78,7 @@ func (p *Installer) Install(templatePkg, targetDir string, force bool) (string,
}

// Process the configuration file and set up namespacedPath and relocate config and content to it
namespacedPath, err := p.ConfigProcessor.ProcessConfig(untarPath, targetDir, force)
namespacedPath, err := p.InstallFromConfig(filepath.Join(untarPath, p.ConfigFileName), targetDir, force)
if err != nil {
return "", fmt.Errorf("Invalid config: %v", err.Error())
}
Expand Down Expand Up @@ -125,7 +127,7 @@ func (p *Installer) InstallClone(gitUri, targetDir, tempDir string, force bool)
return "", fmt.Errorf("Failed to remove '.git' directory")
}

namespacedPath, err := p.ConfigProcessor.ProcessConfig(folderPath, targetDir, force)
namespacedPath, err := p.InstallFromConfig(filepath.Join(folderPath, p.ConfigFileName), targetDir, force)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -177,3 +179,45 @@ func (p *Installer) downloadTemplate(targetURL *url.URL, downloadDir string) (st

return downloadPath, nil
}

func (p *Installer) InstallFromConfig(configFile, targetDir string, force bool) (string, error) {
info, err := p.ConfigProcessor.GetConfigMetadata(configFile)
if err != nil {
return "", err
}

// Create namespaced directory and move contents of temp folder to it
installedPkgPath := filepath.Join(targetDir, info.Author, info.Id)

err = p.AFS.MkdirAll(installedPkgPath, 0750)
if err != nil {
return "", err
}

installedPkgPath = filepath.Join(installedPkgPath, info.Version)
untarredPkgDir := path.Dir(configFile)

// finally move to the full path
errMsgPrefix := "Unable to install in namespace:"
err = p.AFS.Rename(untarredPkgDir, installedPkgPath)
if err != nil {
// if a template already exists
if !force {
// error unless forced
return "", fmt.Errorf("%s Template already installed (%s)", errMsgPrefix, installedPkgPath)
} else {
// remove the exiting template
err = p.AFS.RemoveAll(installedPkgPath)
if err != nil {
return "", fmt.Errorf("%s Unable to overwrite existing template: %v", errMsgPrefix, err)
}
// perform the move again
err = p.AFS.Rename(untarredPkgDir, installedPkgPath)
if err != nil {
return "", fmt.Errorf("%s Unable to force install: %v", errMsgPrefix, err)
}
}
}

return installedPkgPath, err
}

0 comments on commit 51f0502

Please sign in to comment.