Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Command = &cli.Command{ Name: "delete", Usage: "Delete a VM on a public cloud.", ArgsUsage: "<hostname>", Action: func(cCtx *cli.Context) error { ctx := cCtx.Context if cCtx.NArg() < 1 { return errors.New("not enough arguments") } arg := cCtx.Args().Get(0) hostnamesRanges := generators.SplitCommaOutsideOfBrackets(arg) var hostnames []string for _, hostnamesRange := range hostnamesRanges { h := generators.ExpandBrackets(hostnamesRange) hostnames = append(hostnames, h...) } logger.I.Info("Deleting...", zap.Any("hostnames", hostnames)) conf, err := config.ParseFile(cCtx.String("config.path")) if err != nil { return err } if err := conf.Validate(); err != nil { return err } var wg sync.WaitGroup errChan := make(chan error) for _, hostname := range hostnames { wg.Add(1) go func(ctx context.Context, hostname string, wg *sync.WaitGroup, errChan chan<- error) { defer wg.Done() // Search host and cloud by hostname var host *config.Host var cl *config.Cloud var err error for _, suffix := range conf.SuffixSearch { host, cl, err = conf.SearchHostByHostName(hostname + suffix) if err != nil { errChan <- err return } if host != nil && cl != nil { break } } if host == nil && cl == nil { host, cl, err = conf.SearchHostByHostName(hostname) if err != nil { errChan <- err return } } if host == nil && cl == nil { errChan <- errors.New("hostname not found") return } cloudWorker, err := cloud.New(cl) if err != nil { errChan <- err return } if err := cloudWorker.Delete(ctx, host.Name); err != nil { logger.I.Error( "couldn't delete the host", zap.Error(err), zap.Any("host", host), ) errChan <- err return } }( ctx, hostname, &wg, errChan, ) } go func() { wg.Wait() close(errChan) }() for e := range errChan { if e != nil { logger.I.Error("delete thrown an error", zap.Error(e)) err = e } } if err != nil { return err } logger.I.Info("Delete command successful.") return nil }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.