github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/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, err := session.NewSession(config) 164 if err != nil { 165 return nil, err 166 } 167 ec2conn := ec2.New(session) 168 169 // If the subnet is specified but not the AZ, try to determine the AZ automatically 170 if b.config.SubnetId != "" && b.config.AvailabilityZone == "" { 171 log.Printf("[INFO] Finding AZ for the given subnet '%s'", b.config.SubnetId) 172 resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}}) 173 if err != nil { 174 return nil, err 175 } 176 b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone 177 log.Printf("[INFO] AZ found: '%s'", b.config.AvailabilityZone) 178 } 179 180 // Setup the state bag and initial state for the steps 181 state := new(multistep.BasicStateBag) 182 state.Put("config", &b.config) 183 state.Put("ec2", ec2conn) 184 state.Put("hook", hook) 185 state.Put("ui", ui) 186 187 // Build the steps 188 steps := []multistep.Step{ 189 &awscommon.StepPreValidate{ 190 DestAmiName: b.config.AMIName, 191 ForceDeregister: b.config.AMIForceDeregister, 192 }, 193 &awscommon.StepSourceAMIInfo{ 194 SourceAmi: b.config.SourceAmi, 195 EnhancedNetworking: b.config.AMIEnhancedNetworking, 196 AmiFilters: b.config.SourceAmiFilter, 197 }, 198 &awscommon.StepKeyPair{ 199 Debug: b.config.PackerDebug, 200 DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), 201 KeyPairName: b.config.SSHKeyPairName, 202 PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey, 203 TemporaryKeyPairName: b.config.TemporaryKeyPairName, 204 }, 205 &awscommon.StepSecurityGroup{ 206 CommConfig: &b.config.RunConfig.Comm, 207 SecurityGroupIds: b.config.SecurityGroupIds, 208 VpcId: b.config.VpcId, 209 }, 210 &awscommon.StepRunSourceInstance{ 211 Debug: b.config.PackerDebug, 212 SpotPrice: b.config.SpotPrice, 213 SpotPriceProduct: b.config.SpotPriceAutoProduct, 214 InstanceType: b.config.InstanceType, 215 IamInstanceProfile: b.config.IamInstanceProfile, 216 UserData: b.config.UserData, 217 UserDataFile: b.config.UserDataFile, 218 SourceAMI: b.config.SourceAmi, 219 SubnetId: b.config.SubnetId, 220 AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, 221 EbsOptimized: b.config.EbsOptimized, 222 AvailabilityZone: b.config.AvailabilityZone, 223 BlockDevices: b.config.BlockDevices, 224 Tags: b.config.RunTags, 225 }, 226 &awscommon.StepGetPassword{ 227 Debug: b.config.PackerDebug, 228 Comm: &b.config.RunConfig.Comm, 229 Timeout: b.config.WindowsPasswordTimeout, 230 }, 231 &communicator.StepConnect{ 232 Config: &b.config.RunConfig.Comm, 233 Host: awscommon.SSHHost( 234 ec2conn, 235 b.config.SSHPrivateIp), 236 SSHConfig: awscommon.SSHConfig( 237 b.config.RunConfig.Comm.SSHAgentAuth, 238 b.config.RunConfig.Comm.SSHUsername, 239 b.config.RunConfig.Comm.SSHPassword), 240 }, 241 &common.StepProvision{}, 242 &StepUploadX509Cert{}, 243 &StepBundleVolume{ 244 Debug: b.config.PackerDebug, 245 }, 246 &StepUploadBundle{ 247 Debug: b.config.PackerDebug, 248 }, 249 &awscommon.StepDeregisterAMI{ 250 ForceDeregister: b.config.AMIForceDeregister, 251 AMIName: b.config.AMIName, 252 }, 253 &StepRegisterAMI{}, 254 &awscommon.StepAMIRegionCopy{ 255 AccessConfig: &b.config.AccessConfig, 256 Regions: b.config.AMIRegions, 257 Name: b.config.AMIName, 258 }, 259 &awscommon.StepModifyAMIAttributes{ 260 Description: b.config.AMIDescription, 261 Users: b.config.AMIUsers, 262 Groups: b.config.AMIGroups, 263 ProductCodes: b.config.AMIProductCodes, 264 }, 265 &awscommon.StepCreateTags{ 266 Tags: b.config.AMITags, 267 }, 268 } 269 270 // Run! 271 b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) 272 b.runner.Run(state) 273 274 // If there was an error, return that 275 if rawErr, ok := state.GetOk("error"); ok { 276 return nil, rawErr.(error) 277 } 278 279 // If there are no AMIs, then just return 280 if _, ok := state.GetOk("amis"); !ok { 281 return nil, nil 282 } 283 284 // Build the artifact and return it 285 artifact := &awscommon.Artifact{ 286 Amis: state.Get("amis").(map[string]string), 287 BuilderIdValue: BuilderId, 288 Conn: ec2conn, 289 } 290 291 return artifact, nil 292 } 293 294 func (b *Builder) Cancel() { 295 if b.runner != nil { 296 log.Println("Cancelling the step runner...") 297 b.runner.Cancel() 298 } 299 }