Skip to content

Commit 5a3915a

Browse files
authored
Add script to create images from snapshot (#24)
1 parent bf12833 commit 5a3915a

File tree

3 files changed

+180
-2
lines changed

3 files changed

+180
-2
lines changed

.github/workflows/lint.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ jobs:
1717
with:
1818
go-version-file: go.mod
1919

20-
- name: Run linter
20+
- name: Run golangci-lint
2121
uses: golangci/golangci-lint-action@v7
2222
with:
2323
install-mode: goinstall
2424
version: v2.0.2
25+
26+
- name: Run ShellCheck
27+
uses: ludeeus/action-shellcheck@master
28+
29+
- name: Running nilaway
30+
run: |
31+
make lint-nilaway

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,15 @@ cleanup-test-e2e: ## Tear down the Kind cluster used for e2e tests
9999
@$(KIND) delete cluster --name $(KIND_CLUSTER) --kubeconfig $(KIND_KUBECONFIG)
100100

101101
.PHONY: lint
102-
lint: golangci-lint ## Run golangci-lint linter
102+
lint: lint-golangci-lint lint-nilaway ## Run linters
103+
@echo "done"
104+
105+
.PHONY: lint-golangci-lint
106+
lint-golangci-lint: golangci-lint ## Run golangci-lint linter
103107
$(GOLANGCI_LINT) run
108+
109+
.PHONY: lint-nilaway
110+
lint-nilaway: nilaway ## Run nilaway linter
104111
$(NILAWAY) -include-pkgs=github.com/scaleway/cluster-api-provider-scaleway ./...
105112

106113
.PHONY: lint-fix

hack/create-images-from-snapshot.sh

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Default values
5+
BUCKET_NAME=""
6+
TARGET_ZONES=""
7+
TARGET_API="block"
8+
IMAGE_NAME=""
9+
SNAPSHOT_ID=""
10+
SNAPSHOT_ZONE="fr-par-1"
11+
SNAPSHOT_API="block"
12+
PROJECT_ID=""
13+
ARCH="x86_64"
14+
15+
print_help() {
16+
echo "Usage: $0 [options]"
17+
echo "Options:"
18+
echo " Transient resources parameters:"
19+
echo " -b, --bucket-name BUCKET_NAME Specify a bucket name (the bucket must be in the same region as the snapshot)"
20+
echo " Source snapshot parameters:"
21+
echo " -s, --snapshot-id SNAPSHOT_ID Specify the ID of the snapshot that will be used to create images"
22+
echo " -z, --snapshot-zone SNAPSHOT_ZONE Specify the zone of the snapshot that will be used to create images"
23+
echo " -a, --snapshot-api SNAPSHOT_API Specify the API where the snapshot is present (block or instance)"
24+
echo " Output images parameters:"
25+
echo " -c, --arch ARCH Specify the CPU architecture"
26+
echo " -p, --project-id PROJECT_ID Specify the project ID where the images will be created"
27+
echo " -i, --image-name IMAGE_NAME Specify the name of the images to create"
28+
echo " -o, --target-api TARGET_API Specify the target API, where snapshots will be created (block or instance)"
29+
echo " -t, --target-zones TARGET_ZONES Specify the target zones, separated by comma"
30+
echo " Misc:"
31+
echo " -h, --help Show this help message"
32+
}
33+
34+
# Parse flags
35+
while [[ "$#" -gt 0 ]]; do
36+
case "$1" in
37+
-b|--bucket-name)
38+
BUCKET_NAME="$2"
39+
shift 2
40+
;;
41+
-t|--target-zones)
42+
TARGET_ZONES="$2"
43+
shift 2
44+
;;
45+
-o|--target-api)
46+
TARGET_API="$2"
47+
shift 2
48+
;;
49+
-i|--image-name)
50+
IMAGE_NAME="$2"
51+
shift 2
52+
;;
53+
-s|--snapshot-id)
54+
SNAPSHOT_ID="$2"
55+
shift 2
56+
;;
57+
-z|--snapshot-zone)
58+
SNAPSHOT_ZONE="$2"
59+
shift 2
60+
;;
61+
-a|--snapshot-api)
62+
SNAPSHOT_API="$2"
63+
shift 2
64+
;;
65+
-p|--project-id)
66+
PROJECT_ID="$2"
67+
shift 2
68+
;;
69+
-c|--arch)
70+
ARCH="$2"
71+
shift 2
72+
;;
73+
-h|--help)
74+
print_help
75+
exit 0
76+
;;
77+
*)
78+
echo "Unknown option: $1"
79+
print_help
80+
exit 1
81+
;;
82+
esac
83+
done
84+
85+
if [[ -z "$IMAGE_NAME" ]]; then
86+
echo "No image name specified"
87+
exit 1
88+
fi
89+
90+
# Export snapshot to S3.
91+
92+
if [[ -z "$BUCKET_NAME" ]]; then
93+
echo "No bucket name specified"
94+
exit 1
95+
fi
96+
97+
if [[ -z "$SNAPSHOT_ID" ]]; then
98+
echo "No snapshot ID specified"
99+
exit 1
100+
fi
101+
102+
if [[ -z "$SNAPSHOT_ZONE" ]]; then
103+
echo "No snapshot zone specified"
104+
exit 1
105+
fi
106+
107+
echo "Uploading snapshot to S3"
108+
109+
case "$SNAPSHOT_API" in
110+
111+
block)
112+
echo " ... Exporting block snapshot to bucket"
113+
scw block snapshot export-to-object-storage snapshot-id="${SNAPSHOT_ID}" bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" zone="${SNAPSHOT_ZONE}" > /dev/null
114+
echo " ... Waiting for export to finish"
115+
scw block snapshot wait "${SNAPSHOT_ID}" zone="${SNAPSHOT_ZONE}" > /dev/null
116+
;;
117+
118+
instance)
119+
echo " ... Exporting instance snapshot to bucket"
120+
scw instance snapshot export snapshot-id="${SNAPSHOT_ID}" bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" zone="${SNAPSHOT_ZONE}" > /dev/null
121+
echo " ... Waiting for export to finish"
122+
scw instance snapshot wait "${SNAPSHOT_ID}" zone="${SNAPSHOT_ZONE}" > /dev/null
123+
;;
124+
125+
*)
126+
echo "unknown snapshot API"
127+
exit 1
128+
;;
129+
esac
130+
131+
# Import snapshot from S3
132+
133+
export IFS=","
134+
for target_zone in ${TARGET_ZONES}; do
135+
136+
echo "Importing snapshot to ${target_zone} from bucket"
137+
138+
case "$TARGET_API" in
139+
140+
block)
141+
echo " ... Importing block snapshot to ${target_zone} from bucket"
142+
imported_snapshot_id=$(scw block snapshot import-from-object-storage bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" name="${IMAGE_NAME}" project-id="${PROJECT_ID}" zone="${target_zone}" -o template="{{ .ID }}")
143+
echo " ... Waiting for import to finish"
144+
scw block snapshot wait "${imported_snapshot_id}" zone="${target_zone}" > /dev/null
145+
;;
146+
147+
instance)
148+
echo " ... Importing instance snapshot to ${target_zone} from bucket"
149+
imported_snapshot_id=$(scw instance snapshot create bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" name="${IMAGE_NAME}" project-id="${PROJECT_ID}" zone="${target_zone}" volume-type=l_ssd -o template="{{ .Snapshot.ID }}")
150+
echo " ... Waiting for import to finish"
151+
scw instance snapshot wait "${imported_snapshot_id}" zone="${target_zone}" > /dev/null
152+
;;
153+
154+
*)
155+
echo "unknown snapshot API"
156+
exit 1
157+
;;
158+
esac
159+
160+
echo " ... Creating instance image in ${target_zone}"
161+
scw instance image create name="${IMAGE_NAME}" snapshot-id="${imported_snapshot_id}" project-id="${PROJECT_ID}" arch="${ARCH}" zone="${target_zone}" > /dev/null
162+
done
163+
164+
echo "Images were successfully created. The S3 bucket needs to be cleaned manually."

0 commit comments

Comments
 (0)