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