-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatcha.go
More file actions
84 lines (75 loc) · 2.31 KB
/
batcha.go
File metadata and controls
84 lines (75 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package batcha
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/batch"
batchTypes "github.com/aws/aws-sdk-go-v2/service/batch/types"
"github.com/fujiwara/tfstate-lookup/tfstate"
goconfig "github.com/kayac/go-config"
)
// Version is set by goreleaser via ldflags.
var Version = "dev"
// App is the main application struct.
type App struct {
config *Config
configPath string
}
// New creates a new App by loading the config file.
func New(ctx context.Context, configPath string) (*App, error) {
cfg, err := LoadConfig(configPath)
if err != nil {
return nil, err
}
return &App{config: cfg, configPath: configPath}, nil
}
// newBatchClient creates an AWS Batch client from the app's config region.
func (app *App) newBatchClient(ctx context.Context) (*batch.Client, error) {
awsCfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(app.config.Region))
if err != nil {
return nil, err
}
return batch.NewFromConfig(awsCfg), nil
}
// setupPlugins configures the go-config loader with tfstate FuncMaps.
func setupPlugins(ctx context.Context, cfg *Config, loader *goconfig.Loader) error {
for _, p := range cfg.Plugins {
if p.Name != "tfstate" {
continue
}
funcMap, err := tfstate.FuncMap(ctx, p.Config.URL)
if err != nil {
return fmt.Errorf("failed to load tfstate from %s: %w", p.Config.URL, err)
}
loader.Funcs(funcMap)
}
return nil
}
// pickLatestRevision returns the job definition with the highest revision.
func pickLatestRevision(defs []batchTypes.JobDefinition) batchTypes.JobDefinition {
latest := defs[0]
for _, d := range defs[1:] {
if aws.ToInt32(d.Revision) > aws.ToInt32(latest.Revision) {
latest = d
}
}
return latest
}
// normalizeRemoteDefinition converts an AWS job definition to a comparable
// map by stripping AWS-managed fields.
func normalizeRemoteDefinition(def batchTypes.JobDefinition) (map[string]any, error) {
b, err := json.Marshal(def)
if err != nil {
return nil, fmt.Errorf("failed to marshal remote definition: %w", err)
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
return nil, fmt.Errorf("failed to unmarshal remote definition: %w", err)
}
for _, key := range initExcludeKeys {
delete(m, key)
}
return m, nil
}