Skip to content

Commit 0a2c4f2

Browse files
feat(jobs-snapshot): various improvements and fixes (#93)
* feat(jobs-snapshot): various improvements and fixes * Update jobs/instances-snapshot/README.md Co-authored-by: Machavoine Rémy <52951124+RemyMach@users.noreply.github.com> * fix sdk --------- Co-authored-by: Machavoine Rémy <52951124+RemyMach@users.noreply.github.com>
1 parent 87c9569 commit 0a2c4f2

File tree

6 files changed

+51
-56
lines changed

6 files changed

+51
-56
lines changed

jobs/instances-snapshot/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Using apline/golang image
2-
FROM golang:1.22-alpine
2+
FROM golang:1.23-alpine
33

44
# Set destination for COPY
55
WORKDIR /app

jobs/instances-snapshot/README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This example is very simple, it generates snapshots of your desired Instance.
1515
## Step 1 : Build and push to Container registry
1616

1717
Serverless Jobs, like Serverless Containers (which are suited for HTTP applications), works
18-
with Containers. So first, using your terminal reach this folder and run the following commands:
18+
with containers. So first, use your terminal reach this folder and run the following commands:
1919

2020
```shell
2121
# First command is to login to container registry, you can find it in Scaleway console
@@ -24,14 +24,16 @@ docker login rg.fr-par.scw.cloud/jobs-snapshot -u nologin --password-stdin <<< "
2424
# Here we build the image to push
2525
docker build -t rg.fr-par.scw.cloud/jobs-snapshot/jobs-snapshot:v1 .
2626

27+
## TIP: for Apple Silicon or other ARM processors, please use the following command as Serverless Jobs supports amd64 architecture
28+
# docker buildx build --platform linux/amd64 -t rg.fr-par.scw.cloud/jobs-snapshot/jobs-snapshot:v1 .
29+
2730
# Push the image online to be used on Serverless Jobs
2831
docker push rg.fr-par.scw.cloud/jobs-snapshot/jobs-snapshot:v1
2932
```
33+
> [!TIP]
34+
> As we do not expose a web server and we do not require features such as auto-scaling, Serverless Jobs are perfect for this use case.
3035
31-
Note about Serverless Containers versus Serverless Jobs. As we do not expose a web server and we do not
32-
require features such as auto-scaling, Serverless Jobs are perfect for this use case.
33-
34-
To check if everyting is ok, on the Console you can verify if your tag is present in Container Registry.
36+
To check if everyting is ok, on the Scaleway Console you can verify if your tag is present in Container Registry.
3537

3638
## Step 2: Creating the Job Definition
3739

@@ -44,25 +46,33 @@ On Scaleway Console on the following link you can create a new Job Definition: h
4446
1. To schedule your job for example every night at 2am, you can set the cron to `0 2 * * *`.
4547
1. Important: advanced option, you need to set the following environment variables:
4648

49+
> [!TIP]
50+
> For sensitive data like `SCW_ACCESS_KEY` and `SCW_SECRET_KEY` we recommend to inject them via Secret Manager, [more info here](https://www.scaleway.com/en/docs/serverless/jobs/how-to/reference-secret-in-job/).
51+
4752
- `INSTANCE_ID`: grab the instance ID you want to create snapshots from
4853
- `INSTANCE_ZONE`: you need to give the ZONE of you instance, like `fr-par-2`
4954
- `SCW_ACCESS_KEY`: your access key
5055
- `SCW_SECRET_KEY`: your secret key
5156
- `SCW_DEFAULT_ORGANIZATION_ID`: your organzation ID
5257

53-
1. Click "create job"
58+
* Then click "create job"
5459

5560
## Step 3: Run the job
5661

5762
On your created Job Definition, just click the button "Run Job" and within seconds it should be successful.
5863

64+
## Troubleshooting
65+
66+
If your Job Run state goes in error, you can use the "Logs" tab in Scaleway Console to get more informations about the error.
67+
5968
# Possible improvements
6069

6170
You can exercice by adding the following features:
6271

6372
- Instead of managing a single instance, make it account wide
6473
- Add disk backups
6574
- Add alerts if something goes wrong
75+
- Use secret manager instead of job environment variables
6676

6777
# Additional content
6878

jobs/instances-snapshot/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/scaleway/serverless-examples/jobs/instances-snapshot
22

3-
go 1.22.2
3+
go 1.23
44

5-
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21
5+
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32
66

77
require gopkg.in/yaml.v2 v2.4.0 // indirect

jobs/instances-snapshot/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
22
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
3-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21 h1:yWfiTPwYxB0l5fGMhl/G+liULugVIHD9AU77iNLrURQ=
4-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.21/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
3+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32 h1:4+LP7qmsLSGbmc66m1s5dKRMBwztRppfxFKlYqYte/c=
4+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32/go.mod h1:kzh+BSAvpoyHHdHBCDhmSWtBc1NbLMZ2lWHqnBoxFks=
55
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
66
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
77
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

jobs/instances-snapshot/main.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"time"
67

78
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
89
"github.com/scaleway/scaleway-sdk-go/scw"
910
)
1011

12+
const (
13+
envOrgID = "SCW_DEFAULT_ORGANIZATION_ID"
14+
envAccessKey = "SCW_ACCESS_KEY"
15+
envSecretKey = "SCW_SECRET_KEY"
16+
envInstanceID = "INSTANCE_ID"
17+
envInstanceZone = "INSTANCE_ZONE"
18+
)
19+
1120
func main() {
1221
fmt.Println("creating snapshot of instance...")
1322

1423
// Create a Scaleway client with credentials from environment variables.
1524
client, err := scw.NewClient(
1625
// Get your organization ID at https://console.scaleway.com/organization/settings
17-
scw.WithDefaultOrganizationID(os.Getenv("SCW_DEFAULT_ORGANIZATION_ID")),
26+
scw.WithDefaultOrganizationID(os.Getenv(envOrgID)),
1827

1928
// Get your credentials at https://console.scaleway.com/iam/api-keys
20-
scw.WithAuth(os.Getenv("SCW_ACCESS_KEY"), os.Getenv("SCW_SECRET_KEY")),
29+
scw.WithAuth(os.Getenv(envAccessKey), os.Getenv(envSecretKey)),
2130

22-
// Get more about our availability zones at https://www.scaleway.com/en/docs/console/my-account/reference-content/products-availability/
31+
// Get more about our availability
32+
// zones at https://www.scaleway.com/en/docs/console/my-account/reference-content/products-availability/
2333
scw.WithDefaultRegion(scw.RegionFrPar),
2434
)
2535
if err != nil {
@@ -43,40 +53,36 @@ func createSnapshots(instanceAPI *instance.API) error {
4353
return fmt.Errorf("error while getting instance %w", err)
4454
}
4555

56+
now := time.Now().Format(time.DateOnly)
57+
4658
for _, volume := range gotInstance.Server.Volumes {
59+
snapshotName := fmt.Sprintf("snap-vol-%s-%s-%s",
60+
volume.VolumeType.String(),
61+
now,
62+
os.Getenv(envInstanceZone))
63+
4764
snapshotResp, err := instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{
48-
Name: volume.Name + RandomString(4),
65+
Name: snapshotName,
4966
VolumeID: &volume.ID,
50-
VolumeType: instance.SnapshotVolumeTypeBSSD,
51-
Zone: scw.Zone(os.Getenv("INSTANCE_ZONE")),
67+
VolumeType: instance.SnapshotVolumeType(volume.VolumeType),
68+
Zone: scw.Zone(os.Getenv(envInstanceZone)),
5269
})
5370
if err != nil {
54-
return fmt.Errorf("error while creating snapshopt %w", err)
71+
return fmt.Errorf("error while creating snapshot %w", err)
5572
}
73+
5674
fmt.Println("created snapshot ", snapshotResp.Snapshot.ID)
5775
}
5876

5977
return nil
6078
}
6179

6280
func init() {
63-
if os.Getenv("SCW_DEFAULT_ORGANIZATION_ID") == "" {
64-
panic("missing SCW_DEFAULT_ORGANIZATION_ID")
65-
}
81+
mandatoryVariables := [...]string{envOrgID, envAccessKey, envSecretKey, envInstanceID, envInstanceZone}
6682

67-
if os.Getenv("SCW_ACCESS_KEY") == "" {
68-
panic("missing SCW_ACCESS_KEY")
69-
}
70-
71-
if os.Getenv("SCW_SECRET_KEY") == "" {
72-
panic("missing SCW_SECRET_KEY")
73-
}
74-
75-
if os.Getenv("INSTANCE_ID") == "" {
76-
panic("missing INSTANCE_ID")
77-
}
78-
79-
if os.Getenv("INSTANCE_ZONE") == "" {
80-
panic("missing INSTANCE_ZONE")
83+
for idx := range mandatoryVariables {
84+
if os.Getenv(mandatoryVariables[idx]) == "" {
85+
panic("missing environment variable " + mandatoryVariables[idx])
86+
}
8187
}
8288
}

jobs/instances-snapshot/string.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)