Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update packer-provisioner-goss.go #7

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ There is an example packer build with goss tests in the `example/` directory.
"remote_folder": "/tmp",
"remote_path": "/tmp/goss",
"skipInstall": false,
"skip_ssl": false,
"use_sudo": false,
"goss_file": "",
"Username": "",
"Password": "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be username and password

"debug": false
}
]
Expand Down
75 changes: 71 additions & 4 deletions packer-provisioner-goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type GossConfig struct {
Arch string
URL string
DownloadPath string
Username string
Password string
SkipInstall bool

// Enable debug for goss (defaults to false)
Expand All @@ -27,6 +29,15 @@ type GossConfig struct {
// An array of tests to run.
Tests []string

// Use Sudo
UseSudo bool `mapstructure:"use_sudo"`

// skip ssl check flag
SkipSSLChk bool `mapstructure:"skip_ssl"`

// The --gossfile flag
GossFile string `mapstructure:"goss_file"`

// The remote folder where the goss tests will be uploaded to.
// This should be set to a pre-existing directory, it defaults to /tmp
RemoteFolder string `mapstructure:"remote_folder"`
Expand Down Expand Up @@ -95,6 +106,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
p.config.Tests = make([]string, 0)
}

if p.config.GossFile != "" {
p.config.GossFile = fmt.Sprintf("--gossfile %s", p.config.GossFile)
}

var errs *packer.MultiError
if len(p.config.Tests) == 0 {
errs = packer.MultiErrorAppend(errs,
Expand Down Expand Up @@ -144,6 +159,12 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
if err := p.uploadFile(ui, comm, dst, src); err != nil {
return fmt.Errorf("Error uploading goss test: %s", err)
}
} else if s.Mode().IsDir() == true {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else if s.Mode().IsDir() {

also, you can fix the one above it too :)

ui.Message(fmt.Sprintf("Uploading Dir %s", src))
dst := filepath.ToSlash(filepath.Join(p.config.RemotePath, filepath.Base(src)))
if err := p.uploadDir(ui, comm, dst, src); err != nil {
return fmt.Errorf("Error uploading goss test: %s", err)
}
} else {
ui.Message(fmt.Sprintf("Ignoring %s... not a regular file", src))
}
Expand All @@ -169,8 +190,9 @@ func (p *Provisioner) installGoss(ui packer.Ui, comm packer.Communicator) error
cmd := &packer.RemoteCmd{
// Fallback on wget if curl failed for any reason (such as not being installed)
Command: fmt.Sprintf(
"curl -L -o %s %s || wget -O %s %s",
p.config.DownloadPath, p.config.URL, p.config.DownloadPath, p.config.URL),
"curl -L %s %s -o %s %s || wget %s %s -O %s %s",
p.sslFlag("curl"), p.userPass("curl"), p.config.DownloadPath, p.config.URL,
p.sslFlag("wget"), p.userPass("wget"), p.config.DownloadPath, p.config.URL),
}
ui.Message(fmt.Sprintf("Downloading Goss to %s", p.config.DownloadPath))
if err := cmd.StartWithUi(comm, ui); err != nil {
Expand All @@ -191,7 +213,8 @@ func (p *Provisioner) runGoss(ui packer.Ui, comm packer.Communicator) error {
goss := fmt.Sprintf("%s", p.config.DownloadPath)
cmd := &packer.RemoteCmd{
Command: fmt.Sprintf(
"cd %s && %s %s validate", p.config.RemotePath, goss, p.debug()),
"cd %s && %s %s %s %s validate",
p.config.RemotePath, p.enableSudo(), goss, p.config.GossFile, p.debug()),
}
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
Expand All @@ -211,6 +234,49 @@ func (p *Provisioner) debug() string {
return ""
}

func (p *Provisioner) sslFlag(cmdType string) string {
if p.config.SkipSSLChk == true {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if p.config.SkipSSLChk {

switch(cmdType) {
case "curl":
return "-k"
case "wget":
return "--no-check-certificate"
default:
return ""
}
}
return ""
}

// enable sudo if required
func (p *Provisioner) enableSudo() string {
if p.config.UseSudo == true {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if p.config.UseSudo {

return "sudo"
}
return ""
}

// Deal with curl & wget username and password
func (p *Provisioner) userPass(cmdType string) string {
if p.config.Username != "" {
switch(cmdType) {
case "curl":
if p.config.Password == "" {
return fmt.Sprintf("-u %s", p.config.Username)
}
return fmt.Sprintf("-u %s:%s", p.config.Username, p.config.Password)
case "wget":
if p.config.Password == "" {
return fmt.Sprintf("--user=%s", p.config.Username)
}
return fmt.Sprintf("--user=%s --password=%s", p.config.Username, p.config.Password)
default:
return ""
}
}
return ""
}

// createDir creates a directory on the remote server
func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir string) error {
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
Expand Down Expand Up @@ -241,7 +307,8 @@ func (p *Provisioner) uploadFile(ui packer.Ui, comm packer.Communicator, dst, sr
}

// uploadDir uploads a directory
func (p *Provisioner) uploadDir(ui packer.Ui, comm packer.Communicator, dst, src string, ignore []string) error {
func (p *Provisioner) uploadDir(ui packer.Ui, comm packer.Communicator, dst, src string) error {
var ignore []string
if err := p.createDir(ui, comm, dst); err != nil {
return err
}
Expand Down