fix long line format

This commit is contained in:
zhuzhih2017 2017-05-26 10:59:53 +08:00
parent 5e7e5a419d
commit b2aa40655b
10 changed files with 76 additions and 40 deletions

View File

@ -56,7 +56,8 @@ func (c *AlicloudAccessConfig) Config() error {
c.AlicloudSecretKey = os.Getenv("ALICLOUD_SECRET_KEY")
}
if c.AlicloudAccessKey == "" || c.AlicloudSecretKey == "" {
return fmt.Errorf("ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY must set in template file or environment variables")
return fmt.Errorf("ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY must set in template file " +
"or environment variables")
}
return nil

View File

@ -212,7 +212,8 @@ func (b *Builder) Cancel() {
func (b *Builder) chooseNetworkType() InstanceNetWork {
//Alicloud userdata require vpc network and public key require userdata, so besides user specific vpc network,
//choose vpc networks in those cases
if b.config.RunConfig.Comm.SSHPrivateKey != "" || b.config.UserData != "" || b.config.UserDataFile != "" || b.config.VpcId != "" || b.config.VSwitchId != "" || b.config.TemporaryKeyPairName != "" {
if b.config.RunConfig.Comm.SSHPrivateKey != "" || b.config.UserData != "" || b.config.UserDataFile != "" ||
b.config.VpcId != "" || b.config.VSwitchId != "" || b.config.TemporaryKeyPairName != "" {
return VpcNet
} else {
return ClassicNet

View File

@ -123,7 +123,8 @@ func checkSnapshotsDeleted(snapshotIds []string) builderT.TestCheckFunc {
return fmt.Errorf("Query snapshot failed %v", err)
}
if len(snapshotResp) > 0 {
return fmt.Errorf("Snapshots weren't successfully deleted by `ecs_image_force_delete_snapshots`")
return fmt.Errorf("Snapshots weren't successfully deleted by " +
"`ecs_image_force_delete_snapshots`")
}
return nil
}
@ -144,17 +145,21 @@ func checkECSImageSharing(uid string) builderT.TestCheckFunc {
// describe the image, get block devices with a snapshot
client, _ := testAliyunClient()
imageSharePermissionResponse, err := client.DescribeImageSharePermission(&ecs.ModifyImageSharePermissionArgs{
RegionId: "cn-beijing",
ImageId: artifact.AlicloudImages["cn-beijing"],
})
imageSharePermissionResponse, err := client.DescribeImageSharePermission(
&ecs.ModifyImageSharePermissionArgs{
RegionId: "cn-beijing",
ImageId: artifact.AlicloudImages["cn-beijing"],
})
if err != nil {
return fmt.Errorf("Error retrieving Image Attributes for ECS Image Artifact (%#v) in ECS Image Sharing Test: %s", artifact, err)
return fmt.Errorf("Error retrieving Image Attributes for ECS Image Artifact (%#v) "+
"in ECS Image Sharing Test: %s", artifact, err)
}
if len(imageSharePermissionResponse.Accounts.Account) != 1 && imageSharePermissionResponse.Accounts.Account[0].AliyunId != uid {
return fmt.Errorf("share account is incorrect %d", len(imageSharePermissionResponse.Accounts.Account))
if len(imageSharePermissionResponse.Accounts.Account) != 1 &&
imageSharePermissionResponse.Accounts.Account[0].AliyunId != uid {
return fmt.Errorf("share account is incorrect %d",
len(imageSharePermissionResponse.Accounts.Account))
}
return nil

View File

@ -22,15 +22,18 @@ func (s *stepAttachKeyPar) Run(state multistep.StateBag) multistep.StepAction {
instance := state.Get("instance").(*ecs.InstanceAttributesType)
retry_times := 3
for {
err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion), KeyPairName: keyPairName,
InstanceIds: "[\"" + instance.InstanceId + "\"]"})
err := client.AttachKeyPair(&ecs.AttachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion),
KeyPairName: keyPairName, InstanceIds: "[\"" + instance.InstanceId + "\"]"})
if err != nil {
e, _ := err.(*common.Error)
if (!(e.Code == "MissingParameter" || e.Code == "DependencyViolation.WindowsInstance" || e.Code == "InvalidKeyPairName.NotFound" || e.Code == "InvalidRegionId.NotFound")) && retry_times > 0 {
if (!(e.Code == "MissingParameter" || e.Code == "DependencyViolation.WindowsInstance" ||
e.Code == "InvalidKeyPairName.NotFound" || e.Code == "InvalidRegionId.NotFound")) &&
retry_times > 0 {
retry_times = retry_times - 1
continue
}
err := fmt.Errorf("Error attaching keypair %s to instance %s : %s", keyPairName, instance.InstanceId, err)
err := fmt.Errorf("Error attaching keypair %s to instance %s : %s",
keyPairName, instance.InstanceId, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
@ -53,10 +56,11 @@ func (s *stepAttachKeyPar) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
instance := state.Get("instance").(*ecs.InstanceAttributesType)
err := client.DetachKeyPair(&ecs.DetachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion), KeyPairName: keyPairName,
InstanceIds: "[\"" + instance.InstanceId + "\"]"})
err := client.DetachKeyPair(&ecs.DetachKeyPairArgs{RegionId: common.Region(config.AlicloudRegion),
KeyPairName: keyPairName, InstanceIds: "[\"" + instance.InstanceId + "\"]"})
if err != nil {
err := fmt.Errorf("Error Detaching keypair %s to instance %s : %s", keyPairName, instance.InstanceId, err)
err := fmt.Errorf("Error Detaching keypair %s to instance %s : %s", keyPairName,
instance.InstanceId, err)
state.Put("error", err)
ui.Error(err.Error())
return

View File

@ -16,7 +16,8 @@ func (s *stepCheckAlicloudSourceImage) Run(state multistep.StateBag) multistep.S
client := state.Get("client").(*ecs.Client)
config := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui)
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{RegionId: common.Region(config.AlicloudRegion), ImageId: config.AlicloudSourceImage})
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{RegionId: common.Region(config.AlicloudRegion),
ImageId: config.AlicloudSourceImage})
if err != nil {
err := fmt.Errorf("Error querying alicloud image: %s", err)
state.Put("error", err)

View File

@ -29,7 +29,8 @@ func (s *setpConfigAlicloudEIP) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt
}
s.allocatedId = allocateId
if err = client.WaitForEip(common.Region(s.RegionId), allocateId, ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
if err = client.WaitForEip(common.Region(s.RegionId), allocateId,
ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
state.Put("error", err)
ui.Say(fmt.Sprintf("Error allocate alicloud eip: %s", err))
return multistep.ActionHalt
@ -41,7 +42,8 @@ func (s *setpConfigAlicloudEIP) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt
}
if err = client.WaitForEip(common.Region(s.RegionId), allocateId, ecs.EipStatusInUse, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
if err = client.WaitForEip(common.Region(s.RegionId), allocateId,
ecs.EipStatusInUse, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
state.Put("error", err)
ui.Say(fmt.Sprintf("Error associating alicloud eip: %s", err))
return multistep.ActionHalt
@ -66,7 +68,8 @@ func (s *setpConfigAlicloudEIP) Cleanup(state multistep.StateBag) {
ui.Say(fmt.Sprintf("Unassociate alicloud eip failed "))
}
if err := client.WaitForEip(common.Region(s.RegionId), s.allocatedId, ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
if err := client.WaitForEip(common.Region(s.RegionId), s.allocatedId,
ecs.EipStatusAvailable, ALICLOUD_DEFAULT_SHORT_TIMEOUT); err != nil {
ui.Say(fmt.Sprintf("Unassociate alicloud eip timeout "))
}
if err := client.ReleaseEipAddress(s.allocatedId); err != nil {

View File

@ -81,7 +81,9 @@ func (s *stepConfigAlicloudVPC) Cleanup(state multistep.StateBag) {
for {
if err := client.DeleteVpc(s.VpcId); err != nil {
e, _ := err.(*common.Error)
if (e.Code == "DependencyViolation.Instance" || e.Code == "DependencyViolation.RouteEntry" || e.Code == "DependencyViolation.VSwitch" || e.Code == "DependencyViolation.SecurityGroup") && time.Now().Before(start) {
if (e.Code == "DependencyViolation.Instance" || e.Code == "DependencyViolation.RouteEntry" ||
e.Code == "DependencyViolation.VSwitch" ||
e.Code == "DependencyViolation.SecurityGroup") && time.Now().Before(start) {
time.Sleep(1 * time.Second)
continue
}

View File

@ -133,7 +133,9 @@ func (s *stepConfigAlicloudVSwitch) Cleanup(state multistep.StateBag) {
for {
if err := client.DeleteVSwitch(s.VSwitchId); err != nil {
e, _ := err.(*common.Error)
if (e.Code == "IncorrectVSwitchStatus" || e.Code == "DependencyViolation" || e.Code == "DependencyViolation.HaVip" || e.Code == "IncorretRouteEntryStatus") && time.Now().Before(start) {
if (e.Code == "IncorrectVSwitchStatus" || e.Code == "DependencyViolation" ||
e.Code == "DependencyViolation.HaVip" ||
e.Code == "IncorretRouteEntryStatus") && time.Now().Before(start) {
time.Sleep(1 * time.Second)
continue
}

View File

@ -20,7 +20,8 @@ func (s *stepMountAlicloudDisk) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionContinue
}
ui.Say("Start mounting Disks")
disks, _, err := client.DescribeDisks(&ecs.DescribeDisksArgs{InstanceId: instance.InstanceId, RegionId: instance.RegionId})
disks, _, err := client.DescribeDisks(&ecs.DescribeDisksArgs{InstanceId: instance.InstanceId,
RegionId: instance.RegionId})
if err != nil {
err := fmt.Errorf("Error query alicloud disks failed: %s", err)
state.Put("error", err)

View File

@ -155,16 +155,19 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
ImageName: p.config.AlicloudImageName,
})
if err != nil {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
if len(images) > 0 && !p.config.AlicloudImageForceDetele {
return nil, false, fmt.Errorf("Duplicated image exists, please delete the existing images or set the 'image_force_delete' value as true")
return nil, false, fmt.Errorf("Duplicated image exists, please delete the existing images " +
"or set the 'image_force_delete' value as true")
}
// Set up the OSS client
log.Println("Creating OSS Client")
client, err := oss.New(getEndPonit(p.config.AlicloudRegion), p.config.AlicloudAccessKey, p.config.AlicloudSecretKey)
client, err := oss.New(getEndPonit(p.config.AlicloudRegion), p.config.AlicloudAccessKey,
p.config.AlicloudSecretKey)
if err != nil {
return nil, false, fmt.Errorf("Creating oss connection failed: %s", err)
}
@ -182,7 +185,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return nil, false, fmt.Errorf("Failed to upload image %s: %s", source, err)
}
if len(images) > 0 && p.config.AlicloudImageForceDetele {
if err = ecsClient.DeleteImage(packercommon.Region(p.config.AlicloudRegion), images[0].ImageId); err != nil {
if err = ecsClient.DeleteImage(packercommon.Region(p.config.AlicloudRegion),
images[0].ImageId); err != nil {
return nil, false, fmt.Errorf("Delete duplicated image %s failed", images[0].ImageName)
}
}
@ -215,14 +219,16 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
RoleName: "AliyunECSImageImportDefaultRole",
})
if err != nil {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
if roleResponse.Role.RoleId == "" {
if _, err = ramClient.CreateRole(ram.RoleRequest{
RoleName: "AliyunECSImageImportDefaultRole",
AssumeRolePolicyDocument: AliyunECSImageImportDefaultRolePolicy,
}); err != nil {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
if _, err := ramClient.AttachPolicyToRole(ram.AttachPolicyToRoleRequest{
ram.PolicyRequest{
@ -230,18 +236,21 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
PolicyType: "System",
}, "AliyunECSImageImportDefaultRole",
}); err != nil {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
} else {
policyListResponse, err := ramClient.ListPoliciesForRole(ram.RoleQueryRequest{
"AliyunECSImageImportDefaultRole",
})
if err != nil {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
isAliyunECSImageImportRolePolicyNotExit := true
for _, policy := range policyListResponse.Policies.Policy {
if policy.PolicyName == "AliyunECSImageImportRolePolicy" && policy.PolicyType == "System" {
if policy.PolicyName == "AliyunECSImageImportRolePolicy" &&
policy.PolicyType == "System" {
isAliyunECSImageImportRolePolicyNotExit = false
break
}
@ -253,7 +262,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
PolicyType: "System",
}, "AliyunECSImageImportDefaultRole",
}); err != nil {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
}
if _, err = ramClient.UpdateRole(
@ -261,7 +271,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
RoleName: "AliyunECSImageImportDefaultRole",
NewAssumeRolePolicyDocument: AliyunECSImageImportDefaultRolePolicy,
}); err != nil {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
}
for i := 10; i > 0; i = i - 1 {
@ -271,17 +282,20 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
if e.Code == "NoSetRoletoECSServiceAcount" {
time.Sleep(5 * time.Second)
continue
} else if e.Code == "ImageIsImporting" || e.Code == "InvalidImageName.Duplicated" {
} else if e.Code == "ImageIsImporting" ||
e.Code == "InvalidImageName.Duplicated" {
break
}
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
break
}
} else {
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s", getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to start import from %s/%s: %s",
getEndPonit(p.config.OSSBucket), p.config.OSSKey, err)
}
}
@ -298,9 +312,11 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
}
if !p.config.SkipClean {
ui.Message(fmt.Sprintf("Deleting import source %s/%s/%s", getEndPonit(p.config.AlicloudRegion), p.config.OSSBucket, p.config.OSSKey))
ui.Message(fmt.Sprintf("Deleting import source %s/%s/%s",
getEndPonit(p.config.AlicloudRegion), p.config.OSSBucket, p.config.OSSKey))
if err = bucket.DeleteObject(p.config.OSSKey); err != nil {
return nil, false, fmt.Errorf("Failed to delete %s/%s/%s: %s", getEndPonit(p.config.AlicloudRegion), p.config.OSSBucket, p.config.OSSKey, err)
return nil, false, fmt.Errorf("Failed to delete %s/%s/%s: %s",
getEndPonit(p.config.AlicloudRegion), p.config.OSSBucket, p.config.OSSKey, err)
}
}