github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/ebsvolume/builder.go (about) 1 // The ebsvolume package contains a packer.Builder implementation that 2 // builds EBS volumes for Amazon EC2 using an ephemeral instance, 3 package ebsvolume 4 5 import ( 6 "fmt" 7 "log" 8 9 "github.com/aws/aws-sdk-go/service/ec2" 10 awscommon "github.com/hashicorp/packer/builder/amazon/common" 11 "github.com/hashicorp/packer/common" 12 "github.com/hashicorp/packer/helper/communicator" 13 "github.com/hashicorp/packer/helper/config" 14 "github.com/hashicorp/packer/helper/multistep" 15 "github.com/hashicorp/packer/packer" 16 "github.com/hashicorp/packer/template/interpolate" 17 ) 18 19 const BuilderId = "mitchellh.amazon.ebsvolume" 20 21 type Config struct { 22 common.PackerConfig `mapstructure:",squash"` 23 awscommon.AccessConfig `mapstructure:",squash"` 24 awscommon.RunConfig `mapstructure:",squash"` 25 26 VolumeMappings []BlockDevice `mapstructure:"ebs_volumes"` 27 AMIENASupport *bool `mapstructure:"ena_support"` 28 AMISriovNetSupport bool `mapstructure:"sriov_support"` 29 30 launchBlockDevices awscommon.BlockDevices 31 ctx interpolate.Context 32 } 33 34 type Builder struct { 35 config Config 36 runner multistep.Runner 37 } 38 39 func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { 40 b.config.ctx.Funcs = awscommon.TemplateFuncs 41 err := config.Decode(&b.config, &config.DecodeOpts{ 42 Interpolate: true, 43 InterpolateContext: &b.config.ctx, 44 InterpolateFilter: &interpolate.RenderFilter{ 45 Exclude: []string{ 46 "run_tags", 47 "spot_tags", 48 "ebs_volumes", 49 }, 50 }, 51 }, raws...) 52 if err != nil { 53 return nil, err 54 } 55 56 // Accumulate any errors 57 var errs *packer.MultiError 58 errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...) 59 errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) 60 errs = packer.MultiErrorAppend(errs, b.config.launchBlockDevices.Prepare(&b.config.ctx)...) 61 62 for _, d := range b.config.VolumeMappings { 63 if err := d.Prepare(&b.config.ctx); err != nil { 64 errs = packer.MultiErrorAppend(errs, fmt.Errorf("AMIMapping: %s", err.Error())) 65 } 66 } 67 68 b.config.launchBlockDevices, err = commonBlockDevices(b.config.VolumeMappings, &b.config.ctx) 69 if err != nil { 70 errs = packer.MultiErrorAppend(errs, err) 71 } 72 73 if b.config.IsSpotInstance() && ((b.config.AMIENASupport != nil && *b.config.AMIENASupport) || b.config.AMISriovNetSupport) { 74 errs = packer.MultiErrorAppend(errs, 75 fmt.Errorf("Spot instances do not support modification, which is required "+ 76 "when either `ena_support` or `sriov_support` are set. Please ensure "+ 77 "you use an AMI that already has either SR-IOV or ENA enabled.")) 78 } 79 80 if errs != nil && len(errs.Errors) > 0 { 81 return nil, errs 82 } 83 84 packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token) 85 return nil, nil 86 } 87 88 func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { 89 session, err := b.config.Session() 90 if err != nil { 91 return nil, err 92 } 93 ec2conn := ec2.New(session) 94 95 // Setup the state bag and initial state for the steps 96 state := new(multistep.BasicStateBag) 97 state.Put("config", &b.config) 98 state.Put("ec2", ec2conn) 99 state.Put("hook", hook) 100 state.Put("ui", ui) 101 102 var instanceStep multistep.Step 103 104 if b.config.IsSpotInstance() { 105 instanceStep = &awscommon.StepRunSpotInstance{ 106 AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, 107 BlockDevices: b.config.launchBlockDevices, 108 BlockDurationMinutes: b.config.BlockDurationMinutes, 109 Ctx: b.config.ctx, 110 Comm: &b.config.RunConfig.Comm, 111 Debug: b.config.PackerDebug, 112 EbsOptimized: b.config.EbsOptimized, 113 ExpectedRootDevice: "ebs", 114 IamInstanceProfile: b.config.IamInstanceProfile, 115 InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior, 116 InstanceType: b.config.InstanceType, 117 SourceAMI: b.config.SourceAmi, 118 SpotPrice: b.config.SpotPrice, 119 SpotPriceProduct: b.config.SpotPriceAutoProduct, 120 SpotTags: b.config.SpotTags, 121 Tags: b.config.RunTags, 122 UserData: b.config.UserData, 123 UserDataFile: b.config.UserDataFile, 124 } 125 } else { 126 instanceStep = &awscommon.StepRunSourceInstance{ 127 AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, 128 BlockDevices: b.config.launchBlockDevices, 129 Comm: &b.config.RunConfig.Comm, 130 Ctx: b.config.ctx, 131 Debug: b.config.PackerDebug, 132 EbsOptimized: b.config.EbsOptimized, 133 EnableT2Unlimited: b.config.EnableT2Unlimited, 134 ExpectedRootDevice: "ebs", 135 IamInstanceProfile: b.config.IamInstanceProfile, 136 InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior, 137 InstanceType: b.config.InstanceType, 138 IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(), 139 SourceAMI: b.config.SourceAmi, 140 Tags: b.config.RunTags, 141 UserData: b.config.UserData, 142 UserDataFile: b.config.UserDataFile, 143 } 144 } 145 146 // Build the steps 147 steps := []multistep.Step{ 148 &awscommon.StepSourceAMIInfo{ 149 SourceAmi: b.config.SourceAmi, 150 EnableAMISriovNetSupport: b.config.AMISriovNetSupport, 151 EnableAMIENASupport: b.config.AMIENASupport, 152 AmiFilters: b.config.SourceAmiFilter, 153 }, 154 &awscommon.StepNetworkInfo{ 155 VpcId: b.config.VpcId, 156 VpcFilter: b.config.VpcFilter, 157 SecurityGroupIds: b.config.SecurityGroupIds, 158 SecurityGroupFilter: b.config.SecurityGroupFilter, 159 SubnetId: b.config.SubnetId, 160 SubnetFilter: b.config.SubnetFilter, 161 AvailabilityZone: b.config.AvailabilityZone, 162 }, 163 &awscommon.StepKeyPair{ 164 Debug: b.config.PackerDebug, 165 Comm: &b.config.RunConfig.Comm, 166 DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), 167 }, 168 &awscommon.StepSecurityGroup{ 169 SecurityGroupFilter: b.config.SecurityGroupFilter, 170 SecurityGroupIds: b.config.SecurityGroupIds, 171 CommConfig: &b.config.RunConfig.Comm, 172 TemporarySGSourceCidr: b.config.TemporarySGSourceCidr, 173 }, 174 instanceStep, 175 &stepTagEBSVolumes{ 176 VolumeMapping: b.config.VolumeMappings, 177 Ctx: b.config.ctx, 178 }, 179 &awscommon.StepGetPassword{ 180 Debug: b.config.PackerDebug, 181 Comm: &b.config.RunConfig.Comm, 182 Timeout: b.config.WindowsPasswordTimeout, 183 BuildName: b.config.PackerBuildName, 184 }, 185 &communicator.StepConnect{ 186 Config: &b.config.RunConfig.Comm, 187 Host: awscommon.SSHHost( 188 ec2conn, 189 b.config.Comm.SSHInterface), 190 SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(), 191 }, 192 &common.StepProvision{}, 193 &common.StepCleanupTempKeys{ 194 Comm: &b.config.RunConfig.Comm, 195 }, 196 &awscommon.StepStopEBSBackedInstance{ 197 Skip: b.config.IsSpotInstance(), 198 DisableStopInstance: b.config.DisableStopInstance, 199 }, 200 &awscommon.StepModifyEBSBackedInstance{ 201 EnableAMISriovNetSupport: b.config.AMISriovNetSupport, 202 EnableAMIENASupport: b.config.AMIENASupport, 203 }, 204 } 205 206 // Run! 207 b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) 208 b.runner.Run(state) 209 210 // If there was an error, return that 211 if rawErr, ok := state.GetOk("error"); ok { 212 return nil, rawErr.(error) 213 } 214 215 // Build the artifact and return it 216 artifact := &Artifact{ 217 Volumes: state.Get("ebsvolumes").(EbsVolumes), 218 BuilderIdValue: BuilderId, 219 Conn: ec2conn, 220 } 221 ui.Say(fmt.Sprintf("Created Volumes: %s", artifact)) 222 return artifact, nil 223 } 224 225 func (b *Builder) Cancel() { 226 if b.runner != nil { 227 log.Println("Cancelling the step runner...") 228 b.runner.Cancel() 229 } 230 }