Skip to content

Commit cce13a3

Browse files
committed
Several fixes needed to be able to use jar as library
Udpated POM to be able to build, package
1 parent 8aead7e commit cce13a3

7 files changed

Lines changed: 119 additions & 48 deletions

File tree

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.eficode</groupId>
88
<artifactId>devstack</artifactId>
9-
<version>1.0.1-SNAPSHOT</version>
9+
<version>1.0.6-SNAPSHOT</version>
1010
<description>A series of scripts for setting up common developer application suites</description>
1111

1212
<distributionManagement>
@@ -74,7 +74,7 @@
7474
</repository>
7575
</repositories>
7676
<build>
77-
<sourceDirectory>${basedir}/src/main/groovy</sourceDirectory>
77+
<!--sourceDirectory>${basedir}/src/main/groovy/</sourceDirectory-->
7878
<plugins>
7979
<plugin>
8080
<groupId>org.apache.maven.plugins</groupId>
@@ -86,6 +86,7 @@
8686
<phase>generate-sources</phase>
8787
<goals>
8888
<goal>jar-no-fork</goal>
89+
<goal>test-jar-no-fork</goal>
8990
</goals>
9091
</execution>
9192
</executions>
@@ -97,18 +98,31 @@
9798
<executions>
9899
<execution>
99100
<goals>
100-
<goal>execute</goal>
101+
<goal>addSources</goal>
102+
<goal>addTestSources</goal>
103+
<goal>compile</goal>
104+
<goal>compileTests</goal>
101105
</goals>
102106
</execution>
103107
</executions>
104108
<dependencies>
105109
<dependency>
106-
<groupId>org.apache.groovy</groupId>
110+
<groupId>org.codehaus.groovy</groupId>
107111
<artifactId>groovy</artifactId>
108-
<version>4.0.2</version>
112+
<version>3.0.11</version>
109113
<scope>runtime</scope>
110114
</dependency>
111115
</dependencies>
116+
<!--configuration>
117+
<sources>
118+
<source>
119+
<directory>${project.basedir}/src/main/groovy</directory>
120+
<includes>
121+
<include>**/*.groovy</include>
122+
</includes>
123+
</source>
124+
</sources>
125+
</configuration-->
112126

113127
</plugin>
114128
</plugins>

src/main/groovy/com/eficode/devstack/container/Container.groovy

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,29 @@ trait Container {
6868

6969
ArrayList<Map> content = dockerClient.ps().content
7070
ArrayList<String> containerNames = content.collect { it.Names }.flatten()
71-
return containerNames.find { it == "/" + getContainerName() } != null
71+
return containerNames.find { it == "/" + self.containerName } != null
7272

7373
}
7474

7575
String getId() {
7676
return containerId
7777
}
7878

79+
def getSelf() {
80+
return this
81+
}
82+
7983
String getContainerId() {
8084

8185
if (containerId) {
8286
return containerId
8387
}
84-
log.info("\tResolving container ID for: $containerName")
88+
log.info("\tResolving container ID for:" + self.containerName)
89+
8590

8691
ArrayList<Map> content = dockerClient.ps().content
8792

88-
Map container = content.find { it.Names.first() == "/" + containerName }
93+
Map container = content.find { it.Names.first() == "/" + self.containerName }
8994
this.containerId = container?.Id
9095
log.info("\tGot:" + this.containerId)
9196

@@ -94,36 +99,44 @@ trait Container {
9499

95100
boolean startContainer() {
96101

97-
dockerClient.startContainer(containerId)
102+
dockerClient.startContainer(self.containerId)
103+
104+
return isRunning()
105+
}
98106

99-
return dockerClient.inspectContainer(containerId).content.state.running
107+
boolean isRunning() {
108+
return dockerClient.inspectContainer(self.containerId).content.state.running
100109
}
101110

102111
boolean stopAndRemoveContainer() {
103112

104-
dockerClient.stop(containerId, 240000)
105-
dockerClient.wait(containerId)
106-
dockerClient.rm(containerId)
113+
if (self.containerId) {
114+
dockerClient.stop(self.containerId, 240000)
115+
dockerClient.wait(self.containerId)
116+
dockerClient.rm(self.containerId)
107117

108118

109-
try {
110-
dockerClient.inspectContainer(containerId)
111-
} catch (ClientException ex) {
119+
try {
120+
dockerClient.inspectContainer(self.containerId)
121+
} catch (ClientException ex) {
112122

113-
if (ex.response.message == "Not Found") {
114-
return true
123+
if (ex.response.message == "Not Found") {
124+
return true
125+
}
115126
}
127+
return false
128+
}else {
129+
return false
116130
}
117-
return false
118131

119132

120133
}
121134

122135
boolean stopContainer() {
123-
log.info("Stopping container:" + containerId)
124-
dockerClient.stop(containerId, 240000)
125-
if (dockerClient.inspectContainer(containerId).content.state.running) {
126-
log.warn("\tFailed to stop container" + containerId)
136+
log.info("Stopping container:" + self.containerId)
137+
dockerClient.stop(self.containerId, 240000)
138+
if (running) {
139+
log.warn("\tFailed to stop container" + self.containerId)
127140
return false
128141
} else {
129142
log.info("\tContainer stopped")
@@ -137,7 +150,7 @@ trait Container {
137150

138151
log.info("Creating tar file:" + outputPath)
139152
log.debug("\tUsing source paths:")
140-
filePaths.each {log.debug("\t\t$it")}
153+
filePaths.each { log.debug("\t\t$it") }
141154

142155

143156
File outputFile = new File(outputPath)
@@ -150,36 +163,33 @@ trait Container {
150163
File newEntryFile = new File(filePath)
151164

152165
assert (newEntryFile.isDirectory() || newEntryFile.isFile()) && newEntryFile.canRead(), "Error creating TAR cant read file:" + filePath
153-
log.trace("\t"*3 + "Can read file/dir")
166+
log.trace("\t" * 3 + "Can read file/dir")
154167

155168
if (newEntryFile.isDirectory()) {
156-
log.trace("\t"*3 + "File is actually directory, processing sub files")
169+
log.trace("\t" * 3 + "File is actually directory, processing sub files")
157170
newEntryFile.eachFileRecurse(FileType.FILES) { subFile ->
158171

159-
String path = ResourceGroovyMethods.relativePath(newEntryFile, subFile)
160-
log.trace("\t"*4 + "Processing sub file:" + path)
172+
String path = ResourceGroovyMethods.relativePath(newEntryFile, subFile)
173+
log.trace("\t" * 4 + "Processing sub file:" + path)
161174
TarArchiveEntry entry = new TarArchiveEntry(subFile, path)
162175
entry.setSize(subFile.size())
163176
tarArchive.putArchiveEntry(entry)
164177
tarArchive.write(subFile.bytes)
165178
tarArchive.closeArchiveEntry()
166-
log.trace("\t"*5 + "Added to archive")
179+
log.trace("\t" * 5 + "Added to archive")
167180
}
168181
} else {
169-
log.trace("\t"*4 + "Processing file:" + newEntryFile.name)
182+
log.trace("\t" * 4 + "Processing file:" + newEntryFile.name)
170183
TarArchiveEntry entry = new TarArchiveEntry(newEntryFile, newEntryFile.name)
171184
entry.setSize(newEntryFile.size())
172185
tarArchive.putArchiveEntry(entry)
173186
tarArchive.write(newEntryFile.bytes)
174187
tarArchive.closeArchiveEntry()
175-
log.trace("\t"*5 + "Added to archive")
188+
log.trace("\t" * 5 + "Added to archive")
176189

177190
}
178191

179192

180-
181-
182-
183193
}
184194

185195
tarArchive.finish()
@@ -231,7 +241,7 @@ trait Container {
231241
ArrayList<File> copyFilesFromContainer(String containerPath, String destinationPath) {
232242

233243
//containerPath can be both a directory or a file
234-
EngineResponse<InputStream> response = dockerClient.getArchive(containerId, containerPath)
244+
EngineResponse<InputStream> response = dockerClient.getArchive(self.containerId, containerPath)
235245

236246

237247
Path tempFile = Files.createTempFile("docker_download", ".tar")
@@ -249,7 +259,7 @@ trait Container {
249259

250260

251261
File tarFile = createTar([srcFilePath], Files.createTempFile("docker_upload", ".tar").toString())
252-
dockerClient.putArchive(containerId, destinationDirectory, tarFile.newDataInputStream())
262+
dockerClient.putArchive(self.containerId, destinationDirectory, tarFile.newDataInputStream())
253263

254264
return tarFile.delete()
255265
}
@@ -273,9 +283,19 @@ trait Container {
273283

274284
ArrayList<String> runBashCommandInContainer(String command, long timeoutS = 10) {
275285

286+
log.info("Executing bash command in container:")
287+
log.info("\tContainer:" + self.containerName + " (${self.containerId})")
288+
log.info("\tCommand:" + command)
289+
log.info("\tTimeout:" + timeoutS)
290+
if (log.isTraceEnabled()) {
291+
log.trace("\tDocker ping:" + dockerClient.ping().content as String)
292+
}
293+
294+
276295

277296
ContainerCallback callBack = new ContainerCallback()
278-
EngineResponse<IdResponse> response = dockerClient.exec(containerId, ["/bin/bash", "-c", command], callBack, Duration.ofSeconds(timeoutS))
297+
EngineResponse<IdResponse> response = dockerClient.exec(self.containerId, ["/bin/bash", "-c", command], callBack, Duration.ofSeconds(timeoutS))
298+
279299

280300

281301
return callBack.output

src/main/groovy/com/eficode/devstack/deployment/Deployment.groovy

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ trait Deployment {
1616
void setupSecureDockerConnection(String host, String certPath) {
1717

1818
log.info("Setting up secure connection to docker engine")
19-
assert getContainers() != null && !getContainers().empty : "Deployment has no containers defined"
19+
assert getContainers() != null && !getContainers().empty: "Deployment has no containers defined"
2020
//assert getContainers().any{! it.ping()} : "Connection has already been established."
2121
//assert getContainers().created.each {!it} : "Cant setup secure connection when containers have already been created in docker engine"
2222

2323

2424
getContainers().each {
25-
assert it.setupSecureRemoteConnection(host, certPath) : "Error setting up secure connection to docker engine"
25+
assert it.setupSecureRemoteConnection(host, certPath): "Error setting up secure connection to docker engine"
2626
log.info("\tSecure connection setup for container:" + getFriendlyName())
2727
}
2828
log.info("\tSuccessfully setup secure connections to docker engine")
2929
}
3030

3131

3232
boolean startDeployment() {
33-
log.info("Starting deployment:" + friendlyName)
33+
log.info("Starting deployment: " + this.friendlyName)
3434

35-
containers.each {container ->
35+
this.containers.each { container ->
3636
log.debug("\tStarting:" + container.containerName)
37-
assert container.startContainer() : "Error starting container:" + container.containerId
37+
assert container.startContainer(): "Error starting container:" + container.containerId
3838
}
3939

4040
log.info("\tFinished starting deployment")
@@ -43,15 +43,31 @@ trait Deployment {
4343
}
4444

4545
boolean stopDeployment() {
46-
log.info("Stopping deployment:" + friendlyName)
46+
log.info("Stopping deployment: " + this.getFriendlyName())
4747

48-
containers.each {container ->
49-
log.debug("\tStopping:" + container.containerName)
50-
assert container.stopContainer() : "Error stopping container:" + container.containerId
48+
49+
getContainers().each { container ->
50+
log.debug("\tStopping container:" + container.containerName)
51+
assert container.stopContainer(): "Error stopping container:" + container.containerId
5152
}
5253

5354
log.info("\tFinished stopping deployment")
5455
return true
5556

5657
}
58+
59+
boolean removeDeployment() {
60+
log.info("Removing deployment: " + this.getFriendlyName())
61+
62+
63+
getContainers().each { container ->
64+
65+
log.debug("\tRemoving container:" + container.containerName)
66+
container.stopAndRemoveContainer()
67+
68+
}
69+
70+
log.info("\tFinished removing deployment")
71+
return true
72+
}
5773
}

src/main/groovy/com/eficode/devstack/deployment/impl/JsmH2Deployment.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ class JsmH2Deployment implements Deployment{
99

1010
String friendlyName = "JIRA H2 Deployment"
1111
JiraInstanceMangerRest jiraRest
12-
ArrayList<Container> containers = [new JsmContainer()]
12+
ArrayList<Container> containers = []
1313
Map<String,String> appsToInstall = [:]
1414
String jiraLicense
1515

1616
String jiraBaseUrl
1717

1818
JsmH2Deployment(String jiraBaseUrl) {
1919
this.jiraBaseUrl = jiraBaseUrl
20-
jiraRest = new JiraInstanceMangerRest(jiraBaseUrl)
20+
this.jiraRest = new JiraInstanceMangerRest(jiraBaseUrl)
21+
this.containers = [new JsmContainer()]
2122
}
2223

2324
JsmContainer getJsmContainer() {

src/test/groovy/com/eficode/devstack/container/ContainerTest.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ContainerTest extends Specification {
1212

1313
static Logger log = LoggerFactory.getLogger(ContainerTest.class)
1414

15+
String dockerHost = "https://docker.domain.se:2376"
16+
String dockerCertPath = "./resources/dockerCert"
1517

1618

1719
class ContainerImpl implements Container {
@@ -23,6 +25,18 @@ class ContainerTest extends Specification {
2325
}
2426

2527

28+
29+
def testPing() {
30+
31+
setup:
32+
ContainerImpl container = new ContainerImpl()
33+
container.setupSecureRemoteConnection(dockerHost, dockerCertPath)
34+
35+
expect:
36+
container.ping()
37+
38+
}
39+
2640
def testCreateTar() {
2741

2842
setup:

src/test/groovy/com/eficode/devstack/container/impl/JsmContainerTest.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class JsmContainerTest extends Specification {
3333

3434
def setupSpec() {
3535
dockerClient = resolveDockerClient()
36+
dockerClient.stop("JSM")
3637
dockerClient.rm("JSM")
3738
}
3839

0 commit comments

Comments
 (0)