carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "bytes"
5 "context"
6 "strings"
7
8 "github.com/aws/aws-sdk-go-v2/aws"
9 awsconfig "github.com/aws/aws-sdk-go-v2/config"
10 "github.com/aws/aws-sdk-go-v2/service/s3"
11 "github.com/carlosframework/platform/internal/hostbox"
12 "github.com/carlosframework/platform/internal/hostwrite"
13 "github.com/carlosframework/platform/internal/rootaws"
14)
15
16type productionClaimMarkers struct {
17 Client *s3.Client
18 Bucket string
19}
20
21func newProductionClaimMarkers(ctx context.Context, bucket, region string) (ClaimMarkerStore, error) {
22 cfg, err := rootaws.LoadDefaultConfig(ctx, awsconfig.WithRegion(region), awsconfig.WithRetryMaxAttempts(1))
23 if err != nil {
24 return nil, err
25 }
26 return productionClaimMarkers{Client: s3.NewFromConfig(cfg), Bucket: bucket}, nil
27}
28func (m productionClaimMarkers) PutMarker(ctx context.Context, bucket, key string, body []byte, expected string, check func(context.Context) error) (string, error) {
29 if m.Client == nil || bucket != m.Bucket || check == nil || !strings.HasPrefix(key, "instances/") || !strings.HasSuffix(key, "/generation.json") {
30 return "", hostwrite.ErrUnavailable
31 }
32 bounded, cancel := context.WithTimeout(ctx, hostbox.RequestLimit)
33 defer cancel()
34 if err := check(bounded); err != nil {
35 return "", err
36 }
37 in := &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String(key), Body: bytes.NewReader(body)}
38 if expected == "" {
39 in.IfNoneMatch = aws.String("*")
40 } else {
41 in.IfMatch = aws.String(expected)
42 }
43 out, err := m.Client.PutObject(bounded, in)
44 if err != nil {
45 return "", err
46 }
47 if out.VersionId == nil || *out.VersionId == "" || *out.VersionId == "null" {
48 return "", hostwrite.ErrUnavailable
49 }
50 return *out.VersionId, nil
51}
52