-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add cloud-init user data support via Linode Metadata #222
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,7 +51,9 @@ type Driver struct { | |
| StackScriptLabel string | ||
| StackScriptData map[string]string | ||
|
|
||
| Tags string | ||
| // UserData contains base64-encoded cloud-init user data for the Linode Metadata service. | ||
| UserData string | ||
| Tags string | ||
| } | ||
|
|
||
| // VERSION represents the semver version of the package | ||
|
|
@@ -221,6 +223,11 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag { | |
| Usage: "A JSON string specifying data for the selected StackScript", | ||
| Value: "", | ||
| }, | ||
| mcnflag.StringFlag{ | ||
| EnvVar: "LINODE_USER_DATA", | ||
| Name: "linode-user-data", | ||
| Usage: "Path to a cloud-init user-data file for the Linode Metadata service", | ||
| }, | ||
| mcnflag.BoolFlag{ | ||
| EnvVar: "LINODE_CREATE_PRIVATE_IP", | ||
| Name: "linode-create-private-ip", | ||
|
|
@@ -279,6 +286,16 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { | |
| d.UserAgentPrefix = flags.String("linode-ua-prefix") | ||
| d.Tags = flags.String("linode-tags") | ||
|
|
||
| userData := flags.String("linode-user-data") | ||
| if userData != "" { | ||
| encodedUserData, err := encodeUserData(userData) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| d.UserData = encodedUserData | ||
| } | ||
|
Comment on lines
+289
to
+297
|
||
|
|
||
| d.SetSwarmConfigFromFlags(flags) | ||
|
|
||
| if d.APIToken == "" { | ||
|
|
@@ -323,6 +340,24 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { | |
| return nil | ||
| } | ||
|
|
||
| func encodeUserData(userData string) (string, error) { | ||
| if userData == "" { | ||
| return "", nil | ||
| } | ||
|
|
||
| path := strings.TrimSpace(userData) | ||
| if path == "" { | ||
| return "", fmt.Errorf("--linode-user-data requires a file path") | ||
| } | ||
|
|
||
| content, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to read user data from --linode-user-data file %q: %w", path, err) | ||
| } | ||
|
|
||
| return base64.StdEncoding.EncodeToString(content), nil | ||
| } | ||
|
|
||
| // PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation | ||
| func (d *Driver) PreCreateCheck() error { | ||
| // TODO(displague) linode-stackscript-file should be read and uploaded (private), then used for boot. | ||
|
|
@@ -426,6 +461,12 @@ func (d *Driver) Create() error { | |
| log.Infof("Using StackScript %d: %s/%s", d.StackScriptID, d.StackScriptUser, d.StackScriptLabel) | ||
| } | ||
|
|
||
| if d.UserData != "" { | ||
| createOpts.Metadata = &linodego.InstanceMetadataOptions{ | ||
| UserData: d.UserData, | ||
| } | ||
| } | ||
|
|
||
| linode, err := client.CreateInstance(context.TODO(), createOpts) | ||
| if err != nil { | ||
| return err | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.