Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
clivebor committed Sep 17, 2021
2 parents 9cd4090 + 9fc0973 commit f9fab32
Show file tree
Hide file tree
Showing 133 changed files with 2,425 additions and 979 deletions.
2 changes: 1 addition & 1 deletion ahoy-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>za.co.lsd.ahoy</groupId>
<artifactId>ahoy</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</parent>
<artifactId>ahoy-server</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import za.co.lsd.ahoy.server.environmentrelease.EnvironmentRelease;
import za.co.lsd.ahoy.server.environmentrelease.EnvironmentReleaseId;
import za.co.lsd.ahoy.server.environmentrelease.EnvironmentReleaseRepository;
import za.co.lsd.ahoy.server.releases.PromoteOptions;
import za.co.lsd.ahoy.server.releases.ReleaseVersion;
import za.co.lsd.ahoy.server.releases.ReleaseVersionRepository;
import za.co.lsd.ahoy.server.releases.UpgradeOptions;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -76,15 +78,17 @@ public ResponseEntity<EnvironmentRelease> undeploy(@PathVariable Long environmen
return new ResponseEntity<>(undeployedEnvironmentRelease.get(), new HttpHeaders(), HttpStatus.OK);
}

@PostMapping("/promote/{environmentId}/{releaseId}/{destEnvironmentId}")
public ResponseEntity<EnvironmentRelease> promote(@PathVariable Long environmentId, @PathVariable Long releaseId, @PathVariable Long destEnvironmentId) {
EnvironmentRelease promotedEnvironmentRelease = releaseService.promote(environmentId, releaseId, destEnvironmentId);
@PostMapping("/promote/{environmentId}/{releaseId}")
public ResponseEntity<EnvironmentRelease> promote(@PathVariable Long environmentId, @PathVariable Long releaseId,
@RequestBody PromoteOptions promoteOptions) {
EnvironmentRelease promotedEnvironmentRelease = releaseService.promote(environmentId, releaseId, promoteOptions);
return new ResponseEntity<>(promotedEnvironmentRelease, new HttpHeaders(), HttpStatus.OK);
}

@PostMapping("/upgrade/{releaseVersionId}/{version}")
public ResponseEntity<ReleaseVersion> upgrade(@PathVariable Long releaseVersionId, @PathVariable String version) {
ReleaseVersion upgradedReleaseVersion = releaseService.upgrade(releaseVersionId, version);
@PostMapping("/upgrade/{releaseVersionId}")
public ResponseEntity<ReleaseVersion> upgrade(@PathVariable Long releaseVersionId,
@RequestBody UpgradeOptions upgradeOptions) {
ReleaseVersion upgradedReleaseVersion = releaseService.upgrade(releaseVersionId, upgradeOptions);
return new ResponseEntity<>(upgradedReleaseVersion, new HttpHeaders(), HttpStatus.OK);
}

Expand Down
87 changes: 55 additions & 32 deletions ahoy-server/src/main/java/za/co/lsd/ahoy/server/ReleaseService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 LSD Information Technology (Pty) Ltd
* Copyright 2021 LSD Information Technology (Pty) Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,8 +35,10 @@
import za.co.lsd.ahoy.server.environments.Environment;
import za.co.lsd.ahoy.server.environments.EnvironmentException;
import za.co.lsd.ahoy.server.environments.EnvironmentRepository;
import za.co.lsd.ahoy.server.releases.PromoteOptions;
import za.co.lsd.ahoy.server.releases.ReleaseVersion;
import za.co.lsd.ahoy.server.releases.ReleaseVersionRepository;
import za.co.lsd.ahoy.server.releases.UpgradeOptions;

import java.util.ArrayList;
import java.util.Objects;
Expand All @@ -56,12 +58,12 @@ public class ReleaseService {
private ApplicationEventPublisher eventPublisher;

public ReleaseService(EnvironmentRepository environmentRepository,
EnvironmentReleaseRepository environmentReleaseRepository,
ReleaseVersionRepository releaseVersionRepository,
ApplicationEnvironmentConfigRepository applicationEnvironmentConfigRepository,
ApplicationEnvironmentConfigProvider environmentConfigProvider,
ApplicationReleaseStatusRepository applicationReleaseStatusRepository,
ReleaseManager releaseManager) {
EnvironmentReleaseRepository environmentReleaseRepository,
ReleaseVersionRepository releaseVersionRepository,
ApplicationEnvironmentConfigRepository applicationEnvironmentConfigRepository,
ApplicationEnvironmentConfigProvider environmentConfigProvider,
ApplicationReleaseStatusRepository applicationReleaseStatusRepository,
ReleaseManager releaseManager) {
this.environmentRepository = environmentRepository;
this.environmentReleaseRepository = environmentReleaseRepository;
this.releaseVersionRepository = releaseVersionRepository;
Expand Down Expand Up @@ -151,8 +153,9 @@ public Future<EnvironmentRelease> remove(EnvironmentReleaseId environmentRelease
}

@Transactional
public EnvironmentRelease promote(Long environmentId, Long releaseId, Long destEnvironmentId) {
public EnvironmentRelease promote(Long environmentId, Long releaseId, PromoteOptions promoteOptions) {
EnvironmentReleaseId environmentReleaseId = new EnvironmentReleaseId(environmentId, releaseId);
Long destEnvironmentId = promoteOptions.getDestEnvironmentId();
log.info("Promoting environment release: {} to environment: {}", environmentReleaseId, destEnvironmentId);

EnvironmentRelease environmentRelease = environmentReleaseRepository.findById(environmentReleaseId)
Expand All @@ -171,37 +174,36 @@ public EnvironmentRelease promote(Long environmentId, Long releaseId, Long destE
promotedEnvironmentRelease.setId(new EnvironmentReleaseId());
promotedEnvironmentRelease.setRelease(environmentRelease.getRelease());
promotedEnvironmentRelease.setEnvironment(destEnvironment);
promotedEnvironmentRelease = environmentReleaseRepository.save(promotedEnvironmentRelease);

return environmentReleaseRepository.save(promotedEnvironmentRelease);
if (promoteOptions.isCopyEnvironmentConfig()) {
log.info("Copy environment config selected, copying config to promoted environment release: {}", promotedEnvironmentRelease);

for (ReleaseVersion releaseVersion : environmentRelease.getRelease().getReleaseVersions()) {
copyEnvironmentConfig(environmentRelease, promotedEnvironmentRelease, releaseVersion);
}
}

return promotedEnvironmentRelease;
}
}

@Transactional
public ReleaseVersion upgrade(Long releaseVersionId, String version) {
public ReleaseVersion upgrade(Long releaseVersionId, UpgradeOptions upgradeOptions) {
ReleaseVersion currentReleaseVersion = releaseVersionRepository.findById(releaseVersionId)
.orElseThrow(() -> new ResourceNotFoundException("Could not find release version: " + releaseVersionId));

log.info("Upgrading release version: {} to version: {}", currentReleaseVersion.getVersion(), version);
log.info("Upgrading release version: {} to version: {}", currentReleaseVersion, upgradeOptions.getVersion());

ReleaseVersion upgradedReleaseVersion = new ReleaseVersion(version, currentReleaseVersion.getRelease(), new ArrayList<>(currentReleaseVersion.getApplicationVersions()));
ReleaseVersion upgradedReleaseVersion = new ReleaseVersion(upgradeOptions.getVersion(), currentReleaseVersion.getRelease(), new ArrayList<>(currentReleaseVersion.getApplicationVersions()));
upgradedReleaseVersion = releaseVersionRepository.save(upgradedReleaseVersion);

Iterable<EnvironmentRelease> environmentReleases = environmentReleaseRepository.findByRelease_Id(currentReleaseVersion.getRelease().getId());
for (EnvironmentRelease environmentRelease : environmentReleases) {

for (ApplicationVersion applicationVersion : upgradedReleaseVersion.getApplicationVersions()) {
Optional<ApplicationEnvironmentConfig> currentEnvironmentConfig = environmentConfigProvider.environmentConfigFor(
environmentRelease, currentReleaseVersion, applicationVersion);
if (upgradeOptions.isCopyEnvironmentConfig()) {
log.info("Copy environment config selected, copying config to new version: {}", upgradedReleaseVersion);

if (currentEnvironmentConfig.isPresent()) {
ApplicationDeploymentId id = new ApplicationDeploymentId(
environmentRelease.getId(),
upgradedReleaseVersion.getId(),
applicationVersion.getId());

ApplicationEnvironmentConfig newEnvironmentConfig = new ApplicationEnvironmentConfig(id, currentEnvironmentConfig.get());
applicationEnvironmentConfigRepository.save(newEnvironmentConfig);
}
Iterable<EnvironmentRelease> environmentReleases = environmentReleaseRepository.findByRelease_Id_OrderByEnvironmentId(currentReleaseVersion.getRelease().getId());
for (EnvironmentRelease environmentRelease : environmentReleases) {
copyEnvironmentConfig(environmentRelease, currentReleaseVersion, upgradedReleaseVersion);
}
}

Expand All @@ -220,25 +222,46 @@ public EnvironmentRelease copyEnvConfig(Long environmentId, Long releaseId, Long
ReleaseVersion destReleaseVersion = releaseVersionRepository.findById(destReleaseVersionId)
.orElseThrow(() -> new ResourceNotFoundException("Could not find destReleaseVersionId: " + destReleaseVersionId));

copyEnvironmentConfig(environmentRelease, sourceReleaseVersion, destReleaseVersion);

return environmentRelease;
}

/**
* Copies environment config from one release version to another for the same environment release.
*/
private void copyEnvironmentConfig(EnvironmentRelease environmentRelease, ReleaseVersion sourceReleaseVersion, ReleaseVersion destReleaseVersion) {
this.copyEnvironmentConfig(environmentRelease, sourceReleaseVersion, environmentRelease, destReleaseVersion);
}

/**
* Copies environment config from one environment release to another for the same version.
*/
private void copyEnvironmentConfig(EnvironmentRelease sourceEnvironmentRelease, EnvironmentRelease destEnvironmentRelease, ReleaseVersion releaseVersion) {
this.copyEnvironmentConfig(sourceEnvironmentRelease, releaseVersion, destEnvironmentRelease, releaseVersion);
}

/**
* Copies environment config from one environment release version to another environment release version.
*/
private void copyEnvironmentConfig(EnvironmentRelease sourceEnvironmentRelease, ReleaseVersion sourceReleaseVersion, EnvironmentRelease destEnvironmentRelease, ReleaseVersion destReleaseVersion) {
for (ApplicationVersion applicationVersion : destReleaseVersion.getApplicationVersions()) {
Optional<ApplicationEnvironmentConfig> sourceConfig = environmentConfigProvider.environmentConfigFor(
environmentRelease, sourceReleaseVersion, applicationVersion);
sourceEnvironmentRelease, sourceReleaseVersion, applicationVersion);

if (sourceConfig.isPresent()) {
Optional<ApplicationEnvironmentConfig> destConfig = environmentConfigProvider.environmentConfigFor(
environmentRelease, destReleaseVersion, applicationVersion);
destEnvironmentRelease, destReleaseVersion, applicationVersion);

if (destConfig.isEmpty()) {
ApplicationDeploymentId id = new ApplicationDeploymentId(
environmentRelease.getId(),
destEnvironmentRelease.getId(),
destReleaseVersion.getId(),
applicationVersion.getId());
applicationEnvironmentConfigRepository.save(new ApplicationEnvironmentConfig(id, sourceConfig.get()));
}
}
}

return environmentRelease;
}

@Async("deploymentTaskExecutor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Application {

@OneToMany(mappedBy = "application", cascade = CascadeType.REMOVE)
@JsonIgnore
@OrderBy("id")
private List<ApplicationVersion> applicationVersions;

public ApplicationVersion latestApplicationVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -43,29 +40,37 @@ public class ApplicationEnvironmentConfig {

@OneToMany(mappedBy = "applicationEnvironmentConfig", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationEnvironmentConfigReference")
@OrderBy("id")
private List<ApplicationEnvironmentVariable> environmentVariables;

@OneToMany(mappedBy = "applicationEnvironmentConfig", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationEnvironmentConfigReference")
@OrderBy("id")
private List<ApplicationConfig> configs;

@OneToMany(mappedBy = "applicationEnvironmentConfig", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationEnvironmentConfigReference")
@OrderBy("id")
private List<ApplicationVolume> volumes;

@OneToMany(mappedBy = "applicationEnvironmentConfig", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationEnvironmentConfigReference")
@OrderBy("id")
private List<ApplicationSecret> secrets;

public ApplicationEnvironmentConfig(ApplicationDeploymentId id, ApplicationEnvironmentConfig applicationEnvironmentConfig) {
this.id = id;
this.replicas = applicationEnvironmentConfig.getReplicas();
this.routeHostname = applicationEnvironmentConfig.getRouteHostname();
this.routeTargetPort = applicationEnvironmentConfig.getRouteTargetPort();
this.environmentVariables = new ArrayList<>(applicationEnvironmentConfig.getEnvironmentVariables());
this.configs = new ArrayList<>(applicationEnvironmentConfig.getConfigs());
this.volumes = new ArrayList<>(applicationEnvironmentConfig.getVolumes());
this.secrets = new ArrayList<>(applicationEnvironmentConfig.getSecrets());
this.environmentVariables = applicationEnvironmentConfig.getEnvironmentVariables() != null ?
new ArrayList<>(applicationEnvironmentConfig.getEnvironmentVariables()) : null;
this.configs = applicationEnvironmentConfig.getConfigs() != null ?
new ArrayList<>(applicationEnvironmentConfig.getConfigs()) : null;
this.volumes = applicationEnvironmentConfig.getVolumes() != null ?
new ArrayList<>(applicationEnvironmentConfig.getVolumes()) : null;
this.secrets = applicationEnvironmentConfig.getSecrets() != null ?
new ArrayList<>(applicationEnvironmentConfig.getSecrets()) : null;
}

public ApplicationEnvironmentConfig(String routeHostname, Integer routeTargetPort) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 LSD Information Technology (Pty) Ltd
* Copyright 2021 LSD Information Technology (Pty) Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,8 +30,8 @@ public interface ApplicationProjection {
String getName();

@Value("#{target.applicationVersions}")
List<ApplicationVersion> getApplicationVersions();
List<ApplicationVersionSummaryProjection> getApplicationVersions();

@Value("#{target.latestApplicationVersion()}")
ApplicationVersion getLatestApplicationVersion();
ApplicationVersionSimpleProjection getLatestApplicationVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package za.co.lsd.ahoy.server.applications;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ApplicationRepository extends CrudRepository<Application, Long> {
public interface ApplicationRepository extends PagingAndSortingRepository<Application, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package za.co.lsd.ahoy.server.applications;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Data;
import lombok.NoArgsConstructor;
import za.co.lsd.ahoy.server.docker.DockerRegistry;
import za.co.lsd.ahoy.server.releases.ReleaseVersion;
import za.co.lsd.ahoy.server.util.IntegerListConverter;

import javax.persistence.*;
Expand Down Expand Up @@ -57,26 +59,42 @@ public class ApplicationVersion implements Serializable {

@OneToMany(mappedBy = "applicationVersion", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationVersionReference")
@OrderBy("id")
private List<ApplicationEnvironmentVariable> environmentVariables;

@OneToMany(mappedBy = "applicationVersion", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationVersionReference")
@OrderBy("id")
private List<ApplicationConfig> configs;

@OneToMany(mappedBy = "applicationVersion", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationVersionReference")
@OrderBy("id")
private List<ApplicationVolume> volumes;

@OneToMany(mappedBy = "applicationVersion", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("applicationVersionReference")
@OrderBy("id")
private List<ApplicationSecret> secrets;

@ManyToMany(mappedBy = "applicationVersions")
@JsonIgnore
@OrderBy("id")
private List<ReleaseVersion> releaseVersions;

public ApplicationVersion(@NotNull String version, @NotNull String image, Application application) {
this.version = version;
this.image = image;
this.application = application;
}

public ApplicationVersion(@NotNull Long id, @NotNull String version, @NotNull String image, Application application) {
this.id = id;
this.version = version;
this.image = image;
this.application = application;
}

public boolean hasConfigs() {
return configs != null && configs.size() > 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 LSD Information Technology (Pty) Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.lsd.ahoy.server.applications;

import org.springframework.data.rest.core.config.Projection;

@Projection(name = "applicationVersionSimple", types = {ApplicationVersion.class})
public interface ApplicationVersionSimpleProjection {
long getId();

String getVersion();

String getImage();
}
Loading

0 comments on commit f9fab32

Please sign in to comment.