Skip to content

Commit

Permalink
Merge pull request #233 from mmlb/reinstall
Browse files Browse the repository at this point in the history
Add support for device reinstall
  • Loading branch information
displague authored Oct 25, 2022
2 parents 074cf1f + 187dfb8 commit 2989369
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/metal/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
metal
3 changes: 2 additions & 1 deletion docs/metal_device.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Device operations. For more information on provisioning on Equinix Metal, visit

### Synopsis

Device operations: create, get, update, delete, start, stop, and reboot.
Device operations: create, get, update, delete, reinstall, start, stop, and reboot.

### Options

Expand Down Expand Up @@ -33,6 +33,7 @@ Device operations: create, get, update, delete, start, stop, and reboot.
* [metal device delete](metal_device_delete.md) - Deletes a device.
* [metal device get](metal_device_get.md) - Retrieves device list or device details.
* [metal device reboot](metal_device_reboot.md) - Reboots a device.
* [metal device reinstall](metal_device_reinstall.md) - Reinstalls a device.
* [metal device start](metal_device_start.md) - Starts a device.
* [metal device stop](metal_device_stop.md) - Stops a device.
* [metal device update](metal_device_update.md) - Updates a device.
Expand Down
39 changes: 39 additions & 0 deletions docs/metal_device_reinstall.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## metal device reinstall

Reinstalls a device.

### Synopsis

Reinstalls the provided device. The ID of the device to reinstall is required.

```
metal device reinstall -d <device-id> [flags]
```

### Options

```
-h, --help help for reinstall
-d, --id string ID of device to be reinstalled
-O, --operating-system string Operating system name for the device
--preserve-data Avoid wiping data on disks where the os is *not* to be installed into
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file
--exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep
--filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters.
--include strings Comma separated Href references to expand in results, may be dotted three levels deep
-o, --output string Output format (*table, json, yaml)
--search string Search keyword for use in 'get' actions. Search is not supported by all resources.
--sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources.
--sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources.
--token string Metal API Token (METAL_AUTH_TOKEN)
```

### SEE ALSO

* [metal device](metal_device.md) - Device operations. For more information on provisioning on Equinix Metal, visit https://metal.equinix.com/developers/docs/deploy/.

3 changes: 2 additions & 1 deletion internal/devices/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Client) NewCommand() *cobra.Command {
Use: `device`,
Aliases: []string{"server", "servers", "devices"},
Short: "Device operations. For more information on provisioning on Equinix Metal, visit https://metal.equinix.com/developers/docs/deploy/.",
Long: "Device operations: create, get, update, delete, start, stop, and reboot.",
Long: "Device operations: create, get, update, delete, reinstall, start, stop, and reboot.",

PersistentPreRun: func(cmd *cobra.Command, args []string) {
if root := cmd.Root(); root != nil {
Expand All @@ -57,6 +57,7 @@ func (c *Client) NewCommand() *cobra.Command {
c.Delete(),
c.Update(),
c.Reboot(),
c.Reinstall(),
c.Start(),
c.Stop(),
)
Expand Down
65 changes: 65 additions & 0 deletions internal/devices/reinstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright © 2018 Jasmin Gacic <jasmin@stackpointcloud.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package devices

import (
"fmt"

"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

func (c *Client) Reinstall() *cobra.Command {
var (
id string

operatingSystem string
preserveData bool
)

reinstallDeviceCmd := &cobra.Command{
Use: `reinstall -d <device-id>`,
Short: "Reinstalls a device.",
Long: "Reinstalls the provided device. The ID of the device to reinstall is required.",
RunE: func(cmd *cobra.Command, args []string) error {

request := packngo.DeviceReinstallFields{
OperatingSystem: "",
PreserveData: preserveData,
DeprovisionFast: preserveData,
}

_, err := c.Service.Reinstall(id, &request)
if err != nil {
err = fmt.Errorf("Could not reinstall Device: %w", err)
}

return err
},
}
reinstallDeviceCmd.Flags().StringVarP(&id, "id", "d", "", "ID of device to be reinstalled")
_ = reinstallDeviceCmd.MarkFlagRequired("id")

reinstallDeviceCmd.Flags().StringVarP(&operatingSystem, "operating-system", "O", "", "Operating system name for the device")
reinstallDeviceCmd.Flags().BoolVarP(&preserveData, "preserve-data", "", false, "Avoid wiping data on disks where the os is *not* to be installed into")

return reinstallDeviceCmd
}

0 comments on commit 2989369

Please sign in to comment.