-
Notifications
You must be signed in to change notification settings - Fork 19
/
instance_template_register.go
194 lines (158 loc) · 6.59 KB
/
instance_template_register.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package cmd
import (
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/utils"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type instanceTemplateRegisterCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"register"`
Name string `cli-arg:"#"`
URL string `cli-arg:"#"`
Checksum string `cli-arg:"#"`
BootMode string `cli-usage:"template boot mode (legacy|uefi)"`
Description string `cli-usage:"template description"`
Build string `cli-usage:"template build"`
Version string `cli-usage:"template version"`
Maintainer string `cli-usage:"template maintainer"`
DisablePassword bool `cli-usage:"disable password-based authentication"`
DisableSSHKey bool `cli-flag:"disable-ssh-key" cli-usage:"disable SSH key-based authentication"`
FromSnapshot string `cli-usage:"ID of a Compute instance snapshot to register as template"`
Timeout int64 `cli-usage:"registration timeout duration in seconds"`
Username string `cli-usage:"template default username"`
Zone string `cli-short:"z" cli-usage:"zone to register the template into (default: current account's default zone)"`
}
func (c *instanceTemplateRegisterCmd) cmdAliases() []string { return gCreateAlias }
func (c *instanceTemplateRegisterCmd) cmdShort() string {
return "Register a new Compute instance template"
}
func (c *instanceTemplateRegisterCmd) cmdLong() string {
return fmt.Sprintf(`This command registers a new Compute instance template.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&instanceTemplateShowOutput{}), ", "))
}
func (c *instanceTemplateRegisterCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
// In case the user specified a snapshot ID using the `--from-snapshot` flag,
// we add empty positional argument placeholders in order to trick the
// cliCommandDefaultPreRun() wrapper into believing URL/Checksum args were provided,
// but the actual command function won't use them since it will dynamically retrieve
// this information from the specified snapshot export information.
snapshotID, err := cmd.Flags().GetString(mustCLICommandFlagName(c, &c.FromSnapshot))
if err != nil {
return err
}
if snapshotID != "" {
args = append(args, "", "")
}
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceTemplateRegisterCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var (
template *egoscale.Template
err error
)
globalstate.EgoscaleClient.Client.SetTimeout(time.Duration(c.Timeout) * time.Second)
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone),
)
passwordEnabled := !c.DisablePassword
sshKeyEnabled := !c.DisableSSHKey
template = &egoscale.Template{
Checksum: utils.NonEmptyStringPtr(c.Checksum),
DefaultUser: utils.NonEmptyStringPtr(c.Username),
Description: utils.NonEmptyStringPtr(c.Description),
Build: utils.NonEmptyStringPtr(c.Build),
Version: utils.NonEmptyStringPtr(c.Version),
Maintainer: utils.NonEmptyStringPtr(c.Maintainer),
Name: &c.Name,
PasswordEnabled: &passwordEnabled,
SSHKeyEnabled: &sshKeyEnabled,
URL: utils.NonEmptyStringPtr(c.URL),
}
if c.FromSnapshot != "" {
snapshot, err := globalstate.EgoscaleClient.GetSnapshot(ctx, c.Zone, c.FromSnapshot)
if err != nil {
return fmt.Errorf("error retrieving snapshot: %w", err)
}
snapshotExport, err := globalstate.EgoscaleClient.ExportSnapshot(ctx, c.Zone, snapshot)
if err != nil {
return fmt.Errorf("error retrieving snapshot export information: %w", err)
}
template.URL = snapshotExport.PresignedURL
template.Checksum = snapshotExport.MD5sum
// Pre-setting the new template properties from the source template.
instance, err := globalstate.EgoscaleClient.GetInstance(ctx, c.Zone, *snapshot.InstanceID)
if err != nil {
return fmt.Errorf("error retrieving Compute instance from snapshot: %w", err)
}
srcTemplate, err := globalstate.EgoscaleClient.GetTemplate(ctx, c.Zone, *instance.TemplateID)
if err != nil {
return fmt.Errorf("error retrieving Compute instance template from snapshot: %w", err)
}
template.BootMode = srcTemplate.BootMode
// Above properties are inherited from snapshot source template, unless otherwise specified
// by the user from the command line
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DisablePassword)) {
template.PasswordEnabled = &passwordEnabled
} else {
template.PasswordEnabled = srcTemplate.PasswordEnabled
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DisableSSHKey)) {
template.SSHKeyEnabled = &sshKeyEnabled
} else {
template.SSHKeyEnabled = srcTemplate.SSHKeyEnabled
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Username)) {
template.DefaultUser = utils.NonEmptyStringPtr(c.Username)
} else {
template.DefaultUser = srcTemplate.DefaultUser
}
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.BootMode)) {
template.BootMode = &c.BootMode
}
decorateAsyncOperation(fmt.Sprintf("Registering template %q...", c.Name), func() {
template, err = globalstate.EgoscaleClient.RegisterTemplate(ctx, c.Zone, template)
})
if err != nil {
return err
}
if !globalstate.Quiet {
return c.outputFunc(&instanceTemplateShowOutput{
ID: *template.ID,
Family: utils.DefaultString(template.Family, ""),
Name: *template.Name,
Description: utils.DefaultString(template.Description, ""),
CreationDate: template.CreatedAt.String(),
Visibility: *template.Visibility,
Size: *template.Size,
Version: utils.DefaultString(template.Version, ""),
Build: utils.DefaultString(template.Build, ""),
Maintainer: utils.DefaultString(template.Maintainer, ""),
Checksum: *template.Checksum,
DefaultUser: utils.DefaultString(template.DefaultUser, ""),
SSHKeyEnabled: *template.SSHKeyEnabled,
PasswordEnabled: *template.PasswordEnabled,
BootMode: *template.BootMode,
}, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(instanceTemplateCmd, &instanceTemplateRegisterCmd{
cliCommandSettings: defaultCLICmdSettings(),
BootMode: "legacy",
// Template registration can take a _long time_, raising
// the Exoscale API client timeout to 1h by default as a precaution.
Timeout: 3600,
}))
}