github.com/jerryclinesmith/packer@v0.3.7/builder/amazon/instance/builder.go (about)

     1  // The instance package contains a packer.Builder implementation that builds
     2  // AMIs for Amazon EC2 backed by instance storage, as opposed to EBS storage.
     3  package instance
     4  
     5  import (
     6  	"errors"
     7  	"fmt"
     8  	"github.com/mitchellh/goamz/ec2"
     9  	"github.com/mitchellh/multistep"
    10  	awscommon "github.com/mitchellh/packer/builder/amazon/common"
    11  	"github.com/mitchellh/packer/common"
    12  	"github.com/mitchellh/packer/packer"
    13  	"log"
    14  	"os"
    15  	"strings"
    16  )
    17  
    18  // The unique ID for this builder
    19  const BuilderId = "mitchellh.amazon.instance"
    20  
    21  // Config is the configuration that is chained through the steps and
    22  // settable from the template.
    23  type Config struct {
    24  	common.PackerConfig    `mapstructure:",squash"`
    25  	awscommon.AccessConfig `mapstructure:",squash"`
    26  	awscommon.AMIConfig    `mapstructure:",squash"`
    27  	awscommon.BlockDevices `mapstructure:",squash"`
    28  	awscommon.RunConfig    `mapstructure:",squash"`
    29  
    30  	AccountId           string `mapstructure:"account_id"`
    31  	BundleDestination   string `mapstructure:"bundle_destination"`
    32  	BundlePrefix        string `mapstructure:"bundle_prefix"`
    33  	BundleUploadCommand string `mapstructure:"bundle_upload_command"`
    34  	BundleVolCommand    string `mapstructure:"bundle_vol_command"`
    35  	S3Bucket            string `mapstructure:"s3_bucket"`
    36  	X509CertPath        string `mapstructure:"x509_cert_path"`
    37  	X509KeyPath         string `mapstructure:"x509_key_path"`
    38  	X509UploadPath      string `mapstructure:"x509_upload_path"`
    39  
    40  	tpl *packer.ConfigTemplate
    41  }
    42  
    43  type Builder struct {
    44  	config Config
    45  	runner multistep.Runner
    46  }
    47  
    48  func (b *Builder) Prepare(raws ...interface{}) error {
    49  	md, err := common.DecodeConfig(&b.config, raws...)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	b.config.tpl, err = packer.NewConfigTemplate()
    55  	if err != nil {
    56  		return err
    57  	}
    58  	b.config.tpl.UserVars = b.config.PackerUserVars
    59  	b.config.tpl.Funcs(awscommon.TemplateFuncs)
    60  
    61  	if b.config.BundleDestination == "" {
    62  		b.config.BundleDestination = "/tmp"
    63  	}
    64  
    65  	if b.config.BundlePrefix == "" {
    66  		b.config.BundlePrefix = "image-{{timestamp}}"
    67  	}
    68  
    69  	if b.config.BundleUploadCommand == "" {
    70  		b.config.BundleUploadCommand = "sudo -n ec2-upload-bundle " +
    71  			"-b {{.BucketName}} " +
    72  			"-m {{.ManifestPath}} " +
    73  			"-a {{.AccessKey}} " +
    74  			"-s {{.SecretKey}} " +
    75  			"-d {{.BundleDirectory}} " +
    76  			"--batch " +
    77  			"--retry"
    78  	}
    79  
    80  	if b.config.BundleVolCommand == "" {
    81  		b.config.BundleVolCommand = "sudo -n ec2-bundle-vol " +
    82  			"-k {{.KeyPath}} " +
    83  			"-u {{.AccountId}} " +
    84  			"-c {{.CertPath}} " +
    85  			"-r {{.Architecture}} " +
    86  			"-e {{.PrivatePath}} " +
    87  			"-d {{.Destination}} " +
    88  			"-p {{.Prefix}} " +
    89  			"--batch"
    90  	}
    91  
    92  	if b.config.X509UploadPath == "" {
    93  		b.config.X509UploadPath = "/tmp"
    94  	}
    95  
    96  	// Accumulate any errors
    97  	errs := common.CheckUnusedConfig(md)
    98  	errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(b.config.tpl)...)
    99  	errs = packer.MultiErrorAppend(errs, b.config.AMIConfig.Prepare(b.config.tpl)...)
   100  	errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(b.config.tpl)...)
   101  
   102  	validates := map[string]*string{
   103  		"bundle_upload_command": &b.config.BundleUploadCommand,
   104  		"bundle_vol_command":    &b.config.BundleVolCommand,
   105  	}
   106  
   107  	for n, ptr := range validates {
   108  		if err := b.config.tpl.Validate(*ptr); err != nil {
   109  			errs = packer.MultiErrorAppend(
   110  				errs, fmt.Errorf("Error parsing %s: %s", n, err))
   111  		}
   112  	}
   113  
   114  	templates := map[string]*string{
   115  		"account_id":         &b.config.AccountId,
   116  		"ami_name":           &b.config.AMIName,
   117  		"bundle_destination": &b.config.BundleDestination,
   118  		"bundle_prefix":      &b.config.BundlePrefix,
   119  		"s3_bucket":          &b.config.S3Bucket,
   120  		"x509_cert_path":     &b.config.X509CertPath,
   121  		"x509_key_path":      &b.config.X509KeyPath,
   122  		"x509_upload_path":   &b.config.X509UploadPath,
   123  	}
   124  
   125  	for n, ptr := range templates {
   126  		var err error
   127  		*ptr, err = b.config.tpl.Process(*ptr, nil)
   128  		if err != nil {
   129  			errs = packer.MultiErrorAppend(
   130  				errs, fmt.Errorf("Error processing %s: %s", n, err))
   131  		}
   132  	}
   133  
   134  	if b.config.AccountId == "" {
   135  		errs = packer.MultiErrorAppend(errs, errors.New("account_id is required"))
   136  	} else {
   137  		b.config.AccountId = strings.Replace(b.config.AccountId, "-", "", -1)
   138  	}
   139  
   140  	if b.config.S3Bucket == "" {
   141  		errs = packer.MultiErrorAppend(errs, errors.New("s3_bucket is required"))
   142  	}
   143  
   144  	if b.config.X509CertPath == "" {
   145  		errs = packer.MultiErrorAppend(errs, errors.New("x509_cert_path is required"))
   146  	} else if _, err := os.Stat(b.config.X509CertPath); err != nil {
   147  		errs = packer.MultiErrorAppend(
   148  			errs, fmt.Errorf("x509_cert_path points to bad file: %s", err))
   149  	}
   150  
   151  	if b.config.X509KeyPath == "" {
   152  		errs = packer.MultiErrorAppend(errs, errors.New("x509_key_path is required"))
   153  	} else if _, err := os.Stat(b.config.X509KeyPath); err != nil {
   154  		errs = packer.MultiErrorAppend(
   155  			errs, fmt.Errorf("x509_key_path points to bad file: %s", err))
   156  	}
   157  
   158  	if errs != nil && len(errs.Errors) > 0 {
   159  		return errs
   160  	}
   161  
   162  	log.Printf("Config: %+v", b.config)
   163  	return nil
   164  }
   165  
   166  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
   167  	region, err := b.config.Region()
   168  	if err != nil {
   169  		return nil, err
   170  	}
   171  
   172  	auth, err := b.config.AccessConfig.Auth()
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  
   177  	ec2conn := ec2.New(auth, region)
   178  
   179  	// Setup the state bag and initial state for the steps
   180  	state := new(multistep.BasicStateBag)
   181  	state.Put("config", &b.config)
   182  	state.Put("ec2", ec2conn)
   183  	state.Put("hook", hook)
   184  	state.Put("ui", ui)
   185  
   186  	// Build the steps
   187  	steps := []multistep.Step{
   188  		&awscommon.StepKeyPair{
   189  			Debug:        b.config.PackerDebug,
   190  			DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
   191  			KeyPairName:  b.config.TemporaryKeyPairName,
   192  		},
   193  		&awscommon.StepSecurityGroup{
   194  			SecurityGroupId: b.config.SecurityGroupId,
   195  			SSHPort:         b.config.SSHPort,
   196  			VpcId:           b.config.VpcId,
   197  		},
   198  		&awscommon.StepRunSourceInstance{
   199  			Debug:              b.config.PackerDebug,
   200  			ExpectedRootDevice: "instance-store",
   201  			InstanceType:       b.config.InstanceType,
   202  			IamInstanceProfile: b.config.IamInstanceProfile,
   203  			UserData:           b.config.UserData,
   204  			UserDataFile:       b.config.UserDataFile,
   205  			SourceAMI:          b.config.SourceAmi,
   206  			SubnetId:           b.config.SubnetId,
   207  			BlockDevices:       b.config.BlockDevices,
   208  		},
   209  		&common.StepConnectSSH{
   210  			SSHAddress:     awscommon.SSHAddress(ec2conn, b.config.SSHPort),
   211  			SSHConfig:      awscommon.SSHConfig(b.config.SSHUsername),
   212  			SSHWaitTimeout: b.config.SSHTimeout(),
   213  		},
   214  		&common.StepProvision{},
   215  		&StepUploadX509Cert{},
   216  		&StepBundleVolume{},
   217  		&StepUploadBundle{},
   218  		&StepRegisterAMI{},
   219  		&awscommon.StepAMIRegionCopy{
   220  			Regions: b.config.AMIRegions,
   221  		},
   222  		&awscommon.StepModifyAMIAttributes{
   223  			Description:  b.config.AMIDescription,
   224  			Users:        b.config.AMIUsers,
   225  			Groups:       b.config.AMIGroups,
   226  			ProductCodes: b.config.AMIProductCodes,
   227  		},
   228  		&awscommon.StepCreateTags{
   229  			Tags: b.config.AMITags,
   230  		},
   231  	}
   232  
   233  	// Run!
   234  	if b.config.PackerDebug {
   235  		b.runner = &multistep.DebugRunner{
   236  			Steps:   steps,
   237  			PauseFn: common.MultistepDebugFn(ui),
   238  		}
   239  	} else {
   240  		b.runner = &multistep.BasicRunner{Steps: steps}
   241  	}
   242  
   243  	b.runner.Run(state)
   244  
   245  	// If there was an error, return that
   246  	if rawErr, ok := state.GetOk("error"); ok {
   247  		return nil, rawErr.(error)
   248  	}
   249  
   250  	// If there are no AMIs, then just return
   251  	if _, ok := state.GetOk("amis"); !ok {
   252  		return nil, nil
   253  	}
   254  
   255  	// Build the artifact and return it
   256  	artifact := &awscommon.Artifact{
   257  		Amis:           state.Get("amis").(map[string]string),
   258  		BuilderIdValue: BuilderId,
   259  		Conn:           ec2conn,
   260  	}
   261  
   262  	return artifact, nil
   263  }
   264  
   265  func (b *Builder) Cancel() {
   266  	if b.runner != nil {
   267  		log.Println("Cancelling the step runner...")
   268  		b.runner.Cancel()
   269  	}
   270  }