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