blob: 97eb26f38612cdc9fb9b309ce4a8cfc1e0f0a62f [file] [log] [blame]
Tim Windelschmidt886c3862023-05-23 16:47:41 +02001package main
2
3import (
4 "context"
Tim Windelschmidtb765f242024-05-08 01:40:02 +02005 "os"
6 "os/signal"
Tim Windelschmidt886c3862023-05-23 16:47:41 +02007 "time"
8
Tim Windelschmidt72a903f2023-06-27 15:49:36 +02009 "github.com/packethost/packngo"
Tim Windelschmidt886c3862023-05-23 16:47:41 +020010 "github.com/spf13/cobra"
11 "k8s.io/klog/v2"
12
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020013 "source.monogon.dev/cloud/equinix/wrapngo"
Tim Windelschmidt886c3862023-05-23 16:47:41 +020014)
15
16var deleteCmd = &cobra.Command{
17 Use: "delete [target]",
18 Short: "Delete all devices from one project",
19 Args: cobra.ExactArgs(1),
20 Run: doDelete,
21}
22
23func init() {
24 rootCmd.AddCommand(deleteCmd)
25}
26
27func doDelete(cmd *cobra.Command, args []string) {
Tim Windelschmidtb765f242024-05-08 01:40:02 +020028 ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
Tim Windelschmidt886c3862023-05-23 16:47:41 +020029 api := wrapngo.New(&c)
30
31 klog.Infof("Listing devices for %q", args[0])
32
33 devices, err := api.ListDevices(ctx, args[0])
34 if err != nil {
35 klog.Exitf("failed listing devices: %v", err)
36 }
37
38 if len(devices) == 0 {
39 klog.Infof("No devices found in %s", args[0])
40 return
41 }
42
43 klog.Infof("Deleting %d Devices in %s. THIS WILL DELETE SERVERS! You have five seconds to cancel!", len(devices), args[0])
44 time.Sleep(5 * time.Second)
45
46 for _, d := range devices {
Tim Windelschmidt72a903f2023-06-27 15:49:36 +020047 h := "deleted-" + d.Hostname
48 _, err := api.UpdateDevice(ctx, d.ID, &packngo.DeviceUpdateRequest{
49 Hostname: &h,
50 })
51 if err != nil {
52 klog.Infof("failed updating device %s (%s): %v", d.ID, d.Hostname, err)
53 continue
54 }
55
Tim Windelschmidt886c3862023-05-23 16:47:41 +020056 klog.Infof("deleting %s (%s)...", d.ID, d.Hostname)
57 if err := api.DeleteDevice(ctx, d.ID); err != nil {
58 klog.Infof("failed deleting device %s (%s): %v", d.ID, d.Hostname, err)
59 continue
60 }
61 }
62}