1
1
package com.eficode.devstack.container.impl
2
2
3
- import groovy.yaml.YamlBuilder
4
- import groovy.yaml.YamlSlurper
3
+
4
+ import com.fasterxml.jackson.databind.ObjectMapper
5
+ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
5
6
import org.apache.groovy.json.internal.LazyMap
6
7
7
8
import java.nio.file.Files
@@ -14,6 +15,8 @@ class HarborManagerContainer extends DoodContainer {
14
15
String harborBaseUrl
15
16
String basePath
16
17
18
+ ObjectMapper objectMapper = new ObjectMapper (new YAMLFactory ())
19
+
17
20
/**
18
21
* HarborManagerContainer setups a container that runs the Harbor installation scripts, which in turn creates all the "real"
19
22
* harbor containers
@@ -30,14 +33,14 @@ class HarborManagerContainer extends DoodContainer {
30
33
this . harborBaseUrl = baseUrl
31
34
this . harborVersion = harborVersion
32
35
this . containerName = host + " -manager"
33
- this . basePath = baseDir[-1 ] == " /" ? baseDir + host : baseDir + " /" + host
36
+ this . basePath = baseDir[-1 ] == " /" ? baseDir + host : baseDir + " /" + host
34
37
35
38
if (dockerHost && dockerCertPath) {
36
39
assert setupSecureRemoteConnection(dockerHost, dockerCertPath): " Error setting up secure remote docker connection"
37
40
}
38
41
39
42
prepareBindMount(" /var/run/docker.sock" , " /var/run/docker.sock" ) // Mount docker socket
40
- prepareBindMount(baseDir , baseDir, false ) // Mount data dir, data in this dir needs to be accessible by both engine and manager-container using the same path
43
+ prepareBindMount(baseDir, baseDir, false ) // Mount data dir, data in this dir needs to be accessible by both engine and manager-container using the same path
41
44
42
45
}
43
46
@@ -55,22 +58,19 @@ class HarborManagerContainer extends DoodContainer {
55
58
extractDomainFromUrl(harborBaseUrl)
56
59
}
57
60
58
- String getPort (){
59
- extractPortFromUrl( harborBaseUrl)
61
+ String getPort () {
62
+ extractPortFromUrl(harborBaseUrl)
60
63
}
61
64
62
65
63
-
64
-
65
66
@Override
66
67
boolean runAfterDockerSetup () {
67
68
68
69
log. info(" Setting up Harbor" )
69
70
70
71
// Make sure basePath is empty or does not exist
71
72
ArrayList<String > cmdOutput = runBashCommandInContainer(""" ls "$basePath " | wc -l""" , 5 )
72
- assert cmdOutput == [" 0" ] || cmdOutput. any{it. startsWith(" ls: cannot access" )} : " Harbor base path is not empty: $basePath "
73
-
73
+ assert cmdOutput == [" 0" ] || cmdOutput. any { it. startsWith(" ls: cannot access" ) }: " Harbor base path is not empty: $basePath "
74
74
75
75
76
76
cmdOutput = runBashCommandInContainer(" apt install -y wget; echo status: \$ ?" , 100 )
@@ -91,16 +91,27 @@ class HarborManagerContainer extends DoodContainer {
91
91
log. info(" \t Finished downloading and extracting Harbor" )
92
92
93
93
94
- assert modifyInstallYml() : " Error updating Harbor install config file: harbor.yml"
94
+ assert modifyInstallYml(): " Error updating Harbor install config file: harbor.yml"
95
95
96
96
log. info(" \t Starting installation" )
97
- cmdOutput = runBashCommandInContainer(installPath + " /harbor/install.sh ; echo status: \$ ?" , 400 )
98
- assert cmdOutput. last(). contains(" status: 0" ): " Error installing harbor:" + cmdOutput. join(" \n " )
97
+ cmdOutput = runBashCommandInContainer(installPath + " /harbor/install.sh ; echo status: \$ ?" , 400 )
98
+ if (! cmdOutput. last(). contains(" status: 0" )) {
99
+ log. warn(" \t There where problems during setup of harbor, potentially because dockerCompose has yet to mo modified, will modify and try again" )
100
+ }
99
101
102
+ cmdOutput = runBashCommandInContainer(" cd " + installPath + " /harbor && docker-compose stop && echo status: \$ ?" , 80 )
103
+ assert cmdOutput. last(). contains(" status: 0" ): " Error stopping harbor before modifying docker-compose file:" + cmdOutput. join(" \n " )
100
104
101
- assert modifyDockerCompose() : " Error modifying Harbors docker-compose file"
105
+ assert modifyDockerCompose(): " Error modifying Harbors docker-compose file"
102
106
107
+
108
+ sleep(5000 )
103
109
cmdOutput = runBashCommandInContainer(" cd " + installPath + " /harbor && docker-compose up -d && echo status: \$ ?" , 80 )
110
+ if (cmdOutput. last() != " status: 0" || cmdOutput. toString(). contains(" error" )) {
111
+ log. warn(" \t There was an error starting harbor after docker compose modification, this is common and a second attempt will be made" )
112
+ sleep(5000 )
113
+ cmdOutput = runBashCommandInContainer(" cd " + installPath + " /harbor && docker-compose up -d && echo status: \$ ?" , 120 )
114
+ }
104
115
assert cmdOutput. last(). contains(" status: 0" ): " Error applying the modified docker-compose file:" + cmdOutput. join(" \n " )
105
116
106
117
return true
@@ -114,61 +125,60 @@ class HarborManagerContainer extends DoodContainer {
114
125
*/
115
126
boolean modifyDockerCompose () {
116
127
117
-
118
-
119
-
120
128
log. info(" \t Customizing Harbor docker compose" )
121
- Path tmpDir= Files . createTempDirectory(" harbor-compose" )
129
+ Path tmpDir = Files . createTempDirectory(" harbor-compose" )
122
130
String tmpDirPath = tmpDir. toFile(). absolutePath
123
131
124
132
125
-
126
133
ArrayList<File > files = copyFilesFromContainer(" ${ installPath} /harbor/docker-compose.yml" , tmpDirPath + " /" )
127
134
128
- assert files. size() == 1 && files. first(). name == " docker-compose.yml" : " Error, could not find docker-compose.yml file"
135
+ assert files. size() == 1 && files. first(). name == " docker-compose.yml" : " Error, could not find docker-compose.yml file"
129
136
File yamlFile = files. first()
130
137
log. debug(" \t\t Retried docker compose file from container:" + yamlFile. absolutePath)
131
138
132
139
133
- LazyMap originalYml = new YamlSlurper (). parse(yamlFile) as LazyMap
134
- LazyMap modifiedYml = new YamlSlurper (). parse(yamlFile) as LazyMap
140
+ // LazyMap originalYml = new YamlSlurper().parse(yamlFile) as LazyMap
141
+ // LazyMap modifiedYml = new YamlSlurper().parse(yamlFile) as LazyMap
142
+ LazyMap originalYml = objectMapper. readValue(yamlFile, LazyMap . class)
143
+ LazyMap modifiedYml = objectMapper. readValue(yamlFile, LazyMap . class)
135
144
136
145
137
-
138
- modifiedYml. services. each {Entry<String , LazyMap > service ->
139
- log. debug(" \t " * 3 + " Customising Docker Service:" + service. key)
146
+ modifiedYml. services. each { Entry<String , LazyMap > service ->
147
+ log. debug(" \t " * 3 + " Customising Docker Service:" + service. key)
140
148
141
149
service. value. networks = containerDefaultNetworks
142
- log. trace(" \t " * 4 + " Set networks to:" + service. value. networks)
143
- log. trace(" \t " * 4 + " Used to be:" + originalYml. services. get(service. key). networks)
150
+ log. trace(" \t " * 4 + " Set networks to:" + service. value. networks)
151
+ log. trace(" \t " * 4 + " Used to be:" + originalYml. services. get(service. key). networks)
144
152
145
153
}
146
154
147
- log. debug(" \t " * 3 + " Customising Docker Network" )
155
+ log. debug(" \t " * 3 + " Customising Docker Network" )
148
156
modifiedYml. remove(" networks" )
149
157
150
158
Map<String , LazyMap > networks = [:]
151
- containerDefaultNetworks. each {networkName ->
152
- networks. put(networkName as String ,[" external" : true , " name" :networkName] as LazyMap )
159
+ containerDefaultNetworks. each { networkName ->
160
+ networks. put(networkName as String , [" external" : true , " name" : networkName] as LazyMap )
153
161
}
154
162
modifiedYml. put(" networks" , networks)
155
163
// modifiedYml.put("networks", ["default": [external: [name: networkName]]])
156
164
157
- log. trace(" \t " * 4 + " Set networks to:" + modifiedYml. networks)
158
- log. trace(" \t " * 4 + " Used to be:" + originalYml. networks)
159
-
165
+ log. trace(" \t " * 4 + " Set networks to:" + modifiedYml. networks)
166
+ log. trace(" \t " * 4 + " Used to be:" + originalYml. networks)
160
167
161
168
162
- YamlBuilder yamlBuilder = new YamlBuilder ()
163
- yamlBuilder (modifiedYml)
169
+ // Change the user of log container to root https://github.com/goharbor/harbor/issues/16669
170
+ (modifiedYml. services . find { Entry< String , LazyMap > service -> service . key == " log " } as Entry< String , Map > ) . value . put( " user " , " root " )
164
171
172
+ // YamlBuilder yamlBuilder = new YamlBuilder()
173
+ // yamlBuilder(modifiedYml)
165
174
166
- yamlFile. write(yamlBuilder. toString())
175
+ // yamlFile.write(yamlBuilder.toString())
176
+ yamlFile. write( objectMapper. writeValueAsString(modifiedYml))
167
177
168
- assert copyFileToContainer(yamlFile. absolutePath, installPath + " /harbor/" ) : " Error copying updated YAML file to container"
178
+ assert copyFileToContainer(yamlFile. absolutePath, installPath + " /harbor/" ): " Error copying updated YAML file to container"
169
179
tmpDir. deleteDir()
170
180
171
- log. info(" \t Finished customizing installation configuration " )
181
+ log. info(" \t Finished customizing docker-compose file " )
172
182
173
183
return true
174
184
@@ -180,17 +190,19 @@ class HarborManagerContainer extends DoodContainer {
180
190
boolean modifyInstallYml () {
181
191
182
192
183
- Path tmpDir= Files . createTempDirectory(" harbor-conf" )
193
+ Path tmpDir = Files . createTempDirectory(" harbor-conf" )
184
194
String tmpDirPath = tmpDir. toFile(). absolutePath
185
195
186
196
187
197
ArrayList<File > files = copyFilesFromContainer(" ${ installPath} /harbor/harbor.yml.tmpl" , tmpDirPath + " /" )
188
198
189
- assert files. size() == 1 && files. first(). name == " harbor.yml.tmpl" : " Error, could not find template config file"
199
+ assert files. size() == 1 && files. first(). name == " harbor.yml.tmpl" : " Error, could not find template config file"
190
200
File yamlFile = files. first()
191
201
192
202
193
- LazyMap originalYml = new YamlSlurper (). parse(yamlFile) as LazyMap
203
+ // LazyMap originalYml = new YamlSlurper().parse(yamlFile) as LazyMap
204
+ LazyMap originalYml = objectMapper. readValue(yamlFile, LazyMap . class)
205
+
194
206
195
207
196
208
LazyMap modifiedYml = originalYml
@@ -201,14 +213,15 @@ class HarborManagerContainer extends DoodContainer {
201
213
202
214
203
215
204
- YamlBuilder yamlBuilder = new YamlBuilder ()
205
- yamlBuilder(modifiedYml)
216
+ // YamlBuilder yamlBuilder = new YamlBuilder()
217
+ // yamlBuilder(modifiedYml)
206
218
207
219
File modifiedYamlFile = new File (tmpDirPath + " /harbor.yml" )
208
220
modifiedYamlFile. createNewFile()
209
- modifiedYamlFile. write(yamlBuilder. toString(). replaceAll(" \" " , " " ))
221
+ // modifiedYamlFile.write(yamlBuilder.toString().replaceAll("\"", ""))
222
+ modifiedYamlFile. write(objectMapper. writeValueAsString(originalYml). replaceAll(" \" " , " " ))
210
223
211
- assert copyFileToContainer(modifiedYamlFile. absolutePath, installPath + " /harbor/" ) : " Error copying updated YAML file to container"
224
+ assert copyFileToContainer(modifiedYamlFile. absolutePath, installPath + " /harbor/" ): " Error copying updated YAML file to container"
212
225
tmpDir. deleteDir()
213
226
214
227
log. info(" \t Finished customizing installation configuration" )
0 commit comments