github.com/ttysteale/packer@v0.8.2-0.20150708160520-e5f8ea386ed8/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 "github.com/mitchellh/multistep" 14 awscommon "github.com/mitchellh/packer/builder/amazon/common" 15 "github.com/mitchellh/packer/common" 16 "github.com/mitchellh/packer/helper/communicator" 17 "github.com/mitchellh/packer/helper/config" 18 "github.com/mitchellh/packer/packer" 19 "github.com/mitchellh/packer/template/interpolate" 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 "bundle_upload_command", 66 "bundle_vol_command", 67 }, 68 }, 69 }, configs...) 70 if err != nil { 71 return nil, err 72 } 73 74 if b.config.BundleDestination == "" { 75 b.config.BundleDestination = "/tmp" 76 } 77 78 if b.config.BundleUploadCommand == "" { 79 if b.config.IamInstanceProfile != "" { 80 b.config.BundleUploadCommand = "sudo -i -n ec2-upload-bundle " + 81 "-b {{.BucketName}} " + 82 "-m {{.ManifestPath}} " + 83 "-d {{.BundleDirectory}} " + 84 "--batch " + 85 "--region {{.Region}} " + 86 "--retry" 87 } else { 88 b.config.BundleUploadCommand = "sudo -i -n ec2-upload-bundle " + 89 "-b {{.BucketName}} " + 90 "-m {{.ManifestPath}} " + 91 "-a {{.AccessKey}} " + 92 "-s {{.SecretKey}} " + 93 "-d {{.BundleDirectory}} " + 94 "--batch " + 95 "--region {{.Region}} " + 96 "--retry" 97 } 98 } 99 100 if b.config.BundleVolCommand == "" { 101 b.config.BundleVolCommand = "sudo -i -n ec2-bundle-vol " + 102 "-k {{.KeyPath}} " + 103 "-u {{.AccountId}} " + 104 "-c {{.CertPath}} " + 105 "-r {{.Architecture}} " + 106 "-e {{.PrivatePath}}/* " + 107 "-d {{.Destination}} " + 108 "-p {{.Prefix}} " + 109 "--batch " + 110 "--no-filter" 111 } 112 113 if b.config.X509UploadPath == "" { 114 b.config.X509UploadPath = "/tmp" 115 } 116 117 // Accumulate any errors 118 var errs *packer.MultiError 119 errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...) 120 errs = packer.MultiErrorAppend(errs, b.config.BlockDevices.Prepare(&b.config.ctx)...) 121 errs = packer.MultiErrorAppend(errs, b.config.AMIConfig.Prepare(&b.config.ctx)...) 122 errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) 123 124 if b.config.AccountId == "" { 125 errs = packer.MultiErrorAppend(errs, errors.New("account_id is required")) 126 } else { 127 b.config.AccountId = strings.Replace(b.config.AccountId, "-", "", -1) 128 } 129 130 if b.config.S3Bucket == "" { 131 errs = packer.MultiErrorAppend(errs, errors.New("s3_bucket is required")) 132 } 133 134 if b.config.X509CertPath == "" { 135 errs = packer.MultiErrorAppend(errs, errors.New("x509_cert_path is required")) 136 } else if _, err := os.Stat(b.config.X509CertPath); err != nil { 137 errs = packer.MultiErrorAppend( 138 errs, fmt.Errorf("x509_cert_path points to bad file: %s", err)) 139 } 140 141 if b.config.X509KeyPath == "" { 142 errs = packer.MultiErrorAppend(errs, errors.New("x509_key_path is required")) 143 } else if _, err := os.Stat(b.config.X509KeyPath); err != nil { 144 errs = packer.MultiErrorAppend( 145 errs, fmt.Errorf("x509_key_path points to bad file: %s", err)) 146 } 147 148 if errs != nil && len(errs.Errors) > 0 { 149 return nil, errs 150 } 151 152 log.Println(common.ScrubConfig(b.config, b.config.AccessKey, b.config.SecretKey)) 153 return nil, nil 154 } 155 156 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 157 config, err := b.config.Config() 158 if err != nil { 159 return nil, err 160 } 161 162 ec2conn := ec2.New(config) 163 164 // Setup the state bag and initial state for the steps 165 state := new(multistep.BasicStateBag) 166 state.Put("config", &b.config) 167 state.Put("ec2", ec2conn) 168 state.Put("hook", hook) 169 state.Put("ui", ui) 170 171 // Build the steps 172 steps := []multistep.Step{ 173 &awscommon.StepPreValidate{ 174 DestAmiName: b.config.AMIName, 175 ForceDeregister: b.config.AMIForceDeregister, 176 }, 177 &awscommon.StepSourceAMIInfo{ 178 SourceAmi: b.config.SourceAmi, 179 EnhancedNetworking: b.config.AMIEnhancedNetworking, 180 }, 181 &awscommon.StepKeyPair{ 182 Debug: b.config.PackerDebug, 183 DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), 184 KeyPairName: b.config.SSHKeyPairName, 185 PrivateKeyFile: b.config.RunConfig.Comm.SSHPrivateKey, 186 TemporaryKeyPairName: b.config.TemporaryKeyPairName, 187 }, 188 &awscommon.StepSecurityGroup{ 189 CommConfig: &b.config.RunConfig.Comm, 190 SecurityGroupIds: b.config.SecurityGroupIds, 191 VpcId: b.config.VpcId, 192 }, 193 &awscommon.StepRunSourceInstance{ 194 Debug: b.config.PackerDebug, 195 SpotPrice: b.config.SpotPrice, 196 SpotPriceProduct: b.config.SpotPriceAutoProduct, 197 InstanceType: b.config.InstanceType, 198 IamInstanceProfile: b.config.IamInstanceProfile, 199 UserData: b.config.UserData, 200 UserDataFile: b.config.UserDataFile, 201 SourceAMI: b.config.SourceAmi, 202 SubnetId: b.config.SubnetId, 203 AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, 204 AvailabilityZone: b.config.AvailabilityZone, 205 BlockDevices: b.config.BlockDevices, 206 Tags: b.config.RunTags, 207 }, 208 &awscommon.StepGetPassword{ 209 Debug: b.config.PackerDebug, 210 Comm: &b.config.RunConfig.Comm, 211 Timeout: b.config.WindowsPasswordTimeout, 212 }, 213 &communicator.StepConnect{ 214 Config: &b.config.RunConfig.Comm, 215 Host: awscommon.SSHHost( 216 ec2conn, 217 b.config.SSHPrivateIp), 218 SSHConfig: awscommon.SSHConfig( 219 b.config.RunConfig.Comm.SSHUsername), 220 }, 221 &common.StepProvision{}, 222 &StepUploadX509Cert{}, 223 &StepBundleVolume{ 224 Debug: b.config.PackerDebug, 225 }, 226 &StepUploadBundle{ 227 Debug: b.config.PackerDebug, 228 }, 229 &awscommon.StepDeregisterAMI{ 230 ForceDeregister: b.config.AMIForceDeregister, 231 AMIName: b.config.AMIName, 232 }, 233 &StepRegisterAMI{}, 234 &awscommon.StepAMIRegionCopy{ 235 AccessConfig: &b.config.AccessConfig, 236 Regions: b.config.AMIRegions, 237 Name: b.config.AMIName, 238 }, 239 &awscommon.StepModifyAMIAttributes{ 240 Description: b.config.AMIDescription, 241 Users: b.config.AMIUsers, 242 Groups: b.config.AMIGroups, 243 ProductCodes: b.config.AMIProductCodes, 244 }, 245 &awscommon.StepCreateTags{ 246 Tags: b.config.AMITags, 247 }, 248 } 249 250 // Run! 251 if b.config.PackerDebug { 252 b.runner = &multistep.DebugRunner{ 253 Steps: steps, 254 PauseFn: common.MultistepDebugFn(ui), 255 } 256 } else { 257 b.runner = &multistep.BasicRunner{Steps: steps} 258 } 259 260 b.runner.Run(state) 261 262 // If there was an error, return that 263 if rawErr, ok := state.GetOk("error"); ok { 264 return nil, rawErr.(error) 265 } 266 267 // If there are no AMIs, then just return 268 if _, ok := state.GetOk("amis"); !ok { 269 return nil, nil 270 } 271 272 // Build the artifact and return it 273 artifact := &awscommon.Artifact{ 274 Amis: state.Get("amis").(map[string]string), 275 BuilderIdValue: BuilderId, 276 Conn: ec2conn, 277 } 278 279 return artifact, nil 280 } 281 282 func (b *Builder) Cancel() { 283 if b.runner != nil { 284 log.Println("Cancelling the step runner...") 285 b.runner.Cancel() 286 } 287 }