Skip to content

Commit 214163f

Browse files
committed
add user-data-base64
1 parent c0993b2 commit 214163f

4 files changed

Lines changed: 35 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
ENHANCEMENTS:
44

5+
* add the flag `--user-data-base64` about command `ucloud uhost create` to customize the startup behaviors when launching the uhost instance and the value must be base64-encode.(#55)
6+
7+
## 0.1.34 (2020-11-11)
8+
9+
ENHANCEMENTS:
10+
511
* add the flag `--user-data` about command `ucloud uhost create` to customize the startup behaviors when launching the uhost instance.(#54)
612
* add the flag `--gpu-type` about command `ucloud uhost create` to define the type of GPU instance.(#54)
713

base/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
3939
const DefaultProfile = "default"
4040

4141
//Version 版本号
42-
const Version = "0.1.34"
42+
const Version = "0.1.35"
4343

4444
var UserAgent = fmt.Sprintf("UCloud-CLI/%s", Version)
4545

base/util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package base
22

33
import (
44
"bufio"
5+
"encoding/base64"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -699,3 +700,8 @@ func getDefaultProject(cookie, csrfToken string) (string, string, error) {
699700
}
700701
return "", "", fmt.Errorf("default project not found")
701702
}
703+
704+
func IsBase64Encoded(data []byte) bool {
705+
_, err := base64.StdEncoding.DecodeString(string(data))
706+
return err == nil
707+
}

cmd/uhost.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,27 @@ func NewCmdUHostCreate() *cobra.Command {
290290
var hotPlugImageFlag bool
291291
var userData string
292292
var userDataImageFlag bool
293+
var userDataBase64 string
293294

294295
req := base.BizClient.NewCreateUHostInstanceRequest()
295296
eipReq := base.BizClient.NewAllocateEIPRequest()
296297
cmd := &cobra.Command{
297298
Use: "create",
298299
Short: "Create UHost instance",
299300
Long: "Create UHost instance",
301+
PreRunE: func(cmd *cobra.Command, args []string) error {
302+
if len(userData) > 0 && len(userDataBase64) > 0 {
303+
return fmt.Errorf("%q conflicts with %q, can only set one of both", "user-data", "user-data-base64")
304+
}
305+
306+
if len(userDataBase64) > 0 {
307+
if !base.IsBase64Encoded([]byte(userDataBase64)) {
308+
return fmt.Errorf("%q must be base64-encoded", "user-data-base64")
309+
}
310+
}
311+
return nil
312+
},
313+
300314
RunE: func(cmd *cobra.Command, args []string) error {
301315
*req.Memory *= 1024
302316
req.LoginMode = sdk.String("Password")
@@ -309,7 +323,7 @@ func NewCmdUHostCreate() *cobra.Command {
309323
req.Disks = req.Disks[:1]
310324
}
311325

312-
if hotPlug == "true" || len(userData) > 0 {
326+
if hotPlug == "true" || len(userData) > 0 || len(userDataBase64) > 0 {
313327
any, err := describeImageByID(base.PickResourceID(*req.ImageId), *req.ProjectId, *req.Region, *req.Zone)
314328
if err != nil {
315329
return fmt.Errorf("check image support feaures failed: %v", err)
@@ -332,7 +346,7 @@ func NewCmdUHostCreate() *cobra.Command {
332346
req.HotplugFeature = sdk.Bool(false)
333347
}
334348

335-
if !userDataImageFlag && len(userData) > 0 {
349+
if !userDataImageFlag && (len(userData) > 0 || len(userDataBase64) > 0) {
336350
return fmt.Errorf("image %s does not support user-data feature", *req.ImageId)
337351
}
338352

@@ -343,6 +357,10 @@ func NewCmdUHostCreate() *cobra.Command {
343357
if len(userData) > 0 {
344358
req.UserData = sdk.String(base64.StdEncoding.EncodeToString([]byte(userData)))
345359
}
360+
361+
if len(userDataBase64) > 0 {
362+
req.UserData = sdk.String(userDataBase64)
363+
}
346364
}
347365

348366
wg := &sync.WaitGroup{}
@@ -437,7 +455,8 @@ func NewCmdUHostCreate() *cobra.Command {
437455
req.Tag = flags.String("group", "Default", "Optional. Business group")
438456
req.IsolationGroup = flags.String("isolation-group", "", "Optional. Resource ID of isolation group. see 'ucloud uhost isolation-group list")
439457
req.GpuType = flags.String("gpu-type", "", "Optional. The type of GPU instance. Required if defined the `machine-type` as 'G'. Accept values: 'K80', 'P40', 'V100'. Forward to https://docs.ucloud.cn/api/uhost-api/uhost_type for details.")
440-
flags.StringVar(&userData, "user-data", "", "Optional. Customize the startup behaviors when launching the instance. Forward to https://docs.ucloud.cn/uhost/guide/metadata/userdata for details.")
458+
flags.StringVar(&userData, "user-data", "", "Optional. Conflicts with `user-data-base64`. ConCustomize the startup behaviors when launching the instance. Forward to https://docs.ucloud.cn/uhost/guide/metadata/userdata for details.")
459+
flags.StringVar(&userDataBase64, "user-data-base64", "", "Optional. Conflicts with `user-data`. Customize the startup behaviors when launching the instance. The value must be base64-encode. Forward to https://docs.ucloud.cn/uhost/guide/metadata/userdata for details.")
441460

442461
flags.MarkDeprecated("type", "please use --machine-type instead")
443462
flags.SetFlagValues("charge-type", "Month", "Year", "Dynamic", "Trial")

0 commit comments

Comments
 (0)