refactor: add multiError in createInstanceMetadata method to capture multiple errors

Signed-off-by: Pratyush singhal <psinghal20@gmail.com>
This commit is contained in:
Pratyush singhal 2019-06-11 20:08:03 +05:30
parent 4a369b4ef1
commit 6ce6bd8ad3
2 changed files with 14 additions and 13 deletions

View File

@ -16,10 +16,10 @@ type StepCreateInstance struct {
Debug bool
}
func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string, state multistep.StateBag) (map[string]string, error) {
func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string) (map[string]string, *packer.MultiError) {
instanceMetadata := make(map[string]string)
var err error
ui := state.Get("ui").(packer.Ui)
var errs *packer.MultiError
// Copy metadata from config.
for k, v := range c.Metadata {
@ -43,9 +43,7 @@ func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string,
var content []byte
content, err = ioutil.ReadFile(c.StartupScriptFile)
if err != nil {
err = fmt.Errorf("Error reading startup script file: %s", err)
state.Put("error", err)
ui.Error(err.Error())
errs = packer.MultiErrorAppend(errs, err)
}
instanceMetadata[StartupWrappedScriptKey] = string(content)
} else if wrappedStartupScript, exists := instanceMetadata[StartupScriptKey]; exists {
@ -57,9 +55,7 @@ func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string,
var content []byte
content, err = ioutil.ReadFile(value)
if err != nil {
err = fmt.Errorf("Error getting %s metadata from %s: %s", key, value, err)
state.Put("error", err)
ui.Error(err.Error())
errs = packer.MultiErrorAppend(errs, err)
}
instanceMetadata[key] = string(content)
}
@ -74,7 +70,7 @@ func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string,
instanceMetadata[StartupScriptStatusKey] = StartupScriptStatusNotDone
}
return instanceMetadata, err
return instanceMetadata, errs
}
func getImage(c *Config, d Driver) (*Image, error) {
@ -117,7 +113,12 @@ func (s *StepCreateInstance) Run(ctx context.Context, state multistep.StateBag)
var errCh <-chan error
var metadata map[string]string
metadata, err = c.createInstanceMetadata(sourceImage, string(c.Comm.SSHPublicKey), state)
metadata, errs := c.createInstanceMetadata(sourceImage, string(c.Comm.SSHPublicKey))
if errs != nil && len(errs.Errors) > 0 {
state.Put("error", errs.Error())
ui.Error(errs.Error())
}
errCh, err = d.RunInstance(&InstanceConfig{
AcceleratorType: c.AcceleratorType,
AcceleratorCount: c.AcceleratorCount,

View File

@ -303,7 +303,7 @@ func TestCreateInstanceMetadata(t *testing.T) {
key := "abcdefgh12345678"
// create our metadata
metadata, err := c.createInstanceMetadata(image, key, state)
metadata, err := c.createInstanceMetadata(image, key)
assert.True(t, err == nil, "Metadata creation should have succeeded.")
@ -318,7 +318,7 @@ func TestCreateInstanceMetadata_noPublicKey(t *testing.T) {
sshKeys := c.Metadata["sshKeys"]
// create our metadata
metadata, err := c.createInstanceMetadata(image, "", state)
metadata, err := c.createInstanceMetadata(image, "")
assert.True(t, err == nil, "Metadata creation should have succeeded.")
@ -335,7 +335,7 @@ func TestCreateInstanceMetadata_metadataFile(t *testing.T) {
c.MetadataFiles["user-data"] = fileName
// create our metadata
metadata, err := c.createInstanceMetadata(image, "", state)
metadata, err := c.createInstanceMetadata(image, "")
assert.True(t, err == nil, "Metadata creation should have succeeded.")