Skip to content

Commit

Permalink
feat: add --userdata-file to device/update (#370)
Browse files Browse the repository at this point in the history
Some preferential doc sting changes to highlight the ease of the recent
[changes to updating
always-pxe](a64058f)

Also C+V, C+V's the `--userdata-file` code from device/create to
device/update, making it easier to update complicated userdata formats
for devices in place.

```
$ bin/metal device update -i SOMEDEVICEUUID --userdata-file /tmp/my-cloud-init.mime
+--------------------------------------+----------+-------------+--------+
|                  ID                  | HOSTNAME |     OS      | STATE  |
+--------------------------------------+----------+-------------+--------+
| 9ffac3bb-4e04-466b-a786-b703fe52fa7f | h-01     | Custom iPXE | active |
+--------------------------------------+----------+-------------+--------+
```

Passed `make generate-docs` 🤷 ?
  • Loading branch information
displague authored Nov 9, 2023
2 parents 0cb12f6 + d680df9 commit 2554db1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
7 changes: 4 additions & 3 deletions docs/metal_device_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Updates a device.
Updates the hostname of a device. Updates or adds a description, tags, userdata, custom data, and iPXE settings for an already provisioned device. Can also lock or unlock future changes to the device.

```
metal device update -i <device_id> [-H <hostname>] [-d <description>] [--locked <boolean>] [-t <tags>] [-u <userdata>] [-c <customdata>] [-s <ipxe_script_url>] [--always-pxe=<true|false>] [flags]
metal device update -i <device_id> [-H <hostname>] [-d <description>] [--locked <boolean>] [-t <tags>] [-u <userdata> | --userdata-file <filepath>] [-c <customdata>] [-s <ipxe_script_url>] [--always-pxe=<true|false>] [flags]
```

### Examples
Expand All @@ -20,16 +20,17 @@ metal device update -i <device_id> [-H <hostname>] [-d <description>] [--locked
### Options

```
-a, --always-pxe Sets the device to always iPXE on reboot.
-a, --always-pxe Updates the always_pxe toggle for the device (<true|false>).
-c, --customdata string Adds or updates custom data to be included with your device's metadata.
-d, --description string Adds or updates the description for the device.
-h, --help help for update
-H, --hostname string The new hostname of the device.
-i, --id string The UUID of the device.
-s, --ipxe-script-url string Add or update the URL of the iPXE script.
-l, --locked Locks or unlocks the device for future changes.
-l, --locked Locks or unlocks the device for future changes (<true|false>).
-t, --tags strings Adds or updates the tags for the device --tags="tag1,tag2".
-u, --userdata string Adds or updates the userdata for the device.
--userdata-file string Path to a userdata file for device initialization. Can not be used with --userdata.
```

### Options inherited from parent commands
Expand Down
21 changes: 18 additions & 3 deletions internal/devices/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"

metal "github.com/equinix-labs/metal-go/metal/v1"

Expand All @@ -35,6 +36,7 @@ func (c *Client) Update() *cobra.Command {
description string
locked bool
userdata string
userdataFile string
hostname string
tags []string
alwaysPXE bool
Expand All @@ -44,7 +46,7 @@ func (c *Client) Update() *cobra.Command {
)
// updateDeviceCmd represents the updateDevice command
updateDeviceCmd := &cobra.Command{
Use: `update -i <device_id> [-H <hostname>] [-d <description>] [--locked <boolean>] [-t <tags>] [-u <userdata>] [-c <customdata>] [-s <ipxe_script_url>] [--always-pxe=<true|false>]`,
Use: `update -i <device_id> [-H <hostname>] [-d <description>] [--locked <boolean>] [-t <tags>] [-u <userdata> | --userdata-file <filepath>] [-c <customdata>] [-s <ipxe_script_url>] [--always-pxe=<true|false>]`,
Short: "Updates a device.",
Long: "Updates the hostname of a device. Updates or adds a description, tags, userdata, custom data, and iPXE settings for an already provisioned device. Can also lock or unlock future changes to the device.",
Example: ` # Updates the hostname of a device:
Expand All @@ -62,6 +64,18 @@ func (c *Client) Update() *cobra.Command {
deviceUpdate.Description = &description
}

if userdata != "" && userdataFile != "" {
return fmt.Errorf("either userdata or userdata-file should be set")
}

if userdataFile != "" {
userdataRaw, readErr := os.ReadFile(userdataFile)
if readErr != nil {
return fmt.Errorf("could not read userdata-file: %w", readErr)
}
userdata = string(userdataRaw)
}

if userdata != "" {
deviceUpdate.Userdata = &userdata
}
Expand Down Expand Up @@ -108,9 +122,10 @@ func (c *Client) Update() *cobra.Command {
updateDeviceCmd.Flags().StringVarP(&hostname, "hostname", "H", "", "The new hostname of the device.")
updateDeviceCmd.Flags().StringVarP(&description, "description", "d", "", "Adds or updates the description for the device.")
updateDeviceCmd.Flags().StringVarP(&userdata, "userdata", "u", "", "Adds or updates the userdata for the device.")
updateDeviceCmd.Flags().BoolVarP(&locked, "locked", "l", false, "Locks or unlocks the device for future changes.")
updateDeviceCmd.Flags().StringVarP(&userdataFile, "userdata-file", "", "", "Path to a userdata file for device initialization. Can not be used with --userdata.")
updateDeviceCmd.Flags().BoolVarP(&locked, "locked", "l", false, "Locks or unlocks the device for future changes (<true|false>).")
updateDeviceCmd.Flags().StringSliceVarP(&tags, "tags", "t", []string{}, `Adds or updates the tags for the device --tags="tag1,tag2".`)
updateDeviceCmd.Flags().BoolVarP(&alwaysPXE, "always-pxe", "a", false, "Sets the device to always iPXE on reboot.")
updateDeviceCmd.Flags().BoolVarP(&alwaysPXE, "always-pxe", "a", false, "Updates the always_pxe toggle for the device (<true|false>).")
updateDeviceCmd.Flags().StringVarP(&ipxescripturl, "ipxe-script-url", "s", "", "Add or update the URL of the iPXE script.")
updateDeviceCmd.Flags().StringVarP(&customdata, "customdata", "c", "", "Adds or updates custom data to be included with your device's metadata.")
_ = updateDeviceCmd.MarkFlagRequired("id")
Expand Down

0 comments on commit 2554db1

Please sign in to comment.