Skip to content

Commit

Permalink
Merge pull request #460 from srl-labs/dedupDestroy
Browse files Browse the repository at this point in the history
Deduplicating destroy -t vs destroy --all
  • Loading branch information
hellt committed Jun 18, 2021
2 parents f92fadd + c2f429e commit eb969db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 56 deletions.
3 changes: 0 additions & 3 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ var deployCmd = &cobra.Command{
PreRunE: sudoCheck,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if err = topoSet(); err != nil {
return err
}
opts := []clab.ClabOption{
clab.WithDebug(debug),
clab.WithTimeout(timeout),
Expand Down
73 changes: 28 additions & 45 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,18 @@ var destroyCmd = &cobra.Command{
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

opts := []clab.ClabOption{
clab.WithDebug(debug),
clab.WithTimeout(timeout),
clab.WithRuntime(rt, debug, timeout, graceful),
}

topos := map[string]struct{}{}

switch {
case !all:
// stop if not topo file provided and not all labs are requested
// to be deleted
if err = topoSet(); err != nil {
return err
}
opts := []clab.ClabOption{
clab.WithDebug(debug),
clab.WithTimeout(timeout),
clab.WithTopoFile(topo),
clab.WithRuntime(rt, debug, timeout, graceful),
clab.WithGracefulShutdown(graceful),
}
c := clab.NewContainerLab(opts...)

// Parse topology information
if err = c.ParseTopology(); err != nil {
return err
}
labs = append(labs, c)
topos[topo] = struct{}{}
case all:
opts := []clab.ClabOption{
clab.WithDebug(debug),
clab.WithTimeout(timeout),
clab.WithRuntime(rt, debug, timeout, graceful),
}
c := clab.NewContainerLab(opts...)
// list all containerlab containers
containers, err := c.Runtime.ListContainers(ctx, []*types.GenericFilter{{FilterType: "label", Field: "containerlab", Operator: "exists"}})
Expand All @@ -72,32 +57,30 @@ var destroyCmd = &cobra.Command{
return fmt.Errorf("no containerlab labs were found")
}
// get unique topo files from all labs
topos := map[string]struct{}{}
for _, cont := range containers {
topos[cont.Labels["clab-topo-file"]] = struct{}{}
}
for topo := range topos {
opts := []clab.ClabOption{
clab.WithDebug(debug),
clab.WithTimeout(timeout),
clab.WithTopoFile(topo),
clab.WithRuntime(rt, debug, timeout, graceful),
clab.WithGracefulShutdown(graceful),
}
c = clab.NewContainerLab(opts...)
// change to the dir where topo file is located
// to resolve relative paths of license/configs in ParseTopology
if err = os.Chdir(filepath.Dir(topo)); err != nil {
return err
}

// Parse topology information
if err = c.ParseTopology(); err != nil {
return err
}
labs = append(labs, c)
}

for topo := range topos {
opts := append(opts,
clab.WithTopoFile(topo),
clab.WithGracefulShutdown(graceful),
)
c := clab.NewContainerLab(opts...)
// change to the dir where topo file is located
// to resolve relative paths of license/configs in ParseTopology
if err = os.Chdir(filepath.Dir(topo)); err != nil {
return err
}

// Parse topology information
if err = c.ParseTopology(); err != nil {
return err
}
labs = append(labs, c)
}

var errs []error
for _, clab := range labs {
err = destroyLab(ctx, clab)
Expand Down
8 changes: 0 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&rt, "runtime", "r", "", "container runtime")
}

// returns an error if topo path is not provided
func topoSet() error {
if topo == "" {
return errors.New("path to the topology definition file must be provided with --topo/-t flag")
}
return nil
}

func sudoCheck(cmd *cobra.Command, args []string) error {
id := os.Geteuid()
if id != 0 {
Expand Down

0 comments on commit eb969db

Please sign in to comment.