Skip to content

Commit

Permalink
redeploy command (#2374)
Browse files Browse the repository at this point in the history
* containerlab redeploy (#2371)

redeploy command

Co-authored-by: Anatoly Kolpakov <ankolpakov@gmail.com>

* fix typo

* remove reconfigure from the redeploy cmd

* added a test

---------

Co-authored-by: Anatoly Kolpakov <feroxsnake@me.com>
Co-authored-by: Anatoly Kolpakov <ankolpakov@gmail.com>
  • Loading branch information
3 people authored Jan 3, 2025
1 parent 4f0d1cd commit b6b637d
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 7 deletions.
4 changes: 2 additions & 2 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ func (c *CLab) Deploy(ctx context.Context, options *DeployOptions) ([]runtime.Ge
return nil, err
}

err = links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
err = links.SetMgmtNetUnderlyingBridge(c.Config.Mgmt.Bridge)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1250,7 +1250,7 @@ func (c *CLab) Destroy(ctx context.Context, maxWorkers uint, keepMgmtNet bool) e

// Exec execute commands on running topology nodes.
func (c *CLab) Exec(ctx context.Context, cmds []string, options *ExecOptions) (*exec.ExecCollection, error) {
err := links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
err := links.SetMgmtNetUnderlyingBridge(c.Config.Mgmt.Bridge)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func destroyFn(_ *cobra.Command, _ []string) error {
}
}

err = links.SetMgmtNetUnderlayingBridge(nc.Config.Mgmt.Bridge)
err = links.SetMgmtNetUnderlyingBridge(nc.Config.Mgmt.Bridge)
if err != nil {
return err
}
Expand Down
56 changes: 56 additions & 0 deletions cmd/redeploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"net"

"github.com/spf13/cobra"
)

// redeployCmd represents the redeploy command.
var redeployCmd = &cobra.Command{
Use: "redeploy",
Short: "destroy and redeploy a lab",
Long: "destroy a lab and deploy it again based on the topology definition file\nreference: https://containerlab.dev/cmd/redeploy/",
Aliases: []string{"rdep"},
PreRunE: sudoCheck,
SilenceUsage: true,
RunE: redeployFn,
}

func init() {
rootCmd.AddCommand(redeployCmd) // Add to rootCmd

// Add destroy flags
redeployCmd.Flags().BoolVarP(&cleanup, "cleanup", "c", false, "delete lab directory")
redeployCmd.Flags().BoolVarP(&graceful, "graceful", "", false,
"attempt to stop containers before removing")
redeployCmd.Flags().BoolVarP(&all, "all", "a", false, "destroy all containerlab labs")
redeployCmd.Flags().UintVarP(&maxWorkers, "max-workers", "", 0,
"limit the maximum number of workers creating/deleting nodes")
redeployCmd.Flags().BoolVarP(&keepMgmtNet, "keep-mgmt-net", "", false, "do not remove the management network")
redeployCmd.Flags().StringSliceVarP(&nodeFilter, "node-filter", "", []string{},
"comma separated list of nodes to include")

// Add deploy flags
redeployCmd.Flags().BoolVarP(&graph, "graph", "g", false, "generate topology graph")
redeployCmd.Flags().StringVarP(&mgmtNetName, "network", "", "", "management network name")
redeployCmd.Flags().IPNetVarP(&mgmtIPv4Subnet, "ipv4-subnet", "4", net.IPNet{}, "management network IPv4 subnet range")
redeployCmd.Flags().IPNetVarP(&mgmtIPv6Subnet, "ipv6-subnet", "6", net.IPNet{}, "management network IPv6 subnet range")
redeployCmd.Flags().StringVarP(&deployFormat, "format", "f", "table", "output format. One of [table, json]")
redeployCmd.Flags().BoolVarP(&skipPostDeploy, "skip-post-deploy", "", false, "skip post deploy action")
redeployCmd.Flags().StringVarP(&exportTemplate, "export-template", "", "",
"template file for topology data export")
redeployCmd.Flags().BoolVarP(&skipLabDirFileACLs, "skip-labdir-acl", "", false,
"skip the lab directory extended ACLs provisioning")
}

func redeployFn(cmd *cobra.Command, args []string) error {
// First destroy the lab
err := destroyFn(destroyCmd, args)
if err != nil {
return err
}

// Then deploy it again
return deployFn(deployCmd, args)
}
2 changes: 1 addition & 1 deletion cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Refer to the https://containerlab.dev/cmd/save/ documentation to see the exact c
return err
}

