-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcontext.go
More file actions
193 lines (165 loc) · 5.24 KB
/
Copy pathcontext.go
File metadata and controls
193 lines (165 loc) · 5.24 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
package context
import (
"fmt"
"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/util"
"github.com/redhat-developer/mapt/pkg/util/logging"
utilMaps "github.com/redhat-developer/mapt/pkg/util/maps"
"golang.org/x/exp/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 {
ProjectName string
BackedURL string
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
// If remote is set we will run the action through the serverless task spec
Remote bool
// integrations
GHRunnerArgs *github.GithubRunnerArgs
CirrusPWArgs *cirrus.PersistentWorkerArgs
// Spot Bid Safe Limit
SpotPriceIncreaseRate int
}
type context struct {
runID string
projectName string
backedURL string
resultsOutput string
debug bool
debugLevel uint
serverless bool
forceDestroy bool
spotPriceIncreaseRate int
remote bool
tags map[string]string
tagsAsPulumiStringMap pulumi.StringMap
// This will be set if we need specific customization on a specfici execution
provider Provider
}
// mapt context
var mc *context
type Provider interface {
Init(backedURL string) error
Custom(ctx *pulumi.Context) (*pulumi.ProviderResource, error)
}
func Init(ca *ContextArgs, provider Provider) error {
mc = &context{
runID: CreateRunID(),
projectName: ca.ProjectName,
backedURL: ca.BackedURL,
resultsOutput: ca.ResultsOutput,
debug: ca.Debug,
debugLevel: ca.DebugLevel,
tags: ca.Tags,
serverless: ca.Serverless,
forceDestroy: ca.ForceDestroy,
spotPriceIncreaseRate: ca.SpotPriceIncreaseRate,
remote: ca.Remote,
provider: provider,
}
addCommonTags()
// Init provider
if err := provider.Init(ca.BackedURL); err != nil {
return err
}
// Manage integrations
if err := manageIntegration(ca); err != nil {
return err
}
logging.Debugf("context initialized for %s", mc.runID)
return nil
}
func RunID() string { return mc.runID }
func ProjectName() string { return mc.projectName }
func SetProjectName(projectName string) { mc.projectName = projectName }
func BackedURL() string { return mc.backedURL }
func GetResultsOutputPath() string { return mc.resultsOutput }
func GetTags() map[string]string { return mc.tags }
func ResourceTags() pulumi.StringMap { return ResourceTagsWithCustom(nil) }
func Debug() bool { return mc.debug }
func DebugLevel() uint { return mc.debugLevel }
func IsServerless() bool { return mc.serverless }
func IsForceDestroy() bool { return mc.forceDestroy }
func SpotPriceIncreaseRate() int { return mc.spotPriceIncreaseRate }
func IsRemote() bool { return mc.remote }
func CommonOptions(ctx *pulumi.Context) (co []pulumi.ResourceOption) {
// Check if provider requires customization
cp, err := mc.provider.Custom(ctx)
if cp != nil {
co = append(co, pulumi.Provider(*cp))
}
if err != nil {
logging.Errorf("Error registering custom provider %v", err)
}
return
}
// It will create a runID
// if context has been intialized it will set it as the runID for the context
// otherwise it will return the value (one time value)
func CreateRunID() string {
runID := util.RandomID(origin)
if mc != nil {
mc.runID = runID
}
return runID
}
// Get tags ready to be added to any pulumi resource
// in addition we cas set specific custom tags
func ResourceTagsWithCustom(customTags map[string]string) pulumi.StringMap {
lTags := make(map[string]string)
maps.Copy(lTags, mc.tags)
if customTags != nil {
maps.Copy(lTags, customTags)
}
if mc.tagsAsPulumiStringMap == nil {
mc.tagsAsPulumiStringMap = utilMaps.Convert(lTags,
func(name string) string { return name },
func(value string) pulumi.StringInput { return pulumi.String(value) })
}
return mc.tagsAsPulumiStringMap
}
func StackNameByProject(stackName string) string {
return fmt.Sprintf("%s-%s", stackName, mc.projectName)
}
func addCommonTags() {
if mc.tags == nil {
mc.tags = make(map[string]string)
}
mc.tags[tagKeyOrigin] = origin
mc.tags[TagKeyProjectName] = mc.projectName
}
func manageIntegration(ca *ContextArgs) error {
if ca.GHRunnerArgs != nil {
ca.GHRunnerArgs.Name = RunID()
github.Init(ca.GHRunnerArgs)
}
if ca.CirrusPWArgs != nil {
ca.CirrusPWArgs.Name = RunID()
cirrus.Init(ca.CirrusPWArgs)
}
return nil
}