Skip to content

Commit b3873a5

Browse files
committed
refactored ScmManager Setup into own class
1 parent 5e6ddc7 commit b3873a5

File tree

2 files changed

+207
-190
lines changed

2 files changed

+207
-190
lines changed

src/main/groovy/com/cloudogu/gitops/git/providers/scmmanager/ScmManager.groovy

Lines changed: 12 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,35 @@ import com.cloudogu.gitops.git.providers.RepoUrlScope
1010
import com.cloudogu.gitops.git.providers.Scope
1111
import com.cloudogu.gitops.git.providers.scmmanager.api.Repository
1212
import com.cloudogu.gitops.git.providers.scmmanager.api.ScmManagerApiClient
13-
import com.cloudogu.gitops.git.providers.scmmanager.api.ScmManagerUser
1413
import com.cloudogu.gitops.utils.*
1514
import groovy.util.logging.Slf4j
16-
import okhttp3.RequestBody
1715
import retrofit2.Response
1816

1917
@Slf4j
2018
class ScmManager implements GitProvider {
2119

22-
static final String HELM_VALUES_PATH = "scm-manager/values.ftl.yaml"
20+
ScmManagerUrlResolver urls
21+
ScmManagerApiClient apiClient
22+
ScmManagerConfig scmmConfig
2323

24-
private final ScmManagerUrlResolver urls
25-
private final ScmManagerApiClient apiClient
26-
private final ScmManagerConfig scmmConfig
27-
28-
29-
private final NetworkingUtils networkingUtils
30-
private final HelmStrategy helmStrategy
31-
private final K8sClient k8sClient
32-
private final Config config
24+
NetworkingUtils networkingUtils
25+
HelmStrategy helmStrategy
26+
K8sClient k8sClient
27+
Config config
28+
ScmManagerSetup scmManagerSetup
3329

3430
ScmManager(Config config, ScmManagerConfig scmmConfig, HelmStrategy helmStrategy, K8sClient k8sClient, NetworkingUtils networkingUtils) {
3531
this.scmmConfig = scmmConfig
3632
this.config = config
3733
this.helmStrategy = helmStrategy
3834
this.k8sClient = k8sClient
3935
this.networkingUtils = networkingUtils
40-
setupHelm()
36+
this.scmManagerSetup = new ScmManagerSetup(this)
37+
this.scmManagerSetup.setupHelm()
4138
this.urls = new ScmManagerUrlResolver(this.config, this.scmmConfig, this.k8sClient, this.networkingUtils)
4239
this.apiClient = new ScmManagerApiClient(this.urls.clientApiBase().toString(), this.scmmConfig.credentials, this.config.application.insecure)
43-
waitForScmmAvailable()
44-
setup()
40+
this.scmManagerSetup.waitForScmmAvailable()
41+
this.scmManagerSetup.setup()
4542
}
4643

4744
// --- Git operations ---
@@ -184,179 +181,4 @@ class ScmManager implements GitProvider {
184181
Objects.requireNonNull(config, "config must not be null").application.insecure
185182
)
186183
}
187-
188-
void waitForScmmAvailable(int timeoutSeconds = 60, int intervalMillis = 2000) {
189-
log.info("Restarting SCM-Manager!")
190-
long startTime = System.currentTimeMillis()
191-
long timeoutMillis = timeoutSeconds * 1000L
192-
193-
while (System.currentTimeMillis() - startTime < timeoutMillis) {
194-
try {
195-
def call = this.apiClient.generalApi().checkScmmAvailable()
196-
def response = call.execute()
197-
198-
if (response.successful) {
199-
return
200-
} else {
201-
println "SCM-Manager not ready yet: HTTP ${response.code()}"
202-
}
203-
} catch (Exception e) {
204-
println "Waiting for SCM-Manager... Error: ${e.message}"
205-
}
206-
207-
sleep(intervalMillis)
208-
}
209-
throw new RuntimeException("Timeout: SCM-Manager did not respond with 200 OK within ${timeoutSeconds} seconds")
210-
}
211-
212-
void setup() {
213-
installScmmPlugins()
214-
setSetupConfigs()
215-
configureJenkinsPlugin()
216-
addDefaultUsers()
217-
}
218-
219-
void setupHelm() {
220-
def releaseName = 'scmm'
221-
222-
def templatedMap = TemplatingEngine.templateToMap(HELM_VALUES_PATH, [
223-
host : this.scmmConfig.ingress,
224-
remote : this.config.application.remote,
225-
username : this.scmmConfig.credentials.username,
226-
password : this.scmmConfig.credentials.password,
227-
helm : this.scmmConfig.helm,
228-
releaseName: releaseName
229-
])
230-
231-
def helmConfig = this.scmmConfig.helm
232-
def mergedMap = MapUtils.deepMerge(helmConfig.values, templatedMap)
233-
def tempValuesPath = new FileSystemUtils().writeTempFile(mergedMap)
234-
this.helmStrategy.deployFeature(
235-
helmConfig.repoURL,
236-
'scm-manager',
237-
helmConfig.chart,
238-
helmConfig.version,
239-
this.scmmConfig.namespace,
240-
releaseName,
241-
tempValuesPath
242-
)
243-
}
244-
245-
def installScmmPlugins(Boolean restart = true) {
246-
247-
if (System.getenv('SKIP_PLUGINS')?.toLowerCase() == 'true') {
248-
log.info("Skipping SCM plugin installation due to SKIP_PLUGINS=true")
249-
return
250-
}
251-
252-
if (System.getenv('SKIP_RESTART')?.toLowerCase() == 'true') {
253-
log.info("Skipping SCMM restart due to SKIP_RESTART=true")
254-
restart = false
255-
}
256-
257-
def pluginNames = [
258-
"scm-mail-plugin",
259-
"scm-review-plugin",
260-
"scm-code-editor-plugin",
261-
"scm-editor-plugin",
262-
"scm-landingpage-plugin",
263-
"scm-el-plugin",
264-
"scm-readme-plugin",
265-
"scm-webhook-plugin",
266-
"scm-ci-plugin",
267-
"scm-metrics-prometheus-plugin"
268-
]
269-
270-
if (this.config.jenkins.active) {
271-
pluginNames.add("scm-jenkins-plugin")
272-
}
273-
274-
Boolean restartForThisPlugin = true
275-
276-
pluginNames.each { String pluginName ->
277-
log.info("Installing Plugin ${pluginName} ...")
278-
restartForThisPlugin = restart && pluginName == pluginNames.last()
279-
ScmManagerApiClient.handleApiResponse(this.apiClient.pluginApi().install(pluginName, restartForThisPlugin))
280-
}
281-
282-
log.info("SCM-Manager plugin installation finished successfully!")
283-
if (restartForThisPlugin) {
284-
waitForScmmAvailable()
285-
}
286-
}
287-
288-
void setSetupConfigs() {
289-
def setupConfigs = [
290-
enableProxy : false,
291-
proxyPort : 8080,
292-
proxyServer : "proxy.mydomain.com",
293-
proxyUser : null,
294-
proxyPassword : null,
295-
realmDescription : "SONIA :: SCM Manager",
296-
disableGroupingGrid : false,
297-
dateFormat : "YYYY-MM-DD HH:mm:ss",
298-
anonymousAccessEnabled : false,
299-
anonymousMode : "OFF",
300-
baseUrl : this.url,
301-
forceBaseUrl : false,
302-
loginAttemptLimit : -1,
303-
proxyExcludes : [],
304-
skipFailedAuthenticators: false,
305-
pluginUrl : "https://plugin-center-api.scm-manager.org/api/v1/plugins/{version}?os={os}&arch={arch}",
306-
loginAttemptLimitTimeout: 300,
307-
enabledXsrfProtection : true,
308-
namespaceStrategy : "CustomNamespaceStrategy",
309-
loginInfoUrl : "https://login-info.scm-manager.org/api/v1/login-info",
310-
releaseFeedUrl : "https://scm-manager.org/download/rss.xml",
311-
mailDomainName : "scm-manager.local",
312-
adminGroups : [],
313-
adminUsers : []
314-
]
315-
316-
ScmManagerApiClient.handleApiResponse(this.apiClient.generalApi().setConfig(setupConfigs))
317-
log.debug("Successfully added SCMM Setup Configs")
318-
}
319-
320-
void configureJenkinsPlugin() {
321-
322-
def jenkinsPluginConfig = [
323-
disableRepositoryConfiguration: false,
324-
disableMercurialTrigger : false,
325-
disableGitTrigger : false,
326-
disableEventTrigger : false,
327-
url : this.url
328-
]
329-
330-
ScmManagerApiClient.handleApiResponse(this.apiClient.pluginApi().configureJenkinsPlugin(jenkinsPluginConfig))
331-
log.debug("Successfully configured JenkinsPlugin in SCM-Manager.")
332-
}
333-
334-
void addDefaultUsers() {
335-
def metricsUsername = "${this.config.application.namePrefix}metrics"
336-
addUser(this.scmmConfig.gitOpsUsername, this.scmmConfig.password)
337-
addUser(metricsUsername, this.scmmConfig.password)
338-
grantUserPermissions(metricsUsername, ["metrics:read"])
339-
}
340-
341-
void addUser(String username, String password, String email = 'changeme@test.local') {
342-
ScmManagerUser userRequest = [
343-
name : username,
344-
displayName: username,
345-
mail : email,
346-
external : false,
347-
password : password,
348-
active : true,
349-
_links : [:]
350-
]
351-
ScmManagerApiClient.handleApiResponse(this.apiClient.usersApi().addUser(userRequest))
352-
log.debug("Successfully created SCM-Manager User.")
353-
}
354-
355-
void grantUserPermissions(String username,List<String> permissions){
356-
def permissionBody = [
357-
permissions: permissions
358-
]
359-
ScmManagerApiClient.handleApiResponse(this.apiClient.usersApi().setPermissionForUser(username, permissionBody))
360-
log.debug("Granted permissions ${permissions} to user ${username}.")
361-
}
362184
}

0 commit comments

Comments
 (0)