From 7d8e2d5093b15cd2a6c330e279a9c442154ceecb Mon Sep 17 00:00:00 2001 From: postables Date: Thu, 16 Apr 2020 17:04:01 -0700 Subject: [PATCH 1/3] enable force delete for cluster removal --- cluster.go | 2 +- cmd/labctl/command/cluster.go | 2 +- labd/controlapi/cluster.go | 5 +++-- labd/routers/clusterrouter/router.go | 7 +++++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cluster.go b/cluster.go index 9108bc6e..b4693ea8 100644 --- a/cluster.go +++ b/cluster.go @@ -35,7 +35,7 @@ type ClusterAPI interface { List(ctx context.Context, opts ...ListOption) ([]Cluster, error) // Remove destroys clusters permanently. - Remove(ctx context.Context, names ...string) error + Remove(ctx context.Context, force bool, names ...string) error } // Cluster is a group of instances connected in a p2p network. They can be diff --git a/cmd/labctl/command/cluster.go b/cmd/labctl/command/cluster.go index 84d01beb..35b9f817 100644 --- a/cmd/labctl/command/cluster.go +++ b/cmd/labctl/command/cluster.go @@ -251,7 +251,7 @@ func removeClustersAction(c *cli.Context) error { } ctx := cliutil.CommandContext(c) - err = control.Cluster().Remove(ctx, names...) + err = control.Cluster().Remove(ctx, c.Bool("force"), names...) if err != nil { return err } diff --git a/labd/controlapi/cluster.go b/labd/controlapi/cluster.go index 127e338a..eac0e90a 100644 --- a/labd/controlapi/cluster.go +++ b/labd/controlapi/cluster.go @@ -192,9 +192,10 @@ func (a *clusterAPI) List(ctx context.Context, opts ...p2plab.ListOption) ([]p2p type Event struct { } -func (a *clusterAPI) Remove(ctx context.Context, names ...string) error { +func (a *clusterAPI) Remove(ctx context.Context, force bool, names ...string) error { req := a.client.NewRequest("DELETE", a.url("/clusters/delete")). - Option("names", strings.Join(names, ",")) + Option("names", strings.Join(names, ",")). + Option("force", force) resp, err := req.Send(ctx) if err != nil { diff --git a/labd/routers/clusterrouter/router.go b/labd/routers/clusterrouter/router.go index b0471042..46e4b6d6 100644 --- a/labd/routers/clusterrouter/router.go +++ b/labd/routers/clusterrouter/router.go @@ -172,7 +172,10 @@ func (s *router) putClustersLabel(ctx context.Context, w http.ResponseWriter, r func (s *router) deleteClusters(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { names := strings.Split(r.FormValue("names"), ",") - + var force bool + if r.FormValue("force") != "" { + force = true + } ctx, logger := logutil.WithResponseLogger(ctx, w) // TODO: parallelize with different color loggers? @@ -185,7 +188,7 @@ func (s *router) deleteClusters(ctx context.Context, w http.ResponseWriter, r *h return errors.Wrapf(err, "failed to get cluster %q", name) } - if cluster.Status != metadata.ClusterDestroying { + if cluster.Status != metadata.ClusterDestroying || force { cluster.Status = metadata.ClusterDestroying cluster, err = s.db.UpdateCluster(ctx, cluster) if err != nil { From 2376418f61737c51509ca226da20719cd6fe3fe7 Mon Sep 17 00:00:00 2001 From: postables Date: Thu, 16 Apr 2020 17:06:18 -0700 Subject: [PATCH 2/3] make go vet happy --- labapp/labapp.go | 3 ++- labd/labd.go | 3 ++- peer/peer.go | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/labapp/labapp.go b/labapp/labapp.go index 904c9d64..b063e6f0 100644 --- a/labapp/labapp.go +++ b/labapp/labapp.go @@ -36,9 +36,10 @@ func New(ctx context.Context, root, addr string, port int, logger *zerolog.Logge pctx, cancel := context.WithCancel(ctx) p, err := peer.New(pctx, root, port, pdef) if err != nil { + cancel() return nil, err } - closers = append(closers, &daemon.CancelCloser{cancel}) + closers = append(closers, &daemon.CancelCloser{Cancel: cancel}) daemon, err := daemon.New("labapp", addr, logger, approuter.New(p), diff --git a/labd/labd.go b/labd/labd.go index 9fe32561..03093d65 100644 --- a/labd/labd.go +++ b/labd/labd.go @@ -99,9 +99,10 @@ func New(root, addr string, logger *zerolog.Logger, opts ...LabdOption) (*Labd, Routing: "nil", }) if err != nil { + cancel() return nil, errors.Wrap(err, "failed to create seeder peer") } - closers = append(closers, &daemon.CancelCloser{cancel}) + closers = append(closers, &daemon.CancelCloser{Cancel: cancel}) ts := transformers.New(filepath.Join(root, "transformers"), client.HTTPClient) closers = append(closers, ts) diff --git a/peer/peer.go b/peer/peer.go index 673e06d2..c675bc69 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -293,7 +293,7 @@ func (p *Peer) Report(ctx context.Context) (metadata.ReportNode, error) { } return metadata.ReportNode{ - metadata.ReportBitswap{ + Bitswap: metadata.ReportBitswap{ BlocksReceived: stat.BlocksReceived, DataReceived: stat.DataReceived, BlocksSent: stat.BlocksSent, @@ -302,7 +302,7 @@ func (p *Peer) Report(ctx context.Context) (metadata.ReportNode, error) { DupDataReceived: stat.DupDataReceived, MessagesReceived: stat.MessagesReceived, }, - metadata.ReportBandwidth{ + Bandwidth: metadata.ReportBandwidth{ Totals: p.reporter.GetBandwidthTotals(), Peers: peers, Protocols: p.reporter.GetBandwidthByProtocol(), From 4f7b3c1f3dea3c288c526839e15082fb307e9710 Mon Sep 17 00:00:00 2001 From: postables Date: Thu, 16 Apr 2020 17:08:51 -0700 Subject: [PATCH 3/3] cmd/labctl/command: add force removal flag --- cmd/labctl/command/cluster.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/labctl/command/cluster.go b/cmd/labctl/command/cluster.go index 35b9f817..70c77c03 100644 --- a/cmd/labctl/command/cluster.go +++ b/cmd/labctl/command/cluster.go @@ -101,6 +101,12 @@ var clusterCommand = cli.Command{ Aliases: []string{"rm"}, Usage: "Remove clusters.", Action: removeClustersAction, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "force", + Usage: "force removal to ignore possible staleness and other misc issues", + }, + }, }, }, }