Skip to content

Commit 95d3ffe

Browse files
thomas-tacquetnerda-codes
authored andcommitted
feat(jobs): update snapshot tutorial (scaleway#4291)
* feat(jobs): update snapshot tutorial * Update tutorials/snapshot-instances-jobs/index.mdx Co-authored-by: Néda <[email protected]> --------- Co-authored-by: Néda <[email protected]>
1 parent 1941474 commit 95d3ffe

File tree

1 file changed

+37
-53
lines changed

1 file changed

+37
-53
lines changed

tutorials/snapshot-instances-jobs/index.mdx

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,33 @@ You can also download the work files by cloning our [Scaleway Serverless example
4040
import (
4141
"fmt"
4242
"os"
43+
"time"
4344

4445
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
4546
"github.com/scaleway/scaleway-sdk-go/scw"
4647
)
4748

49+
const (
50+
envOrgID = "SCW_DEFAULT_ORGANIZATION_ID"
51+
envAccessKey = "SCW_ACCESS_KEY"
52+
envSecretKey = "SCW_SECRET_KEY"
53+
envInstanceID = "INSTANCE_ID"
54+
envInstanceZone = "INSTANCE_ZONE"
55+
)
56+
4857
func main() {
4958
fmt.Println("creating snapshot of instance...")
5059

5160
// Create a Scaleway client with credentials from environment variables.
5261
client, err := scw.NewClient(
5362
// Get your organization ID at https://console.scaleway.com/organization/settings
54-
scw.WithDefaultOrganizationID(os.Getenv("SCW_DEFAULT_ORGANIZATION_ID")),
63+
scw.WithDefaultOrganizationID(os.Getenv(envOrgID)),
5564

5665
// Get your credentials at https://console.scaleway.com/iam/api-keys
57-
scw.WithAuth(os.Getenv("SCW_ACCESS_KEY"), os.Getenv("SCW_SECRET_KEY")),
66+
scw.WithAuth(os.Getenv(envAccessKey), os.Getenv(envSecretKey)),
5867

59-
// Get more about our availability zones at https://www.scaleway.com/en/docs/my-account/reference-content/products-availability/
68+
// Get more about our availability
69+
// zones at https://www.scaleway.com/en/docs/account/reference-content/products-availability/
6070
scw.WithDefaultRegion(scw.RegionFrPar),
6171
)
6272
if err != nil {
@@ -80,53 +90,50 @@ You can also download the work files by cloning our [Scaleway Serverless example
8090
return fmt.Errorf("error while getting instance %w", err)
8191
}
8292

93+
now := time.Now().Format(time.DateOnly)
94+
8395
for _, volume := range gotInstance.Server.Volumes {
96+
snapshotName := fmt.Sprintf("snap-vol-%s-%s-%s",
97+
volume.VolumeType.String(),
98+
now,
99+
os.Getenv(envInstanceZone))
100+
84101
snapshotResp, err := instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{
85-
Name: volume.Name + RandomString(4),
102+
Name: snapshotName,
86103
VolumeID: &volume.ID,
87-
VolumeType: instance.SnapshotVolumeTypeBSSD,
88-
Zone: scw.Zone(os.Getenv("INSTANCE_ZONE")),
104+
VolumeType: instance.SnapshotVolumeType(volume.VolumeType),
105+
Zone: scw.Zone(os.Getenv(envInstanceZone)),
89106
})
90107
if err != nil {
91-
return fmt.Errorf("error while creating snapshopt %w", err)
108+
return fmt.Errorf("error while creating snapshot %w", err)
92109
}
110+
93111
fmt.Println("created snapshot ", snapshotResp.Snapshot.ID)
94112
}
95113

96114
return nil
97115
}
98116

99117
func init() {
100-
if os.Getenv("SCW_DEFAULT_ORGANIZATION_ID") == "" {
101-
panic("missing SCW_DEFAULT_ORGANIZATION_ID")
102-
}
103-
104-
if os.Getenv("SCW_ACCESS_KEY") == "" {
105-
panic("missing SCW_ACCESS_KEY")
106-
}
118+
mandatoryVariables := [...]string{envOrgID, envAccessKey, envSecretKey, envInstanceID, envInstanceZone}
107119

108-
if os.Getenv("SCW_SECRET_KEY") == "" {
109-
panic("missing SCW_SECRET_KEY")
110-
}
111-
112-
if os.Getenv("INSTANCE_ID") == "" {
113-
panic("missing INSTANCE_ID")
114-
}
115-
116-
if os.Getenv("INSTANCE_ZONE") == "" {
117-
panic("missing INSTANCE_ZONE")
120+
for idx := range mandatoryVariables {
121+
if os.Getenv(mandatoryVariables[idx]) == "" {
122+
panic("missing environment variable " + mandatoryVariables[idx])
123+
}
118124
}
119125
}
126+
120127
```
121128

122129
2. Create a file called `go.mod`, and add the code below to it:
123130

124131
```go
125132
module github.com/scaleway/serverless-examples/jobs/instances-snapshot
126133

127-
go 1.22.2
134+
go 1.23
128135

129-
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21
136+
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30
130137

131138
require gopkg.in/yaml.v2 v2.4.0 // indirect
132139
```
@@ -137,32 +144,6 @@ You can also download the work files by cloning our [Scaleway Serverless example
137144
go get
138145
```
139146

140-
4. Create a file named `string.go`, and add the code below to it:
141-
142-
```go
143-
package main
144-
145-
import "crypto/rand"
146-
147-
// RandomString is used to generate a random string containing upper and lower characters
148-
// + number, of size n.
149-
func RandomString(n int) string {
150-
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
151-
152-
bytes := make([]byte, n)
153-
154-
if _, err := rand.Read(bytes); err != nil {
155-
panic(err)
156-
}
157-
158-
for i, b := range bytes {
159-
bytes[i] = letters[b%byte(len(letters))]
160-
}
161-
162-
return string(bytes)
163-
}
164-
```
165-
166147
## Building and pushing the image to Container Registry
167148

168149
Serverless Jobs rely on containers to run in the cloud, and therefore require a [container image](/serverless-jobs/concepts/#container-image) hosted in the cloud using [Scaleway Container Registry](/container-registry/).
@@ -171,7 +152,7 @@ Serverless Jobs rely on containers to run in the cloud, and therefore require a
171152

172153
```dockerfile
173154
# Using apline/golang image
174-
FROM golang:1.22-alpine
155+
FROM golang:1.23-alpine
175156

176157
# Set destination for COPY
177158
WORKDIR /app
@@ -198,6 +179,9 @@ Serverless Jobs rely on containers to run in the cloud, and therefore require a
198179

199180
```sh
200181
docker build -t rg.fr-par.scw.cloud/your-namespace-name/jobs-snapshot:v1 .
182+
183+
## TIP: for Apple Silicon or other ARM processors, please use the following command as Serverless Jobs supports amd64 architecture
184+
# docker buildx build --platform linux/amd64 -t rg.fr-par.scw.cloud/jobs-snapshot/jobs-snapshot:v1 .
201185
```
202186

203187
4. Run the following command to push the container image to the registry:

0 commit comments

Comments
 (0)