Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instance): add support for cloud-init #1145

Merged
merged 6 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ARGS:
[security-group-id] The security group ID it use for this server
[placement-group-id] The placement group ID in witch the server has to be created
[bootscript-id] The bootscript ID to use, if empty the local boot will be used
[cloud-init] The cloud-init script to use
[organization-id] Organization ID to use. If none is passed will use default organization ID from the config
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ARGS:
server-id UUID of the server
[name] Name of the server
[ip] IP that should be attached to the server (use ip=none to detach)
[cloud-init] The cloud-init script to use
[boot-type] (local | bootscript | rescue)
[tags.{index}] Tags of the server
[bootscript]
Expand Down
19 changes: 19 additions & 0 deletions internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package instance

import (
"bytes"
"context"
"fmt"
"net"
Expand Down Expand Up @@ -204,6 +205,7 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
PlacementGroupID *instance.NullableStringValue
SecurityGroupID *string
VolumeIDs *[]string
CloudInit string
}

c.ArgsType = reflect.TypeOf(instanceUpdateServerRequestCustom{})
Expand All @@ -229,6 +231,10 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
Name: "ip",
Short: `IP that should be attached to the server (use ip=none to detach)`,
})
c.ArgSpecs.AddBefore("boot-type", &core.ArgSpec{
Name: "cloud-init",
Short: "The cloud-init script to use",
})

c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
customRequest := argsI.(*instanceUpdateServerRequestCustom)
Expand Down Expand Up @@ -315,6 +321,19 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
customRequest.Volumes = &volumes
}

// Set cloud-init
if customRequest.CloudInit != "" {
err := api.SetServerUserData(&instance.SetServerUserDataRequest{
Zone: updateServerRequest.Zone,
ServerID: customRequest.ServerID,
Key: "cloud-init",
Content: bytes.NewBufferString(customRequest.CloudInit),
})
if err != nil {
return nil, err
}
}

updateServerResponse, err := api.UpdateServer(updateServerRequest)
if err != nil {
return nil, err
Expand Down
25 changes: 24 additions & 1 deletion internal/namespaces/instance/v1/custom_server_create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package instance

import (
"bytes"
"context"
"fmt"
"net"
Expand Down Expand Up @@ -33,6 +34,7 @@ type instanceCreateServerRequest struct {
SecurityGroupID string
PlacementGroupID string
BootscriptID string
CloudInit string
}

// TODO: Remove all error uppercase and punctuations when [APIGW-1367] will be done
Expand Down Expand Up @@ -99,6 +101,10 @@ func serverCreateCommand() *core.Command {
Name: "bootscript-id",
Short: "The bootscript ID to use, if empty the local boot will be used",
},
{
Name: "cloud-init",
Short: "The cloud-init script to use",
},
core.OrganizationIDArgSpec(),
core.ZoneArgSpec(),
},
Expand Down Expand Up @@ -334,7 +340,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
}

//
// Server
// Server Creation
//
logger.Debugf("creating server")
serverRes, err := apiInstance.CreateServer(serverReq)
Expand All @@ -356,6 +362,23 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
server := serverRes.Server
logger.Debugf("server created %s", server.ID)

//
// Cloud-init
//
if args.CloudInit != "" {
err := apiInstance.SetServerUserData(&instance.SetServerUserDataRequest{
Zone: args.Zone,
ServerID: server.ID,
Key: "cloud-init",
Content: bytes.NewBufferString(args.CloudInit),
})
if err != nil {
logger.Warningf("error while setting up your cloud-init metadata: %s. Note that the server is successfully created.", err)
} else {
logger.Debugf("cloud-init set")
}
}

//
// Start server by default
//
Expand Down