Skip to content

Commit

Permalink
Merge pull request #29 from opentelekomcloud/feature/user-data
Browse files Browse the repository at this point in the history
Add support of `-otc-user-data-file` flag
  • Loading branch information
outcatcher authored Apr 8, 2020
2 parents 52f8edf + 1115659 commit fd9779b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export PATH:=/usr/local/go/bin:$(PATH)
exec_path := /usr/local/bin/
exec_name := docker-machine-driver-otc

VERSION := 0.2.2b4
VERSION := 0.2.2b5


default: test build
Expand Down
15 changes: 14 additions & 1 deletion driver/opentelekomcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type Driver struct {
FloatingIP managedSting `json:"floating_ip"`
Token string `json:"token,omitempty"`
RootVolumeOpts *services.DiskOpts `json:"-"`
UserDataFile string `json:"-"`
IPVersion int `json:"-"`
skipEIPCreation bool
eipConfig *services.ElasticIPOpts
Expand Down Expand Up @@ -357,12 +358,22 @@ func (d *Driver) createInstance() error {
if d.K8sSecurityGroupID != "" {
secGroups = append(secGroups, d.K8sSecurityGroupID)
}

serverOpts := &servers.CreateOpts{
Name: d.MachineName,
FlavorRef: d.FlavorID,
SecurityGroups: secGroups,
AvailabilityZone: d.AvailabilityZone,
}

if d.UserDataFile != "" {
userData, err := ioutil.ReadFile(d.UserDataFile)
if err != nil {
return err
}
serverOpts.UserData = userData
}

instance, err := d.client.CreateInstance(serverOpts, d.SubnetID.Value, d.KeyPairName.Value, d.RootVolumeOpts)
if err != nil {
return err
Expand Down Expand Up @@ -522,7 +533,8 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
mcnflag.StringFlag{
Name: "otc-user-data-file",
EnvVar: "OS_USER_DATA_FILE",
Usage: "File containing an otc userdata script",
Usage: "File containing an userdata script",
Value: "",
},
mcnflag.StringFlag{
Name: "otc-token",
Expand Down Expand Up @@ -926,6 +938,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.KeyPairName = managedSting{Value: flags.String("otc-keypair-name")}
d.PrivateKeyFile = flags.String("otc-private-key-file")
d.Token = flags.String("otc-token")
d.UserDataFile = flags.String("otc-user-data-file")
d.AccessKey = services.AccessKey{
AccessKey: flags.String("otc-access-key-id"),
SecretKey: flags.String("otc-access-key-key"),
Expand Down
23 changes: 23 additions & 0 deletions driver/opentelekomcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,28 @@ func TestDriver_SetConfigFromFlagsDeprecated(t *testing.T) {
assert.Equal(t, eipType, driverNew.eipConfig.IPType)
assert.Equal(t, driverNew.skipEIPCreation, driverDeprecated.skipEIPCreation)
assert.Equal(t, true, driverNew.skipEIPCreation)
}

// This test won't check anything really, it exists only for debug purposes
func TestDriver_CreateWithUserData(t *testing.T) {
fileName := "tmp.sh"
userData := []byte("#!/usr/bin/bash\necho touch > /tmp/my")
require.NoError(t, ioutil.WriteFile(fileName, userData, os.ModePerm))
defer func() {
_ = os.Remove(fileName)
}()

driver, err := newDriverFromFlags(
map[string]interface{}{
"otc-cloud": "otc",
"otc-user-data-file": fileName,
})
require.NoError(t, err)
require.NoError(t, driver.initCompute())
require.NoError(t, driver.initNetwork())
defer func() {
assert.NoError(t, cleanupResources(driver))
}()
assert.NoError(t, driver.Create())
assert.NoError(t, driver.Remove())
}
1 change: 1 addition & 0 deletions driver/services/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (c *Client) CreateInstance(opts *servers.CreateOpts, subnetID string, keyPa
FlavorRef: opts.FlavorRef,
FlavorName: opts.FlavorName,
SecurityGroups: opts.SecurityGroups,
UserData: opts.UserData,
AvailabilityZone: opts.AvailabilityZone,
Networks: []servers.Network{{UUID: subnetID}},
ServiceClient: c.ComputeV2,
Expand Down

0 comments on commit fd9779b

Please sign in to comment.