github.com/rothwerx/packer@v0.9.0/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  	"log"
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/aws/aws-sdk-go/aws/session"
    13  	"github.com/aws/aws-sdk-go/service/ec2"
    14  	"github.com/mitchellh/multistep"
    15  	awscommon "github.com/mitchellh/packer/builder/amazon/common"
    16  	"github.com/mitchellh/packer/common"
    17  	"github.com/mitchellh/packer/helper/communicator"
    18  	"github.com/mitchellh/packer/helper/config"
    19  	"github.com/mitchellh/packer/packer"
    20  	"github.com/mitchellh/packer/template/interpolate"
    21  )
    22  
    23  // The unique ID for this builder
    24  const BuilderId = "mitchellh.amazon.instance"
    25  
    26  // Config is the configuration that is chained through the steps and
    27  // settable from the template.
    28  type Config struct {
    29  	common.PackerConfig    `mapstructure:",squash"`
    30  	awscommon.AccessConfig `mapstructure:",squash"`
    31  	awscommon.AMIConfig    `mapstructure:",squash"`
    32  	awscommon.BlockDevices `mapstructure:",squash"`
    33  	awscommon.RunConfig    `mapstructure:",squash"`
    34  
    35  	AccountId           string `mapstructure:"account_id"`
    36  	BundleDestination   string `mapstructure:"bundle_destination"`
    37  	BundlePrefix        string `mapstructure:"bundle_prefix"`
    38  	BundleUploadCommand string `mapstructure:"bundle_upload_command"`
    39  	BundleVolCommand    string `mapstructure:"bundle_vol_command"`
    40  	S3Bucket            string `mapstructure:"s3_bucket"`
    41  	X509CertPath        string `mapstructure:"x509_cert_path"`
    42  	X509KeyPath         string `mapstructure:"x509_key_path"`
    43  	X509UploadPath      string `mapstructure:"x509_upload_path"`
    44  
    45  	ctx interpolate.Context
    46  }
    47  
    48  type Builder struct {
    49  	config Config
    50  	runner multistep.Runner
    51  }
    52  
    53  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    54  	configs := make([]interface{}, len(raws)+1)
    55  	configs[0] = map[string]interface{}{
    56  		"bundle_prefix": "image-{{timestamp}}",
    57  	}
    58  	copy(configs[1:], raws)
    59  
    60  	b.config.ctx.Funcs = awscommon.TemplateFuncs
    61  	err := config.Decode(&b.config, &config.DecodeOpts{
    62  		Interpolate:        true,
    63  		InterpolateContext: &b.config.ctx,
    64  		InterpolateFilter: &interpolate.RenderFilter{
    65  			Exclude: []string{
    66  				"bundle_upload_command",
    67  				"bundle_vol_command",
    68  			},
    69  		},
    70  	}, configs...)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	if b.config.BundleDestination == "" {
    76  		b.config.BundleDestination = "/tmp"
    77  	}
    78  
    79  	if b.config.BundleUploadCommand == "" {
    80  		if b.config.IamInstanceProfile != "" {
    81  			b.config.BundleUploadCommand = "sudo -i -n ec2-upload-bundle " +
    82  				"-b {{.BucketName}} " +
    83  				"-m {{.ManifestPath}} " +
    84  				"-d {{.BundleDirectory}} " +
    85  				"--batch " +
    86  				"--region {{.Region}} " +
    87  				"--retry"
    88  		} else {
    89  			b.config.BundleUploadCommand = "sudo -i -n ec2-upload-bundle " +
    90  				"-b {{.BucketName}} " +
    91  				"-m {{.ManifestPath}} " +
    92  				"-a {{.AccessKey}} " +
    93  				"-s {{.SecretKey}} " +
    94  				"-d {{.BundleDirectory}} " +
    95  				"--batch " +
    96  				"--region {{.Region}} " +
    97  				"--retry"
    98  		}
    99  	}
   100  
   101  	if b.config.BundleVolCommand == "" {
   102  		b.config.BundleVolCommand = "sudo -i -n ec2-bundle-vol " +
   103  			"-k {{.KeyPath}} " +
   104  			"-u {{.AccountId}} " +
   105  			"-c {{.CertPath}} " +
   106  			"-r {{.Architecture}} " +
   107  			"-e {{.PrivatePath}}/* " +
   108  			"-d {{.Destination}} " +
   109  			"-p {{.Prefix}} " +
   110  			"--batch " +
   111  			"--no-filter"
   112  	}
   113  
   114  	if b.config.X509UploadPath == "" {
   115  		b.config.X509UploadPath = "/tmp"
   116  	}
   117  
   118  	// Accumulate any errors
   119  	var errs *packer.MultiError
   120  	errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
   121  	errs = packer.MultiErrorAppend(errs, b.config.BlockDevices.Prepare(&b.config.ctx)...)
   122  	errs = packer.MultiErrorAppend(errs, b.config.AMIConfig.Prepare(&b.config.ctx)...)
   123  	errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
   124  
   125  	if b.config.AccountId == "" {
   126  		errs = packer.MultiErrorAppend(errs, errors.New("account_id is required"))
   127  	} else {
   128  		b.config.AccountId = strings.Replace(b.config.AccountId, "-", "", -1)
   129  	}
   130  
   131  	if b.config.S3Bucket == "" {
   132  		errs = packer.MultiErrorAppend(errs, errors.New("s3_bucket is required"))
   133  	}
   134  
   135  	if b.config.X509CertPath == "" {
   136  		errs = packer.MultiErrorAppend(errs, errors.New("x509_cert_path is required"))
   137  	} else if _, err := os.Stat(b.config.X509CertPath); err != nil {
   138  		errs = packer.MultiErrorAppend(
   139  			errs, fmt.Errorf("x509_cert_path points to bad file: %s", err))
   140  	}
   141  
   142  	if b.config.X509KeyPath == "" {
   143  		errs = packer.MultiErrorAppend(errs, errors.New("x509_key_path is required"))
   144  	} else if _, err := os.Stat(b.config.X509KeyPath); err != nil {
   145  		errs = packer.MultiErrorAppend(
   146  			errs, fmt.Errorf("x509_key_path points to bad file: %s", err))
   147  	}
   148  
   149  	if errs != nil && len(errs.Errors) > 0 {
   150  		return nil, errs
   151  	}
   152  
   153  	log.Println(common.ScrubConfig(b.config, b.config.AccessKey, b.config.SecretKey))
   154  	return nil, nil
   155  }
   156  
   157  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
   158  	config, err := b.config.Config()
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	session := session.New(config)
   164  	ec2conn := ec2.New(session)
   165  
   166  	// If the subnet is specified but not the AZ, try to determine the AZ automatically
   167  	if b.config.SubnetId != "" && b.config.AvailabilityZone == "" {
   168  		log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId)
   169  		resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}})
   170  		if err != nil {
   171  			return nil, err
   172  		}
   173  		b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone
   174  		log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone)
   175  	}
   176  
   177  	// Setup the state bag and initial state for the steps
   178  	state := new(multistep.BasicStateBag)
   179  	state.Put("config", &b.config)
   180  	state.Put("ec2", ec2conn)
   181  	state.Put("hook", hook)
   182  	state.Put("ui", ui)
   183  
   184  	// Build the steps
   185  	steps := []multistep.Step{
   186  		&awscommon.StepPreValidate{
   187  			DestAmiName:     b.config.AMIName,
   188  			ForceDeregister: b.config.AMIForceDeregister,
   189  		},
   190  		&awscommon.StepSourceAMIInfo{
   191  			SourceAmi:          b.config.SourceAmi,
   192  			EnhancedNetworking: b.config.AMIEnhancedNetworking,
   193  		},
   194  		&awscommon.StepKeyPair{
   195  			Debug:                b.config.PackerDebug,
   196  			DebugKeyPath:         fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
   197  			KeyPairName:          b.config.SSHKeyPairName,
   198  			PrivateKeyFile:       b.config.RunConfig.Comm.SSHPrivateKey,
   199  			TemporaryKeyPairName: b.config.TemporaryKeyPairName,
   200  		},
   201  		&awscommon.StepSecurityGroup{
   202  			CommConfig:       &b.config.RunConfig.Comm,
   203  			SecurityGroupIds: b.config.SecurityGroupIds,
   204  			VpcId:            b.config.VpcId,
   205  		},
   206  		&awscommon.StepRunSourceInstance{
   207  			Debug:                    b.config.PackerDebug,
   208  			SpotPrice:                b.config.SpotPrice,
   209  			SpotPriceProduct:         b.config.SpotPriceAutoProduct,
   210  			InstanceType:             b.config.InstanceType,
   211  			IamInstanceProfile:       b.config.IamInstanceProfile,
   212  			UserData:                 b.config.UserData,
   213  			UserDataFile:             b.config.UserDataFile,
   214  			SourceAMI:                b.config.SourceAmi,
   215  			SubnetId:                 b.config.SubnetId,
   216  			AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
   217  			EbsOptimized:             b.config.EbsOptimized,
   218  			AvailabilityZone:         b.config.AvailabilityZone,
   219  			BlockDevices:             b.config.BlockDevices,
   220  			Tags:                     b.config.RunTags,
   221  		},
   222  		&awscommon.StepGetPassword{
   223  			Debug:   b.config.PackerDebug,
   224  			Comm:    &b.config.RunConfig.Comm,
   225  			Timeout: b.config.WindowsPasswordTimeout,
   226  		},
   227  		&communicator.StepConnect{
   228  			Config: &b.config.RunConfig.Comm,
   229  			Host: awscommon.SSHHost(
   230  				ec2conn,
   231  				b.config.SSHPrivateIp),
   232  			SSHConfig: awscommon.SSHConfig(
   233  				b.config.RunConfig.Comm.SSHUsername),
   234  		},
   235  		&common.StepProvision{},
   236  		&StepUploadX509Cert{},
   237  		&StepBundleVolume{
   238  			Debug: b.config.PackerDebug,
   239  		},
   240  		&StepUploadBundle{
   241  			Debug: b.config.PackerDebug,
   242  		},
   243  		&awscommon.StepDeregisterAMI{
   244  			ForceDeregister: b.config.AMIForceDeregister,
   245  			AMIName:         b.config.AMIName,
   246  		},
   247  		&StepRegisterAMI{},
   248  		&awscommon.StepAMIRegionCopy{
   249  			AccessConfig: &b.config.AccessConfig,
   250  			Regions:      b.config.AMIRegions,
   251  			Name:         b.config.AMIName,
   252  		},
   253  		&awscommon.StepModifyAMIAttributes{
   254  			Description:  b.config.AMIDescription,
   255  			Users:        b.config.AMIUsers,
   256  			Groups:       b.config.AMIGroups,
   257  			ProductCodes: b.config.AMIProductCodes,
   258  		},
   259  		&awscommon.StepCreateTags{
   260  			Tags: b.config.AMITags,
   261  		},
   262  	}
   263  
   264  	// Run!
   265  	if b.config.PackerDebug {
   266  		b.runner = &multistep.DebugRunner{
   267  			Steps:   steps,
   268  			PauseFn: common.MultistepDebugFn(ui),
   269  		}
   270  	} else {
   271  		b.runner = &multistep.BasicRunner{Steps: steps}
   272  	}
   273  
   274  	b.runner.Run(state)
   275  
   276  	// If there was an error, return that
   277  	if rawErr, ok := state.GetOk("error"); ok {
   278  		return nil, rawErr.(error)
   279  	}
   280  
   281  	// If there are no AMIs, then just return
   282  	if _, ok := state.GetOk("amis"); !ok {
   283  		return nil, nil
   284  	}
   285  
   286  	// Build the artifact and return it
   287  	artifact := &awscommon.Artifact{
   288  		Amis:           state.Get("amis").(map[string]string),
   289  		BuilderIdValue: BuilderId,
   290  		Conn:           ec2conn,
   291  	}
   292  
   293  	return artifact, nil
   294  }
   295  
   296  func (b *Builder) Cancel() {
   297  	if b.runner != nil {
   298  		log.Println("Cancelling the step runner...")
   299  		b.runner.Cancel()
   300  	}
   301  }