-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathS3BackendPlugin.groovy
149 lines (114 loc) · 4.41 KB
/
S3BackendPlugin.groovy
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
class S3BackendPlugin implements TerraformInitCommandPlugin, Resettable {
public static Closure keyPattern
public static void init() {
S3BackendPlugin plugin = new S3BackendPlugin()
TerraformInitCommand.addPlugin(plugin)
}
@Override
public void apply(TerraformInitCommand command) {
String environment = command.getEnvironment()
def configs = [:]
configs['key'] = getKey(environment)
configs['bucket'] = getBucket(environment)
configs['region'] = getRegion(environment)
configs['dynamodb_table'] = getDynamodbTable(environment)
configs['encrypt'] = getEncrypt(environment)
configs['kms_key_id'] = getKmsKeyId(environment)
configs.each { entry ->
if (entry.value?.trim()) {
command.withBackendConfig("${entry.key}=${entry.value}")
} else {
println("No S3 backend ${entry.key} found")
}
}
}
public String getKey(String environment) {
Closure backendKeyPattern = keyPattern
if (backendKeyPattern == null) {
String repoSlug = getStandardizedRepoSlug()
backendKeyPattern = { String env -> "terraform/${repoSlug}/${env}" }
}
return backendKeyPattern.call(environment)
}
public String getBucket(String environment) {
def env = getEnv()
String bucket = env["S3_BACKEND_BUCKET"]
if (bucket == null) {
println("No S3_BACKEND_BUCKET found - checking for environment-specific bucket")
bucket = env["${environment.toUpperCase()}_S3_BACKEND_BUCKET"]
}
if (bucket == null) {
bucket = env["${environment}_S3_BACKEND_BUCKET"]
}
if (bucket == null) {
println("No ${environment.toUpperCase()}_S3_BACKEND_BUCKET found either.")
}
return bucket
}
public String getRegion(String environment) {
def env = getEnv()
String region = env['S3_BACKEND_REGION']
if (region == null) {
println("No S3_BACKEND_REGION found - checking for environment-specific region")
region = env["${environment.toUpperCase()}_S3_BACKEND_REGION"]
}
if (region == null) {
region = env["${environment}_S3_BACKEND_REGION"]
}
if (region == null) {
region = env['DEFAULT_S3_BACKEND_REGION']
if (region != null) {
println("WARNING: DEFAULT_S3_BACKEND_REGION is deprecated, please use S3_BACKEND_REGION or ${environment.toUpperCase()}_S3_BACKEND_REGION")
}
}
return region
}
public String getDynamodbTable(String environment) {
def env = getEnv()
String table = env["S3_BACKEND_DYNAMODB_TABLE"]
if (table == null) {
table = env["${environment.toUpperCase()}_S3_BACKEND_DYNAMODB_TABLE"]
}
if (table == null) {
table = env["${environment}_S3_BACKEND_DYNAMODB_TABLE"]
}
if (table == null) {
table = env["${environment.toUpperCase()}_S3_BACKEND_DYNAMO_TABLE_LOCK"]
if (table != null) {
println("${environment.toUpperCase()}_S3_BACKEND_DYNAMO_TABLE_LOCK is deprecated - please use ${environment.toUpperCase()}_S3_BACKEND_DYNAMODB_TABLE instead")
}
}
return table
}
public String getEncrypt(String environment) {
def env = getEnv()
String encrypt = env["S3_BACKEND_ENCRYPT"]
if (encrypt == null) {
encrypt = env["${environment.toUpperCase()}_S3_BACKEND_ENCRYPT"]
}
if (encrypt == null) {
encrypt = env["${environment}_S3_BACKEND_ENCRYPT"]
}
return encrypt
}
public String getKmsKeyId(String environment) {
def env = getEnv()
String arn = env["S3_BACKEND_KMS_KEY_ID"]
if (arn == null) {
arn = env["${environment.toUpperCase()}_S3_BACKEND_KMS_KEY_ID"]
}
if (arn == null) {
arn = env["${environment}_S3_BACKEND_KMS_KEY_ID"]
}
return arn
}
public getEnv() {
return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getEnv() : [:]
}
public getStandardizedRepoSlug() {
return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getStandardizedRepoSlug() : null
}
public static reset() {
keyPattern = null
}
}