blob: 5e1d132ea9b82a3cb6e4942ff4d8374ce2e095de [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Tim Windelschmidt886c3862023-05-23 16:47:41 +02004package main
5
6import (
7 "context"
Tim Windelschmidtb765f242024-05-08 01:40:02 +02008 "os"
9 "os/signal"
Tim Windelschmidt886c3862023-05-23 16:47:41 +020010 "time"
11
Tim Windelschmidt72a903f2023-06-27 15:49:36 +020012 "github.com/packethost/packngo"
Tim Windelschmidt886c3862023-05-23 16:47:41 +020013 "github.com/spf13/cobra"
14 "k8s.io/klog/v2"
15
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020016 "source.monogon.dev/cloud/equinix/wrapngo"
Tim Windelschmidt886c3862023-05-23 16:47:41 +020017)
18
19var deleteCmd = &cobra.Command{
20 Use: "delete [target]",
21 Short: "Delete all devices from one project",
22 Args: cobra.ExactArgs(1),
23 Run: doDelete,
24}
25
26func init() {
27 rootCmd.AddCommand(deleteCmd)
28}
29
30func doDelete(cmd *cobra.Command, args []string) {
Tim Windelschmidtb765f242024-05-08 01:40:02 +020031 ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
Tim Windelschmidt886c3862023-05-23 16:47:41 +020032 api := wrapngo.New(&c)
33
34 klog.Infof("Listing devices for %q", args[0])
35
36 devices, err := api.ListDevices(ctx, args[0])
37 if err != nil {
38 klog.Exitf("failed listing devices: %v", err)
39 }
40
41 if len(devices) == 0 {
42 klog.Infof("No devices found in %s", args[0])
43 return
44 }
45
46 klog.Infof("Deleting %d Devices in %s. THIS WILL DELETE SERVERS! You have five seconds to cancel!", len(devices), args[0])
47 time.Sleep(5 * time.Second)
48
49 for _, d := range devices {
Tim Windelschmidt72a903f2023-06-27 15:49:36 +020050 h := "deleted-" + d.Hostname
51 _, err := api.UpdateDevice(ctx, d.ID, &packngo.DeviceUpdateRequest{
52 Hostname: &h,
53 })
54 if err != nil {
55 klog.Infof("failed updating device %s (%s): %v", d.ID, d.Hostname, err)
56 continue
57 }
58
Tim Windelschmidt886c3862023-05-23 16:47:41 +020059 klog.Infof("deleting %s (%s)...", d.ID, d.Hostname)
60 if err := api.DeleteDevice(ctx, d.ID); err != nil {
61 klog.Infof("failed deleting device %s (%s): %v", d.ID, d.Hostname, err)
62 continue
63 }
64 }
65}