Skip to content

Commit

Permalink
feat: added retry timeout options
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 6, 2018
1 parent 9e36bb8 commit 9408f31
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ There is an example packer build with goss tests in the `example/` directory.
"vars_file": "",
"username": "",
"password": "",
"debug": false
"debug": false,
"retry_timeout": "0s",
"sleep": "1s"
}
]
```
Expand Down
33 changes: 26 additions & 7 deletions packer-provisioner-goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ type GossConfig struct {
// An array of tests to run.
Tests []string

// Goss options for retry and timeouts
RetryTimeout string `mapstructure:"retry_timeout"`
Sleep string `mapstructure:"sleep"`

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

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

// The --gossfile flag
GossFile string `mapstructure:"goss_file"`
Expand Down Expand Up @@ -252,11 +256,12 @@ func (p *Provisioner) installGoss(ui packer.Ui, comm packer.Communicator) error

// runGoss runs the Goss tests
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 %s %s %s validate %s",
p.config.RemotePath, p.enableSudo(), goss, p.config.GossFile, p.vars(), p.debug(), p.format()),
"cd %s && %s %s %s %s %s validate --retry-timeout %s --sleep %s %s",
p.config.RemotePath, p.enableSudo(), p.config.DownloadPath, p.config.GossFile,
p.vars(), p.debug(), p.retryTimeout(), p.sleep(), p.format(),
),
}
if err := cmd.StartWithUi(comm, ui); err != nil {
return err
Expand All @@ -268,6 +273,20 @@ func (p *Provisioner) runGoss(ui packer.Ui, comm packer.Communicator) error {
return nil
}

func (p *Provisioner) retryTimeout() string {
if p.config.RetryTimeout == "" {
return "0s" // goss default
}
return p.config.RetryTimeout
}

func (p *Provisioner) sleep() string {
if p.config.Sleep == "" {
return "1s" // goss default
}
return p.config.Sleep
}

// debug returns the debug flag if debug is configured
func (p *Provisioner) debug() string {
if p.config.Debug {
Expand All @@ -292,7 +311,7 @@ func (p *Provisioner) vars() string {

func (p *Provisioner) sslFlag(cmdType string) string {
if p.config.SkipSSLChk {
switch(cmdType) {
switch cmdType {
case "curl":
return "-k"
case "wget":
Expand All @@ -315,7 +334,7 @@ func (p *Provisioner) enableSudo() string {
// Deal with curl & wget username and password
func (p *Provisioner) userPass(cmdType string) string {
if p.config.Username != "" {
switch(cmdType) {
switch cmdType {
case "curl":
if p.config.Password == "" {
return fmt.Sprintf("-u %s", p.config.Username)
Expand All @@ -327,7 +346,7 @@ func (p *Provisioner) userPass(cmdType string) string {
}
return fmt.Sprintf("--user=%s --password=%s", p.config.Username, p.config.Password)
default:
return ""
return ""
}
}
return ""
Expand Down

0 comments on commit 9408f31

Please # to comment.