-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_ensure_keypair.go
107 lines (95 loc) · 2.91 KB
/
step_ensure_keypair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package qingcloud
import (
"context"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/yunify/qingcloud-sdk-go/service"
)
type StepEnsureKeypair struct {
}
func (step *StepEnsureKeypair) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config, _ := state.Get(BuilderConfig).(Config)
ui, _ := state.Get(UI).(packer.Ui)
ui.Message("Create keypair if needed")
qservice := config.GetQingCloudService()
var loginKeyPairID string
var privateKey string
//security group not found, create one
keypairService, err := qservice.KeyPair(config.Zone)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
if config.Password != "" {
state.Put(Password, config.Password)
}
flgHasKeypair := false
if config.KeypairID == AllocateNewID {
keypairOutput, err := keypairService.CreateKeyPair(
&service.CreateKeyPairInput{
KeyPairName: service.String("packer" + config.PackerConfig.PackerBuildName),
Mode: service.String("system"),
EncryptMethod: service.String("ssh-rsa"),
},
)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
privateKey = *keypairOutput.PrivateKey
loginKeyPairID = *keypairOutput.KeyPairID
flgHasKeypair = true
} else if config.KeypairID == LocalKey {
publicKey, err := loadFileContent(DefaultPublicKey)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
privateKey, err = loadFileContent(DefaultPrivateKey)
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
keypairOutput, err := keypairService.CreateKeyPair(
&service.CreateKeyPairInput{
KeyPairName: service.String("packer" + config.PackerConfig.PackerBuildName),
Mode: service.String("user"),
PublicKey: service.String(publicKey),
})
if err != nil {
ui.Error(err.Error())
return multistep.ActionHalt
}
loginKeyPairID = *keypairOutput.KeyPairID
privateKey = *keypairOutput.PrivateKey
flgHasKeypair = true
} else if len(config.KeypairID) != 0 {
loginKeyPairID = config.KeypairID
pk, err := config.ReadSSHPrivateKeyFile()
if err != nil {
ui.Error(err.Error())
}
privateKey = string(pk)
flgHasKeypair = true
}
if flgHasKeypair {
state.Put(LoginKeyPairID, loginKeyPairID)
state.Put(PrivateKey, privateKey)
}
return multistep.ActionContinue
}
func (step *StepEnsureKeypair) Cleanup(state multistep.StateBag) {
config, _ := state.Get(BuilderConfig).(Config)
ui, _ := state.Get(UI).(packer.Ui)
ui.Message("Clean up keypair if needed")
keypairID, ok := state.Get(LoginKeyPairID).(string)
if ok && keypairID != config.KeypairID {
qservice := config.GetQingCloudService()
keypairService, err := qservice.KeyPair(config.Zone)
if err != nil {
ui.Error(err.Error())
return
}
keypairService.DeleteKeyPairs(&service.DeleteKeyPairsInput{KeyPairs: []*string{service.String(keypairID)}})
}
}