Skip to content

Commit 528f9f2

Browse files
authored
docs: add more example in cookbook (#2977)
1 parent 5959fd7 commit 528f9f2

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

docs/cookbook.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,87 @@
33
This file regroups some useful combinations of different commands to efficiently manage your Scaleway resources.
44
Do not hesitate to open a PR and share your favorite recipes.
55

6+
## General
7+
8+
### Retrieve specific field
9+
10+
```bash
11+
## Retrieve all available instance type names
12+
13+
# Using jq
14+
scw -o json instance server-type list | jq -r '.[].name'
15+
# Using CLI templates
16+
scw instance server-type list -o template="{{ .Name }}"
17+
```
18+
19+
### Filter output using jq
20+
21+
```bash
22+
# Retrieve all available instance type with GPUs
23+
scw -o json instance server-type list | jq '.[] | select (.gpu > 0)'```
24+
```
25+
26+
### Parallelize actions using xargs
27+
28+
```bash
29+
## Reboot all listed servers at the same time, 8 max concurrent actions
30+
31+
# Using jq
32+
scw -o json instance server list | jq -r '.[].id' | xargs -L1 -P8 scw instance server reboot
33+
# Using CLI templates
34+
scw instance server list -o template="{{ .ID }}" | xargs -L1 -P8 scw instance server reboot
35+
```
36+
37+
### Create arguments for a cli command and use it in xargs
38+
39+
```bash
40+
## List private-nics of all listed servers
41+
42+
# Using jq
43+
scw -o json instance server list | jq -r '.[] | "server-id=\(.id)"' | xargs -L1 scw instance private-nic list
44+
# Using CLI templates
45+
scw instance server list -o template="server-id={{ .ID }}" | xargs -L1 scw instance private-nic list
46+
```
47+
648
## Instance
749

850
### Start/Stop a group of servers based on a tag
951
```bash
1052
# Start all servers with tag staging
11-
scw -o json instance server list tags.0=staging | jq -r .[].id | xargs scw instance server start -w
53+
scw -o json instance server list tags.0=staging | jq -r '.[].id' | xargs scw instance server start -w
1254
1355
# Stop all servers with tag staging
14-
scw -o json instance server list tags.0=staging | jq -r .[].id | xargs scw instance server stop -w
56+
scw -o json instance server list tags.0=staging | jq -r '.[].id' | xargs scw instance server stop -w
57+
```
58+
59+
### Servers and private networks
60+
61+
```bash
62+
# Add all listed servers to a given private network
63+
scw -o json instance server list tags.0=staging | jq '.[].id' | xargs -t -I{} scw instance private-nic create private-network-id=<pn-id> server-id={}
64+
65+
# List all servers in a specific private network
66+
scw instance server list -ojson | jq 'map(select (.private_nics | map(select (.private_network_id == "<pn-id>")) | length == 1))'
67+
# List all servers not in a specific private network
68+
scw instance server list -ojson | jq 'map(select (.private_nics | map(select (.private_network_id == "<pn-id>")) | length == 0))'
69+
```
70+
71+
### Action on servers across multiple zones
72+
73+
```bash
74+
## Reboot all servers across all zones, 8 server at a time
75+
76+
# Using jq
77+
scw -o json instance server list zone=all | jq -r '.[] | "\(.id) zone=\(.zone)"' | xargs -P8 -L1 scw instance server reboot
78+
# Using CLI templates
79+
scw instance server list zone=all -o template="{{.ID}} zone={{.Zone}}" | xargs -P8 -L1 scw instance server reboot
80+
```
81+
82+
## Database
83+
84+
### Filter backups by date
85+
86+
```bash
87+
# Get all backup older than 7 days
88+
scw rdb backup list -ojson | jq --arg d "$(date -d "7 days ago" --utc --iso-8601=ns)" '.[] | select (.created_at < $d)'
1589
```

0 commit comments

Comments
 (0)