|
| 1 | +package com.eficode.devstack.deployment.impl |
| 2 | + |
| 3 | +import com.eficode.devstack.container.Container |
| 4 | +import com.eficode.devstack.container.impl.AllureContainer |
| 5 | +import com.eficode.devstack.container.impl.JsmContainer |
| 6 | +import com.eficode.devstack.deployment.Deployment |
| 7 | +import com.eficode.devstack.util.DirectorySyncer |
| 8 | +import com.eficode.devstack.util.DockerClientDS |
| 9 | +import de.gesellix.docker.client.DockerClient |
| 10 | +import de.gesellix.docker.client.EngineResponseContent |
| 11 | +import de.gesellix.docker.remote.api.Volume |
| 12 | +import org.slf4j.Logger |
| 13 | +import org.slf4j.LoggerFactory |
| 14 | + |
| 15 | + |
| 16 | +class JsmDevDeployment implements Deployment { |
| 17 | + |
| 18 | + Logger log = LoggerFactory.getLogger(this.class) |
| 19 | + String friendlyName = "JSM Development Platform" |
| 20 | + String deploymentNetworkName = "jsmdev" |
| 21 | + ArrayList<Deployment> subDeployments = [] |
| 22 | + |
| 23 | + DirectorySyncer srcSyncer |
| 24 | + ArrayList<String> srcCodePaths |
| 25 | + Volume srcCodeVolume |
| 26 | + |
| 27 | + AllureContainer allureContainer |
| 28 | + Volume allureReportVolume |
| 29 | + |
| 30 | + DockerClientDS dockerClient |
| 31 | + |
| 32 | + //Used when naming various Docker components |
| 33 | + String componentsPrefix |
| 34 | + |
| 35 | + JsmDevDeployment(String jiraBaseUrl, String dockerHost, String dockerCertPath, ArrayList<String> srcCodePaths) { |
| 36 | + |
| 37 | + componentsPrefix = AllureContainer.extractDomainFromUrl(jiraBaseUrl) |
| 38 | + |
| 39 | + allureContainer = new AllureContainer(dockerHost, dockerCertPath) |
| 40 | + allureContainer.containerName = componentsPrefix + "-reporter" |
| 41 | + dockerClient = allureContainer.dockerClient |
| 42 | + |
| 43 | + allureReportVolume = getOrCreateVolume( componentsPrefix+ "-reports") |
| 44 | + allureContainer.prepareCustomEnvVar(["CHECK_RESULTS_EVERY_SECONDS=3", "KEEP_HISTORY=1", "KEEP_HISTORY_LATEST=30"]) |
| 45 | + allureContainer.prepareVolumeMount(allureReportVolume.name, "/app/allure-results", false) |
| 46 | + |
| 47 | + |
| 48 | + srcCodeVolume = getOrCreateVolume(componentsPrefix + "-code") |
| 49 | + this.srcCodePaths = srcCodePaths |
| 50 | + |
| 51 | + subDeployments.add(new JsmH2Deployment(jiraBaseUrl, dockerHost, dockerCertPath)) |
| 52 | + jsmDeployment.jsmContainer.prepareVolumeMount(srcCodeVolume.name, "/var/atlassian/application-data/jira/scripts/", false) |
| 53 | + jsmDeployment.jsmContainer.prepareVolumeMount(allureReportVolume.name, "/var/atlassian/application-data/jira/allure-results/", false) |
| 54 | + |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + JsmH2Deployment getJsmDeployment() { |
| 59 | + return subDeployments.find { it instanceof JsmH2Deployment } as JsmH2Deployment |
| 60 | + } |
| 61 | + |
| 62 | + JsmContainer getJsmContainer() { |
| 63 | + return jsmDeployment.jsmContainer |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + @Override |
| 68 | + ArrayList<Container> getContainers() { |
| 69 | + return [srcSyncer, allureContainer, jsmContainer] |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + void setContainers(ArrayList<Container> containers) { |
| 74 | + throw new InputMismatchException("Not implemented") |
| 75 | + } |
| 76 | + |
| 77 | + Volume getOrCreateVolume(String volumeName) { |
| 78 | + Volume volume = dockerClient.getVolumesWithName(volumeName).find { true } |
| 79 | + |
| 80 | + if (volume) { |
| 81 | + log.debug("\tFound existing volume:" + volume.name) |
| 82 | + } else { |
| 83 | + log.debug("\tCreating new volume $volumeName") |
| 84 | + EngineResponseContent<Volume> volumeResponse = dockerClient.createVolume(volumeName) |
| 85 | + volume = volumeResponse?.content |
| 86 | + assert volume: "Error creating volume $volumeName, " + volumeResponse?.getStatus()?.text |
| 87 | + log.debug("\t\tCreated volume:" + volume.name) |
| 88 | + } |
| 89 | + |
| 90 | + return volume |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + boolean setupDeployment() { |
| 95 | + |
| 96 | + |
| 97 | + srcSyncer = DirectorySyncer.createSyncToVolume(srcCodePaths, srcCodeVolume.name, "-avh --chown=2001:2001") |
| 98 | + allureContainer.created ?: allureContainer.createContainer() |
| 99 | + allureContainer.startContainer() |
| 100 | + |
| 101 | + jsmDeployment.setupDeployment(true, false) |
| 102 | + |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + public static class Builder { |
| 107 | + |
| 108 | + private String jsmLicense |
| 109 | + private String jsmBaseUrl |
| 110 | + private String jsmVersion = "latest" |
| 111 | + private String jsmJvmDebugPort = "5005" |
| 112 | + private Boolean enableJsmDooD = false |
| 113 | + |
| 114 | + private Map<String, String> appsToInstall = [:] |
| 115 | + |
| 116 | + private String dockerHost |
| 117 | + private String dockerCertPath |
| 118 | + |
| 119 | + private ArrayList<String> srcCodePaths |
| 120 | + |
| 121 | + Builder(String baseUrl, String jsmLicense, ArrayList<String> srcCodePaths) { |
| 122 | + this.jsmLicense = jsmLicense |
| 123 | + this.jsmBaseUrl = baseUrl |
| 124 | + this.srcCodePaths = srcCodePaths |
| 125 | + } |
| 126 | + |
| 127 | + Builder setJsmVersion(String version) { |
| 128 | + this.jsmVersion = version |
| 129 | + return this |
| 130 | + } |
| 131 | + |
| 132 | + Builder setJsmJvmDebugPort(String port) { |
| 133 | + this.jsmJvmDebugPort = port |
| 134 | + return this |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Enable Docker Outside of Docker by mounting "/var/run/docker.sock" in to jsm container |
| 139 | + * @return |
| 140 | + */ |
| 141 | + Builder enableJsmDood() { |
| 142 | + this.enableJsmDooD = true |
| 143 | + return this |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + Builder addAppToInstall(String appUrl, String appLicense = "") { |
| 148 | + this.appsToInstall.put(appUrl, appLicense) |
| 149 | + return this |
| 150 | + } |
| 151 | + |
| 152 | + Builder setDockerHost(String host) { |
| 153 | + this.dockerHost = host |
| 154 | + return this |
| 155 | + } |
| 156 | + |
| 157 | + Builder setDockerCertPath(String certPath) { |
| 158 | + this.dockerCertPath = certPath |
| 159 | + return this |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + JsmDevDeployment build() { |
| 164 | + |
| 165 | + JsmDevDeployment devDeployment = new JsmDevDeployment(jsmBaseUrl, dockerHost, dockerCertPath, srcCodePaths) |
| 166 | + |
| 167 | + devDeployment.jsmDeployment.jsmContainer.containerImageTag = this.jsmVersion |
| 168 | + devDeployment.jsmDeployment.jsmContainer.created ?: devDeployment.jsmDeployment.jsmContainer.enableJvmDebug(this.jsmJvmDebugPort) |
| 169 | + devDeployment.jsmDeployment.setJiraLicense(this.jsmLicense) |
| 170 | + devDeployment.jsmDeployment.jsmContainer.enableAppUpload() |
| 171 | + if (enableJsmDooD) { |
| 172 | + devDeployment.jsmDeployment.jsmContainer.prepareBindMount("/var/run/docker.sock", "/var/run/docker.sock", false) |
| 173 | + } |
| 174 | + devDeployment.jsmDeployment.appsToInstall = this.appsToInstall |
| 175 | + |
| 176 | + |
| 177 | + return devDeployment |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | +} |
| 185 | + |
| 186 | + |
0 commit comments