Skip to content

Commit b6b637d

Browse files
helltaxxyhtrxAnatoly Kolpakov
authored
redeploy command (srl-labs#2374)
* containerlab redeploy (srl-labs#2371) redeploy command Co-authored-by: Anatoly Kolpakov <[email protected]> * fix typo * remove reconfigure from the redeploy cmd * added a test --------- Co-authored-by: Anatoly Kolpakov <[email protected]> Co-authored-by: Anatoly Kolpakov <[email protected]>
1 parent 4f0d1cd commit b6b637d

File tree

10 files changed

+207
-7
lines changed

10 files changed

+207
-7
lines changed

clab/clab.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ func (c *CLab) Deploy(ctx context.Context, options *DeployOptions) ([]runtime.Ge
993993
return nil, err
994994
}
995995

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

12511251
// Exec execute commands on running topology nodes.
12521252
func (c *CLab) Exec(ctx context.Context, cmds []string, options *ExecOptions) (*exec.ExecCollection, error) {
1253-
err := links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
1253+
err := links.SetMgmtNetUnderlyingBridge(c.Config.Mgmt.Bridge)
12541254
if err != nil {
12551255
return nil, err
12561256
}

cmd/destroy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func destroyFn(_ *cobra.Command, _ []string) error {
126126
}
127127
}
128128

129-
err = links.SetMgmtNetUnderlayingBridge(nc.Config.Mgmt.Bridge)
129+
err = links.SetMgmtNetUnderlyingBridge(nc.Config.Mgmt.Bridge)
130130
if err != nil {
131131
return err
132132
}

cmd/redeploy.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import (
4+
"net"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// redeployCmd represents the redeploy command.
10+
var redeployCmd = &cobra.Command{
11+
Use: "redeploy",
12+
Short: "destroy and redeploy a lab",
13+
Long: "destroy a lab and deploy it again based on the topology definition file\nreference: https://containerlab.dev/cmd/redeploy/",
14+
Aliases: []string{"rdep"},
15+
PreRunE: sudoCheck,
16+
SilenceUsage: true,
17+
RunE: redeployFn,
18+
}
19+
20+
func init() {
21+
rootCmd.AddCommand(redeployCmd) // Add to rootCmd
22+
23+
// Add destroy flags
24+
redeployCmd.Flags().BoolVarP(&cleanup, "cleanup", "c", false, "delete lab directory")
25+
redeployCmd.Flags().BoolVarP(&graceful, "graceful", "", false,
26+
"attempt to stop containers before removing")
27+
redeployCmd.Flags().BoolVarP(&all, "all", "a", false, "destroy all containerlab labs")
28+
redeployCmd.Flags().UintVarP(&maxWorkers, "max-workers", "", 0,
29+
"limit the maximum number of workers creating/deleting nodes")
30+
redeployCmd.Flags().BoolVarP(&keepMgmtNet, "keep-mgmt-net", "", false, "do not remove the management network")
31+
redeployCmd.Flags().StringSliceVarP(&nodeFilter, "node-filter", "", []string{},
32+
"comma separated list of nodes to include")
33+
34+
// Add deploy flags
35+
redeployCmd.Flags().BoolVarP(&graph, "graph", "g", false, "generate topology graph")
36+
redeployCmd.Flags().StringVarP(&mgmtNetName, "network", "", "", "management network name")
37+
redeployCmd.Flags().IPNetVarP(&mgmtIPv4Subnet, "ipv4-subnet", "4", net.IPNet{}, "management network IPv4 subnet range")
38+
redeployCmd.Flags().IPNetVarP(&mgmtIPv6Subnet, "ipv6-subnet", "6", net.IPNet{}, "management network IPv6 subnet range")
39+
redeployCmd.Flags().StringVarP(&deployFormat, "format", "f", "table", "output format. One of [table, json]")
40+
redeployCmd.Flags().BoolVarP(&skipPostDeploy, "skip-post-deploy", "", false, "skip post deploy action")
41+
redeployCmd.Flags().StringVarP(&exportTemplate, "export-template", "", "",
42+
"template file for topology data export")
43+
redeployCmd.Flags().BoolVarP(&skipLabDirFileACLs, "skip-labdir-acl", "", false,
44+
"skip the lab directory extended ACLs provisioning")
45+
}
46+
47+
func redeployFn(cmd *cobra.Command, args []string) error {
48+
// First destroy the lab
49+
err := destroyFn(destroyCmd, args)
50+
if err != nil {
51+
return err
52+
}
53+
54+
// Then deploy it again
55+
return deployFn(deployCmd, args)
56+
}

cmd/save.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Refer to the https://containerlab.dev/cmd/save/ documentation to see the exact c
4646
return err
4747
}
4848

49-
err = links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
49+
err = links.SetMgmtNetUnderlyingBridge(c.Config.Mgmt.Bridge)
5050
if err != nil {
5151
return err
5252
}

docs/cmd/deploy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The `deploy` command spins up a lab using the topology expressed via [topology d
1616

1717
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.
1818

19-
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.
19+
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.
2020

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

docs/cmd/destroy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ To make containerlab attempt a graceful shutdown of the running containers, add
3636

3737
#### keep-mgmt-net
3838

39-
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.
39+
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.
4040

4141
#### all
4242

docs/cmd/redeploy.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# redeploy command
2+
3+
### Description
4+
5+
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.
6+
7+
The two most common applications of this command are:
8+
9+
1. Redeploying a lab while keeping the lab directory intact:
10+
11+
```bash
12+
sudo containerlab redeploy -t mylab.clab.yml
13+
```
14+
15+
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.
16+
17+
2. Redeploying a lab while removing the lab directory at the destroy stage:
18+
19+
```bash
20+
sudo containerlab redeploy --cleanup -t mylab.clab.yml
21+
```
22+
23+
or using the shorthands:
24+
25+
```bash
26+
sudo clab rdep -c -t mylab.clab.yml
27+
```
28+
29+
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.
30+
31+
### Usage
32+
33+
`containerlab [global-flags] redeploy [local-flags]`
34+
35+
**aliases:** `rdep`
36+
37+
### Flags
38+
39+
#### topology
40+
41+
With the global `--topo | -t` flag a user sets the path to the topology definition file that will be used to redeploy a lab.
42+
43+
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.
44+
45+
When the topology file flag is omitted, containerlab will try to find the matching file name by looking at the current working directory.
46+
47+
If more than one file is found for directory-based path or when the flag is omitted entirely, containerlab will fail with an error.
48+
49+
#### cleanup
50+
51+
The local `--cleanup | -c` flag instructs containerlab to remove the lab directory and all its content during the destroy phase.
52+
53+
Without this flag present, containerlab will keep the lab directory and all files inside of it.
54+
55+
#### graceful
56+
57+
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.
58+
59+
#### graph
60+
61+
The local `--graph | -g` flag instructs containerlab to generate a topology graph after deploying the lab.
62+
63+
#### network
64+
65+
With `--network` flag users can specify a custom name for the management network that containerlab creates for the lab.
66+
67+
#### ipv4-subnet
68+
69+
Using `--ipv4-subnet | -4` flag users can define a custom IPv4 subnet that containerlab will use to assign management IPv4 addresses.
70+
71+
#### ipv6-subnet
72+
73+
Using `--ipv6-subnet | -6` flag users can define a custom IPv6 subnet that containerlab will use to assign management IPv6 addresses.
74+
75+
#### max-workers
76+
77+
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.
78+
79+
#### keep-mgmt-net
80+
81+
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.
82+
83+
#### skip-post-deploy
84+
85+
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.
86+
87+
#### export-template
88+
89+
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.
90+
91+
#### node-filter
92+
93+
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.
94+
95+
When a subset of nodes is specified, containerlab will only redeploy those nodes and their links and ignore the rest.
96+
97+
#### skip-labdir-acl
98+
99+
The `--skip-labdir-acl` flag can be used to skip the lab directory access control list (ACL) provisioning during the deploy phase.
100+
101+
### Examples
102+
103+
#### Redeploy a lab using the given topology file
104+
105+
```bash
106+
containerlab redeploy -t mylab.clab.yml
107+
```
108+
109+
#### Redeploy a lab with removing the Lab directory at destroy stage
110+
111+
```bash
112+
containerlab redeploy --cleanup -t mylab.clab.yml
113+
```
114+
115+
#### Redeploy a lab without specifying topology file
116+
117+
Given that a single topology file is present in the current directory.
118+
119+
```bash
120+
containerlab redeploy
121+
```
122+
123+
#### Redeploy a lab using short flag names
124+
125+
```bash
126+
clab rdep -t mylab.clab.yml
127+
```
128+
129+
#### Redeploy specific nodes in a lab
130+
131+
```bash
132+
containerlab redeploy -t mylab.clab.yml --node-filter "node1,node2"
133+
```

links/link_mgmt-net.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func GetMgmtBrLinkNode() Node { // skipcq: RVV-B0001
155155
return getMgmtBrLinkNode()
156156
}
157157

158-
func SetMgmtNetUnderlayingBridge(bridge string) error {
158+
func SetMgmtNetUnderlyingBridge(bridge string) error {
159159
getMgmtBrLinkNode().GenericLinkNode.shortname = bridge
160160
return nil
161161
}

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ nav:
8585
- Command reference:
8686
- deploy: cmd/deploy.md
8787
- destroy: cmd/destroy.md
88+
- redeploy: cmd/redeploy.md
8889
- inspect: cmd/inspect.md
8990
- save: cmd/save.md
9091
- exec: cmd/exec.md

tests/01-smoke/02-destroy-all.robot

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ Verify ipv4-range is set correctly
9494
Log ${output}
9595
Should Contain ${output} 172.20.30.9
9696

97+
Redeploy second lab
98+
${result} = Run Process
99+
... sudo -E ${CLAB_BIN} --runtime ${runtime} redeploy -c -t ${CURDIR}/${lab2-file}
100+
... cwd=/tmp # using a different cwd to check lab resolution via container labels
101+
... shell=True
102+
Log ${result.stdout}
103+
Log ${result.stderr}
104+
Should Be Equal As Integers ${result.rc} 0
105+
Should Exist /tmp/clab-single-node
106+
97107
Destroy all labs
98108
${rc} ${output} = Run And Return Rc And Output
99109
... sudo -E ${CLAB_BIN} --runtime ${runtime} destroy --all --cleanup

0 commit comments

Comments
 (0)