-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathpro.go
More file actions
282 lines (225 loc) · 7.65 KB
/
pro.go
File metadata and controls
282 lines (225 loc) · 7.65 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package exec
import (
"errors"
"fmt"
"os"
"runtime"
"github.com/cloudposse/atmos/pkg/perf"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
git "github.com/cloudposse/atmos/pkg/git"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/pro"
"github.com/cloudposse/atmos/pkg/pro/dtos"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui/theme"
u "github.com/cloudposse/atmos/pkg/utils"
pkgversion "github.com/cloudposse/atmos/pkg/version"
"github.com/spf13/cobra"
)
type ProLockUnlockCmdArgs struct {
Component string
Stack string
AtmosConfig schema.AtmosConfiguration
}
type ProLockCmdArgs struct {
ProLockUnlockCmdArgs
LockMessage string
LockTTL int32
}
type ProUnlockCmdArgs struct {
ProLockUnlockCmdArgs
}
func parseLockUnlockCliArgs(cmd *cobra.Command, args []string) (ProLockUnlockCmdArgs, error) {
info, err := ProcessCommandLineArgs("terraform", cmd, args, nil)
if err != nil {
return ProLockUnlockCmdArgs{}, errors.Join(errUtils.ErrFailedToProcessArgs, err)
}
// InitCliConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
atmosConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return ProLockUnlockCmdArgs{}, errors.Join(errUtils.ErrFailedToInitConfig, err)
}
flags := cmd.Flags()
component, err := flags.GetString("component")
if err != nil {
return ProLockUnlockCmdArgs{}, errors.Join(errUtils.ErrFailedToGetComponentFlag, err)
}
stack, err := flags.GetString("stack")
if err != nil {
return ProLockUnlockCmdArgs{}, errors.Join(errUtils.ErrFailedToGetStackFlag, err)
}
if component == "" || stack == "" {
return ProLockUnlockCmdArgs{}, errUtils.ErrComponentAndStackRequired
}
result := ProLockUnlockCmdArgs{
Component: component,
Stack: stack,
AtmosConfig: atmosConfig,
}
return result, nil
}
func parseLockCliArgs(cmd *cobra.Command, args []string) (ProLockCmdArgs, error) {
commonArgs, err := parseLockUnlockCliArgs(cmd, args)
if err != nil {
return ProLockCmdArgs{}, err
}
flags := cmd.Flags()
ttl, err := flags.GetInt32("ttl")
if err != nil {
return ProLockCmdArgs{}, err
}
if ttl == 0 {
ttl = 30
}
message, err := flags.GetString("message")
if err != nil {
return ProLockCmdArgs{}, err
}
if message == "" {
message = "Locked by Atmos"
}
result := ProLockCmdArgs{
ProLockUnlockCmdArgs: commonArgs,
LockMessage: message,
LockTTL: ttl,
}
return result, nil
}
func parseUnlockCliArgs(cmd *cobra.Command, args []string) (ProUnlockCmdArgs, error) {
commonArgs, err := parseLockUnlockCliArgs(cmd, args)
if err != nil {
return ProUnlockCmdArgs{}, err
}
result := ProUnlockCmdArgs{
ProLockUnlockCmdArgs: commonArgs,
}
return result, nil
}
// ExecuteProLockCommand executes `atmos pro lock` command.
func ExecuteProLockCommand(cmd *cobra.Command, args []string) error {
defer perf.Track(nil, "exec.ExecuteProLockCommand")()
a, err := parseLockCliArgs(cmd, args)
if err != nil {
return err
}
gitRepo := git.NewDefaultGitRepo()
apiClient, err := pro.NewAtmosProAPIClientFromEnv(&a.AtmosConfig)
if err != nil {
return errors.Join(errUtils.ErrFailedToCreateAPIClient, err)
}
return executeProLock(&a, apiClient, gitRepo)
}
// executeProLock is the core lock logic extracted for testability.
func executeProLock(a *ProLockCmdArgs, apiClient pro.AtmosProAPIClientInterface, gitRepo git.GitRepoInterface) error {
repoInfo, err := gitRepo.GetLocalRepoInfo()
if err != nil {
return errors.Join(errUtils.ErrFailedToGetLocalRepo, err)
}
owner := repoInfo.RepoOwner
repoName := repoInfo.RepoName
dto := dtos.LockStackRequest{
Key: fmt.Sprintf("%s/%s/%s/%s", owner, repoName, a.Stack, a.Component),
TTL: a.LockTTL,
LockMessage: a.LockMessage,
Properties: nil,
}
lock, err := apiClient.LockStack(&dto)
if err != nil {
return errors.Join(errUtils.ErrFailedToLockStack, err)
}
u.PrintfMessageToTUI("\n%s Stack '%s' successfully locked\n\n", theme.Styles.Checkmark, lock.Data.Key)
log.Debug("Stack lock acquired", "key", lock.Data.Key, "lockID", lock.Data.ID, "expires", lock.Data.ExpiresAt)
return nil
}
// ExecuteProUnlockCommand executes `atmos pro unlock` command.
func ExecuteProUnlockCommand(cmd *cobra.Command, args []string) error {
defer perf.Track(nil, "exec.ExecuteProUnlockCommand")()
a, err := parseUnlockCliArgs(cmd, args)
if err != nil {
return err
}
gitRepo := git.NewDefaultGitRepo()
apiClient, err := pro.NewAtmosProAPIClientFromEnv(&a.AtmosConfig)
if err != nil {
return errors.Join(errUtils.ErrFailedToCreateAPIClient, err)
}
return executeProUnlock(&a, apiClient, gitRepo)
}
// executeProUnlock is the core unlock logic extracted for testability.
func executeProUnlock(a *ProUnlockCmdArgs, apiClient pro.AtmosProAPIClientInterface, gitRepo git.GitRepoInterface) error {
repoInfo, err := gitRepo.GetLocalRepoInfo()
if err != nil {
return errors.Join(errUtils.ErrFailedToGetLocalRepo, err)
}
owner := repoInfo.RepoOwner
repoName := repoInfo.RepoName
dto := dtos.UnlockStackRequest{
Key: fmt.Sprintf("%s/%s/%s/%s", owner, repoName, a.Stack, a.Component),
}
_, err = apiClient.UnlockStack(&dto)
if err != nil {
return errors.Join(errUtils.ErrFailedToUnlockStack, err)
}
u.PrintfMessageToTUI("\n%s Stack '%s' successfully unlocked\n\n", theme.Styles.Checkmark, dto.Key)
log.Debug("Stack lock released", "key", dto.Key)
return nil
}
// uploadStatus uploads the terraform results to the pro API.
func uploadStatus(info *schema.ConfigAndStacksInfo, exitCode int, client pro.AtmosProAPIClientInterface, gitRepo git.GitRepoInterface) error {
// Get the git repository info
repoInfo, err := gitRepo.GetLocalRepoInfo()
if err != nil {
return errors.Join(errUtils.ErrFailedToGetLocalRepo, err)
}
// Get current git SHA
gitSHA, err := gitRepo.GetCurrentCommitSHA()
if err != nil {
// Log warning but don't fail the upload
log.Warn(fmt.Sprintf("Failed to get current git SHA: %v", err))
gitSHA = ""
}
// Get run ID from environment variables.
// Note: This is an exception to the general rule of using viper.BindEnv for environment variables.
// The run ID is always provided by the CI/CD environment and is not part of the stack configuration.
//nolint:forbidigo // Exception: Run ID is always from CI/CD environment, not config
atmosProRunID := os.Getenv("ATMOS_PRO_RUN_ID")
// Create the DTO
dto := dtos.InstanceStatusUploadRequest{
AtmosProRunID: atmosProRunID,
AtmosVersion: pkgversion.Version,
AtmosOS: runtime.GOOS,
AtmosArch: runtime.GOARCH,
GitSHA: gitSHA,
RepoURL: repoInfo.RepoUrl,
RepoName: repoInfo.RepoName,
RepoOwner: repoInfo.RepoOwner,
RepoHost: repoInfo.RepoHost,
Stack: info.Stack,
Component: info.Component,
Command: info.SubCommand,
ExitCode: exitCode,
}
// Upload the status
if err := client.UploadInstanceStatus(&dto); err != nil {
return errors.Join(errUtils.ErrFailedToUploadInstanceStatus, err)
}
return nil
}
// shouldUploadStatus determines if status should be uploaded.
func shouldUploadStatus(info *schema.ConfigAndStacksInfo) bool {
// Only upload for plan and apply commands.
if info.SubCommand != "plan" && info.SubCommand != "apply" {
return false
}
// Check if pro is enabled in component settings
if proSettings, ok := info.ComponentSettingsSection["pro"].(map[string]interface{}); ok {
if enabled, ok := proSettings["enabled"].(bool); ok && enabled {
return true
}
}
// Log warning if pro is not enabled
log.Warn("Pro is not enabled. Skipping upload of Terraform result.")
return false
}