err = links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
err = links.SetMgmtNetUnderlyingBridge(c.Config.Mgmt.Bridge)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The `deploy` command spins up a lab using the topology expressed via [topology d

With the global `--topo | -t` flag a user sets the path to the topology definition file that will be used to spin up a lab.

When the topology path refers to a directory, containerlab will look for a file with `.clab.yml` extension in that directory and use it as a topology definition file.
When the topology path refers to a directory, containerlab will look for a file with `.clab.yml` or `.clab.yaml` extension in that directory and use it as a topology definition file.

When the topology file flag is omitted, containerlab will try to find the matching file name by looking at the current working directory.

Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/destroy.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ To make containerlab attempt a graceful shutdown of the running containers, add

#### keep-mgmt-net

Do not try to remove the management network. Usually the management docker network (in case of docker) and the underlaying bridge are being removed. If you have attached additional resources outside of containerlab and you want the bridge to remain intact just add the `--keep-mgmt-net` flag.
Do not try to remove the management network. Usually the management docker network (in case of docker) and the underlying bridge are being removed. If you have attached additional resources outside of containerlab and you want the bridge to remain intact just add the `--keep-mgmt-net` flag.

#### all

Expand Down
133 changes: 133 additions & 0 deletions docs/cmd/redeploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# redeploy command

### Description

The `redeploy` command redeploys a lab referenced by a provided topology definition file. It effectively combines the `destroy` and `deploy` commands into a single operation.

The two most common applications of this command are:

1. Redeploying a lab while keeping the lab directory intact:

```bash
sudo containerlab redeploy -t mylab.clab.yml
```

This command will destroy the lab and redeploy it using the same topology file and the same lab directory. This should keep intact any saved configurations for the nodes.

2. Redeploying a lab while removing the lab directory at the destroy stage:

```bash
sudo containerlab redeploy --cleanup -t mylab.clab.yml
```

or using the shorthands:

```bash
sudo clab rdep -c -t mylab.clab.yml
```

This command will destroy the lab and remove the lab directory before deploying the lab again. This ensures a clean redeployment as if you were deploying a lab for the first time discarding any previous lab state.

### Usage

`containerlab [global-flags] redeploy [local-flags]`

**aliases:** `rdep`

### Flags

#### topology

With the global `--topo | -t` flag a user sets the path to the topology definition file that will be used to redeploy a lab.

When the topology path refers to a directory, containerlab will look for a file with `.clab.yml` or `.clab.yaml` extension in that directory and use it as a topology definition file.

When the topology file flag is omitted, containerlab will try to find the matching file name by looking at the current working directory.

If more than one file is found for directory-based path or when the flag is omitted entirely, containerlab will fail with an error.

#### cleanup

The local `--cleanup | -c` flag instructs containerlab to remove the lab directory and all its content during the destroy phase.

Without this flag present, containerlab will keep the lab directory and all files inside of it.

#### graceful

To make containerlab attempt a graceful shutdown of the running containers during destroy phase, add the `--graceful` flag. Without it, containers will be removed forcefully without attempting to stop them.

#### graph

The local `--graph | -g` flag instructs containerlab to generate a topology graph after deploying the lab.

#### network

With `--network` flag users can specify a custom name for the management network that containerlab creates for the lab.

#### ipv4-subnet

Using `--ipv4-subnet | -4` flag users can define a custom IPv4 subnet that containerlab will use to assign management IPv4 addresses.

#### ipv6-subnet

Using `--ipv6-subnet | -6` flag users can define a custom IPv6 subnet that containerlab will use to assign management IPv6 addresses.

#### max-workers

With `--max-workers` flag, it is possible to limit the number of concurrent workers that create/delete containers or wire virtual links. By default, the number of workers equals the number of nodes/links to process.

#### keep-mgmt-net

Do not try to remove the management network during destroy phase. Usually the management docker network (in case of docker) and the underlying bridge are being removed. If you have attached additional resources outside of containerlab and you want the bridge to remain intact just add the `--keep-mgmt-net` flag.

#### skip-post-deploy

The `--skip-post-deploy` flag can be used to skip the post-deploy phase of the lab deployment. This is a global flag that affects all nodes in the lab.

#### export-template

The local `--export-template` flag allows a user to specify a custom Go template that will be used for exporting topology data into `topology-data.json` file under the lab directory.

#### node-filter

The local `--node-filter` flag allows users to specify a subset of topology nodes targeted by `redeploy` command. The value of this flag is a comma-separated list of node names as they appear in the topology.

When a subset of nodes is specified, containerlab will only redeploy those nodes and their links and ignore the rest.

#### skip-labdir-acl

The `--skip-labdir-acl` flag can be used to skip the lab directory access control list (ACL) provisioning during the deploy phase.

### Examples

#### Redeploy a lab using the given topology file

```bash
containerlab redeploy -t mylab.clab.yml
```

#### Redeploy a lab with removing the Lab directory at destroy stage

```bash
containerlab redeploy --cleanup -t mylab.clab.yml
```

#### Redeploy a lab without specifying topology file

Given that a single topology file is present in the current directory.

```bash
containerlab redeploy
```

#### Redeploy a lab using short flag names

```bash
clab rdep -t mylab.clab.yml
```

#### Redeploy specific nodes in a lab

```bash
containerlab redeploy -t mylab.clab.yml --node-filter "node1,node2"
```
2 changes: 1 addition & 1 deletion links/link_mgmt-net.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func GetMgmtBrLinkNode() Node { // skipcq: RVV-B0001
return getMgmtBrLinkNode()
}

func SetMgmtNetUnderlayingBridge(bridge string) error {
func SetMgmtNetUnderlyingBridge(bridge string) error {
getMgmtBrLinkNode().GenericLinkNode.shortname = bridge
return nil
}
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ nav:
- Command reference:
- deploy: cmd/deploy.md
- destroy: cmd/destroy.md
- redeploy: cmd/redeploy.md
- inspect: cmd/inspect.md
- save: cmd/save.md
- exec: cmd/exec.md
Expand Down
10 changes: 10 additions & 0 deletions tests/01-smoke/02-destroy-all.robot
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ Verify ipv4-range is set correctly
Log ${output}
Should Contain ${output} 172.20.30.9

Redeploy second lab
${result} = Run Process
... sudo -E ${CLAB_BIN} --runtime ${runtime} redeploy -c -t ${CURDIR}/${lab2-file}
... cwd=/tmp # using a different cwd to check lab resolution via container labels
... shell=True
Log ${result.stdout}
Log ${result.stderr}
Should Be Equal As Integers ${result.rc} 0
Should Exist /tmp/clab-single-node

Destroy all labs
${rc} ${output} = Run And Return Rc And Output
... sudo -E ${CLAB_BIN} --runtime ${runtime} destroy --all --cleanup
Expand Down

0 comments on commit b6b637d

Please sign in to comment.