-
Notifications
You must be signed in to change notification settings - Fork 3
/
JsmAndBitbucketH2Deployment.groovy
248 lines (174 loc) · 8.4 KB
/
JsmAndBitbucketH2Deployment.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
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
package com.eficode.devstack.deployment.impl
import com.eficode.atlassian.bitbucketInstanceManager.BitbucketInstanceManagerRest
import com.eficode.atlassian.jiraInstanceManager.JiraInstanceManagerRest
import com.eficode.devstack.container.Container
import com.eficode.devstack.container.impl.BitbucketContainer
import com.eficode.devstack.container.impl.JsmContainer
import com.eficode.devstack.deployment.Deployment
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future
class JsmAndBitbucketH2Deployment implements Deployment {
String friendlyName = "JIRA and Bitbucket H2 Deployment"
String containerNetworkName = "jsm_and_bitbucket"
ArrayList<Deployment> subDeployments = []
Logger log = LoggerFactory.getLogger(this.class)
Map<String, String> jiraAppsToInstall = [:]
String jiraLicense
String jiraBaseUrl
JiraInstanceManagerRest jiraRest
String bitbucketBaseUrl
BitbucketInstanceManagerRest bitbucketRest
String bitbucketLicense
JsmAndBitbucketH2Deployment(String jiraBaseUrl, String bitbucketBaseUrl, String dockerHost = "", String dockerCertPath = "") {
this.jiraBaseUrl = jiraBaseUrl
this.jiraRest = new JiraInstanceManagerRest(jiraBaseUrl)
this.bitbucketBaseUrl = bitbucketBaseUrl
this.bitbucketRest = new BitbucketInstanceManagerRest(bitbucketBaseUrl)
this.subDeployments = [new JsmH2Deployment(jiraBaseUrl, dockerHost, dockerCertPath), new BitbucketH2Deployment(bitbucketBaseUrl, dockerHost, dockerCertPath)]
}
ArrayList<Container> getContainers() {
return [jsmContainer, bitbucketContainer]
}
void setContainers(ArrayList<Container> containers) {
this.containers = containers
}
JsmH2Deployment getJsmH2Deployment() {
return subDeployments.find { it instanceof JsmH2Deployment } as JsmH2Deployment
}
JsmContainer getJsmContainer() {
return jsmH2Deployment.jsmContainer
}
BitbucketH2Deployment getBitbucketH2Deployment() {
return subDeployments.find { it instanceof BitbucketH2Deployment } as BitbucketH2Deployment
}
BitbucketContainer getBitbucketContainer() {
return bitbucketH2Deployment.bitbucketContainer
}
void setJiraLicense(String licenseText) {
this.jiraLicense = licenseText
}
void setBitbucketLicense(String licenseText) {
this.bitbucketLicense = licenseText
}
/**
* Install apps in to JIRA
* @param appsAndLicenses key = App url (from marketplace), value = license string (optional)
* @return true if no apps where installed, or apps where installed successfully
*/
/*
boolean installJiraApps(Map<String,String> appsAndLicenses = jiraAppsToInstall) {
if (appsAndLicenses) {
log.info("Installing ${appsAndLicenses.size()} app(s)")
appsAndLicenses.each {url, license ->
assert jiraRest.installApp(url, license) : "Error installing app:" + url
}
}
return true
}
*/
private class SetupDeploymentTask implements Callable<Boolean> {
Deployment deployment
SetupDeploymentTask(Deployment deployment) {
this.deployment = deployment
}
@Override
Boolean call() throws Exception {
this.deployment.setupDeployment()
}
}
boolean setupDeployment() {
log.info("Setting up deployment:" + friendlyName)
assert jiraLicense: "Error no Jira License has been setup"
assert bitbucketLicense: "Error no Bitbucket License has been setup"
jsmH2Deployment.setJiraLicense(jiraLicense)
jsmH2Deployment.jsmContainer.enableAppUpload()
bitbucketH2Deployment.setBitbucketLicence(bitbucketLicense)
jsmH2Deployment.deploymentNetworkName = this.containerNetworkName
bitbucketH2Deployment.deploymentNetworkName = this.containerNetworkName
jsmContainer.createBridgeNetwork(this.containerNetworkName)
ExecutorService threadPool = Executors.newFixedThreadPool(2)
Future jsmFuture = threadPool.submit(new SetupDeploymentTask(jsmH2Deployment))
Future bitbucketFuture = threadPool.submit(new SetupDeploymentTask(bitbucketH2Deployment))
threadPool.shutdown()
while (!jsmFuture.done || !bitbucketFuture.done) {
log.info("Waiting for deployments to finish")
log.info("\tJSM Finished:" + jsmFuture.done)
log.info("\tBitbucket Finished:" + bitbucketFuture.done)
if (bitbucketFuture.done) {
log.info("\tBitbucket deployment finished successfully:" + bitbucketFuture.get())
}
if (jsmFuture.done) {
log.info("\tJSM deployment finished successfully:" + jsmFuture.get())
}
sleep(5000)
}
if (bitbucketFuture.done) {
log.info("\tBitbucket deployment finished successfully:" + bitbucketFuture.get())
}
if (jsmFuture.done) {
log.info("\tJSM deployment finished successfully:" + jsmFuture.get())
}
if (jiraAppsToInstall) {
log.info("\tInstalling user defined JIRA Apps")
assert installJiraApps(): "Error installing user defined JIRA apps"
log.info("\t\tFinished installing user defined JIRA Apps")
}
if (!jiraAppsToInstall.any { it.key.contains("jiraShortcuts") }) {
log.info("\tInstalling JiraShortcuts app") //Needed for setting up applink to bitbucket
assert installJiraApps(
[
"https://github.com/eficode/JiraShortcuts/raw/packages/repository/com/eficode/atlassian/jira/jiraShortcuts/2.0.1-SNAPSHOT-groovy-3.0/jiraShortcuts-2.0.1-SNAPSHOT-groovy-3.0.jar":""
]
) : "Error installing JiraShortcuts JIRA apps"
log.info("\t\tFinished installing JiraShortcuts JIRA Apps")
}
if (jiraRest.scriptRunnerIsInstalled()) {
log.info("\tSetting up application link between JIRA and Bitbucket")
String appLinkScript = getClass().getResourceAsStream("/com/eficode/devstack/deployment/jira/scripts/CreateBitbucketLink.groovy").text
appLinkScript = appLinkScript.replaceAll("BITBUCKET_URL", bitbucketBaseUrl)
appLinkScript = appLinkScript.replaceAll("BITBUCKET_USER", "admin")
appLinkScript = appLinkScript.replaceAll("BITBUCKET_PASSWORD", "admin")
log.trace("\t\tUsing Script:")
appLinkScript.eachLine {line ->
log.trace("\t"*3 + line)
}
Map appLinkResult = jiraRest.executeLocalScriptFile(appLinkScript)
log.debug("\t\tFinished executing application link script")
log.trace("\t"* 3 + "Script returned logs:")
appLinkResult.log.each {log.trace("\t"*4 + it)}
ArrayList<String>jiraLog = jsmContainer.runBashCommandInContainer("tail -n 500 log/atlassian-jira.log")
assert jiraLog.any {it.contains("Created link:Bitbucket")} : "Error creating application link from JIRA to bitbucket"
assert appLinkResult.success : "Error creating application link from JIRA to bitbucket"
log.info("\tFinished setting up application link between JIRA and Bitbucket successfully")
}else {
log.warn("\tScriptRunner was not installed, NOT setting up application link between JIRA and Bitbucket")
}
return jsmFuture.get() && bitbucketFuture.get()
}
/*
@Override
void setupSecureDockerConnection(String host, String certPath) {
subDeployments.each { deployment ->
deployment.setupSecureDockerConnection(host, certPath)
}
}
*/
/**
* Install apps in to JIRA
* @param appsAndLicenses key = App url (from marketplace), value = license string (optional)
* @return true if no apps where installed, or apps where installed successfully
*/
boolean installJiraApps(Map<String, String> appsAndLicenses = jiraAppsToInstall) {
if (appsAndLicenses) {
log.info("Installing ${appsAndLicenses.size()} jiraapp(s)")
appsAndLicenses.each { url, license ->
assert jiraRest.installApp(url, license): "Error installing app:" + url
}
}
return true
}
}