github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/builder/amazon/ebs/builder.go (about) 1 // The amazonebs package contains a packer.Builder implementation that 2 // builds AMIs for Amazon EC2. 3 // 4 // In general, there are two types of AMIs that can be created: ebs-backed or 5 // instance-store. This builder _only_ builds ebs-backed images. 6 package ebs 7 8 import ( 9 "fmt" 10 "github.com/mitchellh/goamz/ec2" 11 "github.com/mitchellh/multistep" 12 awscommon "github.com/mitchellh/packer/builder/amazon/common" 13 "github.com/mitchellh/packer/common" 14 "github.com/mitchellh/packer/packer" 15 "log" 16 ) 17 18 // The unique ID for this builder 19 const BuilderId = "mitchellh.amazonebs" 20 21 type config struct { 22 common.PackerConfig `mapstructure:",squash"` 23 awscommon.AccessConfig `mapstructure:",squash"` 24 awscommon.AMIConfig `mapstructure:",squash"` 25 awscommon.BlockDevices `mapstructure:",squash"` 26 awscommon.RunConfig `mapstructure:",squash"` 27 28 tpl *packer.ConfigTemplate 29 } 30 31 type Builder struct { 32 config config 33 runner multistep.Runner 34 } 35 36 func (b *Builder) Prepare(raws ...interface{}) error { 37 md, err := common.DecodeConfig(&b.config, raws...) 38 if err != nil { 39 return err 40 } 41 42 b.config.tpl, err = packer.NewConfigTemplate() 43 if err != nil { 44 return err 45 } 46 b.config.tpl.UserVars = b.config.PackerUserVars 47 b.config.tpl.Funcs(awscommon.TemplateFuncs) 48 49 // Accumulate any errors 50 errs := common.CheckUnusedConfig(md) 51 errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(b.config.tpl)...) 52 errs = packer.MultiErrorAppend(errs, b.config.AMIConfig.Prepare(b.config.tpl)...) 53 errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(b.config.tpl)...) 54 55 if errs != nil && len(errs.Errors) > 0 { 56 return errs 57 } 58 59 log.Println(common.ScrubConfig(b.config), b.config.AccessKey, b.config.SecretKey) 60 return nil 61 } 62 63 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 64 region, err := b.config.Region() 65 if err != nil { 66 return nil, err 67 } 68 69 auth, err := b.config.AccessConfig.Auth() 70 if err != nil { 71 return nil, err 72 } 73 74 ec2conn := ec2.New(auth, region) 75 76 // Setup the state bag and initial state for the steps 77 state := new(multistep.BasicStateBag) 78 state.Put("config", b.config) 79 state.Put("ec2", ec2conn) 80 state.Put("hook", hook) 81 state.Put("ui", ui) 82 83 // Build the steps 84 steps := []multistep.Step{ 85 &awscommon.StepKeyPair{ 86 Debug: b.config.PackerDebug, 87 DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), 88 KeyPairName: b.config.TemporaryKeyPairName, 89 }, 90 &awscommon.StepSecurityGroup{ 91 SecurityGroupId: b.config.SecurityGroupId, 92 SSHPort: b.config.SSHPort, 93 VpcId: b.config.VpcId, 94 }, 95 &awscommon.StepRunSourceInstance{ 96 Debug: b.config.PackerDebug, 97 ExpectedRootDevice: "ebs", 98 InstanceType: b.config.InstanceType, 99 UserData: b.config.UserData, 100 UserDataFile: b.config.UserDataFile, 101 SourceAMI: b.config.SourceAmi, 102 IamInstanceProfile: b.config.IamInstanceProfile, 103 SubnetId: b.config.SubnetId, 104 BlockDevices: b.config.BlockDevices, 105 }, 106 &common.StepConnectSSH{ 107 SSHAddress: awscommon.SSHAddress(ec2conn, b.config.SSHPort), 108 SSHConfig: awscommon.SSHConfig(b.config.SSHUsername), 109 SSHWaitTimeout: b.config.SSHTimeout(), 110 }, 111 &common.StepProvision{}, 112 &stepStopInstance{}, 113 &stepCreateAMI{}, 114 &awscommon.StepAMIRegionCopy{ 115 Regions: b.config.AMIRegions, 116 }, 117 &awscommon.StepModifyAMIAttributes{ 118 Description: b.config.AMIDescription, 119 Users: b.config.AMIUsers, 120 Groups: b.config.AMIGroups, 121 }, 122 &awscommon.StepCreateTags{ 123 Tags: b.config.AMITags, 124 }, 125 } 126 127 // Run! 128 if b.config.PackerDebug { 129 b.runner = &multistep.DebugRunner{ 130 Steps: steps, 131 PauseFn: common.MultistepDebugFn(ui), 132 } 133 } else { 134 b.runner = &multistep.BasicRunner{Steps: steps} 135 } 136 137 b.runner.Run(state) 138 139 // If there was an error, return that 140 if rawErr, ok := state.GetOk("error"); ok { 141 return nil, rawErr.(error) 142 } 143 144 // If there are no AMIs, then just return 145 if _, ok := state.GetOk("amis"); !ok { 146 return nil, nil 147 } 148 149 // Build the artifact and return it 150 artifact := &awscommon.Artifact{ 151 Amis: state.Get("amis").(map[string]string), 152 BuilderIdValue: BuilderId, 153 Conn: ec2conn, 154 } 155 156 return artifact, nil 157 } 158 159 func (b *Builder) Cancel() { 160 if b.runner != nil { 161 log.Println("Cancelling the step runner...") 162 b.runner.Cancel() 163 } 164 }