From 50f48c3dfd4edfed93ed0032d6791b0fb43f3683 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Wed, 22 Sep 2021 14:34:21 +0200 Subject: [PATCH] Add support for user-data The OTC driver has some traces of a person attempting to support cloud-init user data but this feature was not really available. Add support for a new option --otc-user-data-file= which accepts any valid cloud-init script of yaml. This was tested against the production OTC API. Signed-off-by: Zygmunt Krynicki --- otc/ecs.go | 19 ++++++++++++++++++- otcgo/ecs/instances.go | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/otc/ecs.go b/otc/ecs.go index 25222eb..408b07f 100644 --- a/otc/ecs.go +++ b/otc/ecs.go @@ -3,7 +3,7 @@ package otc import ( "crypto/md5" "crypto/rand" - _ "encoding/base64" + "encoding/base64" "fmt" "io" "io/ioutil" @@ -70,6 +70,7 @@ type Driver struct { AdminPass string KeyName string JobId string + UserData []byte //network ElasticIpBool int @@ -204,6 +205,12 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag { Value: 0, EnvVar: "ELASTIC_IP", }, + mcnflag.StringFlag{ + EnvVar: "OS_USER_DATA_FILE", + Name: "otc-user-data-file", + Usage: "File containing an openstack userdata script", + Value: "", + }, } } @@ -227,6 +234,15 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { d.ElasticIpType = flags.String("otc-elasticip-type") d.SSHUser = flags.String("otc-ssh-user") d.ElasticIpBool = flags.Int("otc-elastic-ip") + if name := flags.String("otc-user-data-file"); name != "" { + userData, err := ioutil.ReadFile(name) + if err == nil { + d.UserData = userData + } else { + return err + } + } + //fmt.Printf("test for region: %s\n", d.Region) return d.checkConfig() } @@ -588,6 +604,7 @@ func (d *Driver) createInstance() (ecsModules.CreateCloudServerResp, error) { instanceDesc.SetKey_name(d.KeyName) instanceDesc.SetAdminPass(d.AdminPass) instanceDesc.SetSecurity_groups(sgList) + instanceDesc.SetUser_data(base64.StdEncoding.EncodeToString(d.UserData)) /*log.Debugf("%s | SSH User: %s", d.MachineName, d.SSHUser) if d.SSHUser != "" { diff --git a/otcgo/ecs/instances.go b/otcgo/ecs/instances.go index a99fba0..8158207 100644 --- a/otcgo/ecs/instances.go +++ b/otcgo/ecs/instances.go @@ -31,6 +31,7 @@ type CreateInstanceAttribute struct { SecGrps []SecGrp `json:"security_groups"` AdminPass string `json:"adminPass"` KeyName string `json:"key_name"` + UserData []byte `json:"user_data"` } type CreateInstanceArgs struct {