-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcontext.go
More file actions
198 lines (169 loc) · 5.47 KB
/
context.go
File metadata and controls
198 lines (169 loc) · 5.47 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
package context
import (
"context"
"fmt"
"maps"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/redhat-developer/mapt/pkg/integrations/cirrus"
"github.com/redhat-developer/mapt/pkg/integrations/github"
"github.com/redhat-developer/mapt/pkg/integrations/gitlab"
"github.com/redhat-developer/mapt/pkg/util"
"github.com/redhat-developer/mapt/pkg/util/logging"
utilMaps "github.com/redhat-developer/mapt/pkg/util/maps"
)
var (
// mapt image to make self use. OCI image value is passed during building time
// this is intended for full build process, when building mapt binary we need to ensure
// OCI image already exists to make use of it
OCI = "quay.io/redhat-developer/mapt:v0.0.0-unset"
)
const (
tagKeyOrigin = "origin"
origin = "mapt"
TagKeyProjectName = "projectName"
TagKeyRunID = "runid"
)
type ContextArgs struct {
Context context.Context
ProjectName string
BackedURL string
//Optional
ResultsOutput string
Debug bool
DebugLevel uint
Tags map[string]string
// serverless here is used to set the credentials based on
// roles inherid by tasks as serverless
// see SetAWSCredentials function
// take into account that the name may change as the approach to get
// credentials from role is more general approach
Serverless bool
// This forces destroy even when lock exists
ForceDestroy bool
// Keep Pulumi state files in backend after successful destroy (by default, state files are removed)
KeepState bool
// integrations
GHRunnerArgs *github.GithubRunnerArgs
CirrusPWArgs *cirrus.PersistentWorkerArgs
GLRunnerArgs *gitlab.GitLabRunnerArgs
}
type Context struct {
ctx context.Context
runID string
projectName string
backedURL string
resultsOutput string
debug bool
debugLevel uint
serverless bool
forceDestroy bool
keepState bool
// spotPriceIncreaseRate int
tags map[string]string
tagsAsPulumiStringMap pulumi.StringMap
// if non-spot we will use this when creating
// as we may need to change it during state file check / createe
targetHostingPlace string
}
type Provider interface {
Init(ctx context.Context, backedURL string) (string, error)
DefaultHostingPlace() (*string, error)
}
func InitNoState() *Context { return &Context{} }
func Init(ca *ContextArgs, provider Provider) (*Context, error) {
// Default to context.Background() if no context is provided
ctx := ca.Context
if ctx == nil {
logging.Warn("no context provided to ContextArgs; falling back to context.Background()")
ctx = context.Background()
}
c := &Context{
ctx: ctx,
runID: util.RandomID(origin),
projectName: ca.ProjectName,
backedURL: ca.BackedURL,
resultsOutput: ca.ResultsOutput,
debug: ca.Debug,
debugLevel: ca.DebugLevel,
tags: ca.Tags,
serverless: ca.Serverless,
forceDestroy: ca.ForceDestroy,
keepState: ca.KeepState,
}
addCommonTags(c)
hp, err := provider.DefaultHostingPlace()
if err != nil {
return nil, err
}
if hp != nil {
c.targetHostingPlace = *hp
}
// Manage
resolvedURL, err := provider.Init(ctx, ca.BackedURL)
if err != nil {
return nil, err
}
if resolvedURL != "" {
c.backedURL = resolvedURL
}
// Manage integrations
if err := manageIntegration(c, ca); err != nil {
return nil, err
}
logging.Debugf("context initialized for %s", c.runID)
return c, nil
}
func (c *Context) Context() context.Context { return c.ctx }
func (c *Context) RunID() string { return c.runID }
func (c *Context) ProjectName() string { return c.projectName }
func (c *Context) SetProjectName(projectName string) { c.projectName = projectName }
func (c *Context) BackedURL() string { return c.backedURL }
func (c *Context) GetResultsOutputPath() string { return c.resultsOutput }
func (c *Context) GetTags() map[string]string { return c.tags }
func (c *Context) ResourceTags() pulumi.StringMap { return c.ResourceTagsWithCustom(nil) }
func (c *Context) Debug() bool { return c.debug }
func (c *Context) DebugLevel() uint { return c.debugLevel }
func (c *Context) IsServerless() bool { return c.serverless }
func (c *Context) IsForceDestroy() bool { return c.forceDestroy }
func (c *Context) IsKeepState() bool { return c.keepState }
func (c *Context) TargetHostingPlace() string { return c.targetHostingPlace }
// Get tags ready to be added to any pulumi resource
// in addition we cas set specific custom tags
func (c *Context) ResourceTagsWithCustom(customTags map[string]string) pulumi.StringMap {
lTags := make(map[string]string)
maps.Copy(lTags, c.tags)
if customTags != nil {
maps.Copy(lTags, customTags)
}
if c.tagsAsPulumiStringMap == nil {
c.tagsAsPulumiStringMap = utilMaps.Convert(lTags,
func(name string) string { return name },
func(value string) pulumi.StringInput { return pulumi.String(value) })
}
return c.tagsAsPulumiStringMap
}
func (c *Context) StackNameByProject(stackName string) string {
return fmt.Sprintf("%s-%s", stackName, c.projectName)
}
func addCommonTags(c *Context) {
if c.tags == nil {
c.tags = make(map[string]string)
}
c.tags[tagKeyOrigin] = origin
c.tags[TagKeyProjectName] = c.projectName
}
func manageIntegration(c *Context, ca *ContextArgs) error {
if ca.GHRunnerArgs != nil {
ca.GHRunnerArgs.Name = c.RunID()
github.Init(ca.GHRunnerArgs)
}
if ca.CirrusPWArgs != nil {
ca.CirrusPWArgs.Name = c.RunID()
cirrus.Init(ca.CirrusPWArgs)
}
if ca.GLRunnerArgs != nil {
ca.GLRunnerArgs.Name = c.RunID()
gitlab.Init(ca.GLRunnerArgs)
}
return nil
}