Skip to content

Commit

Permalink
Read deployment configuration from database instead of configuration
Browse files Browse the repository at this point in the history
Fixes #126
  • Loading branch information
micheljung committed Aug 14, 2017
1 parent 31eb165 commit 885b5b2
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 105 deletions.
12 changes: 0 additions & 12 deletions src/main/java/com/faforever/api/config/FafApiProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@Data
@ConfigurationProperties(prefix = "faf-api", ignoreUnknownFields = false)
Expand Down Expand Up @@ -134,16 +132,6 @@ public static class Deployment {
private String repositoriesDirectory;
private String filesDirectoryFormat = "updates_%s_files";
private String forgedAllianceExePath;
private List<DeploymentConfiguration> configurations = new ArrayList<>();

@Data
public static class DeploymentConfiguration {
private String repositoryUrl;
private String branch;
private String modName;
private String modFilesExtension;
private boolean replaceExisting;
}
}

@Data
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/faforever/api/data/domain/FeaturedMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class FeaturedMod {
private int order;
private String gitUrl;
private String gitBranch;
private String fileExtension;
/** Whether overriding an existing version is allowed. */
private boolean allowOverride;

@Id
@Column(name = "id")
Expand Down Expand Up @@ -62,4 +65,14 @@ public String getGitUrl() {
public String getGitBranch() {
return gitBranch;
}

@Column(name = "file_extension")
public String getFileExtension() {
return fileExtension;
}

@Column(name = "allow_override")
public boolean isAllowOverride() {
return allowOverride;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.faforever.api.deployment;

import com.faforever.api.config.FafApiProperties;
import com.faforever.api.config.FafApiProperties.Deployment.DeploymentConfiguration;
import com.faforever.api.data.domain.FeaturedMod;
import com.faforever.api.featuredmods.FeaturedModService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -21,24 +22,26 @@ public class GitHubDeploymentService {
private final ApplicationContext applicationContext;
private final FafApiProperties fafApiProperties;
private final ObjectMapper objectMapper;
private final FeaturedModService featuredModService;

public GitHubDeploymentService(ApplicationContext applicationContext, FafApiProperties fafApiProperties, ObjectMapper objectMapper) {
public GitHubDeploymentService(ApplicationContext applicationContext, FafApiProperties fafApiProperties,
ObjectMapper objectMapper, FeaturedModService featuredModService) {
this.applicationContext = applicationContext;
this.fafApiProperties = fafApiProperties;
this.objectMapper = objectMapper;
this.featuredModService = featuredModService;
}

@SneakyThrows
public void createDeploymentIfEligible(Push push) {
void createDeploymentIfEligible(Push push) {
String ref = push.getRef();
Optional<DeploymentConfiguration> optional = fafApiProperties.getDeployment().getConfigurations().stream()
.filter(configuration ->
push.getRepository().gitHttpTransportUrl().equals(configuration.getRepositoryUrl())
&& push.getRef().replace("refs/heads/", "").equals(configuration.getBranch()))
.findFirst();
String httpUrl = push.getRepository().gitHttpTransportUrl();
String branch = push.getRef().replace("refs/heads/", "");

Optional<FeaturedMod> optional = featuredModService.findByGitUrlAndGitBranch(httpUrl, branch);

if (!optional.isPresent()) {
log.warn("No configuration present for repository '{}' and ref '{}'", push.getRepository().gitHttpTransportUrl(), push.getRef());
log.warn("No configuration present for repository '{}' and branch '{}'", httpUrl, branch);
return;
}

Expand All @@ -53,15 +56,15 @@ public void createDeploymentIfEligible(Push push) {

@Async
@SneakyThrows
public void deploy(GHDeployment deployment) {
void deploy(GHDeployment deployment) {
String environment = deployment.getEnvironment();
if (!fafApiProperties.getGitHub().getDeploymentEnvironment().equals(environment)) {
log.warn("Ignoring deployment for environment: {}", environment);
return;
}

applicationContext.getBean(LegacyFeaturedModDeploymentTask.class)
.setConfiguration(objectMapper.readValue(deployment.getPayload(), DeploymentConfiguration.class))
.setFeaturedMod(objectMapper.readValue(deployment.getPayload(), FeaturedMod.class))
.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.faforever.api.config.FafApiProperties;
import com.faforever.api.config.FafApiProperties.Deployment;
import com.faforever.api.config.FafApiProperties.Deployment.DeploymentConfiguration;
import com.faforever.api.data.domain.FeaturedMod;
import com.faforever.api.deployment.git.GitWrapper;
import com.faforever.api.featuredmods.FeaturedModFile;
import com.faforever.api.featuredmods.FeaturedModService;
Expand Down Expand Up @@ -62,7 +62,7 @@ public class LegacyFeaturedModDeploymentTask implements Runnable {
private final FafApiProperties apiProperties;

@Setter
private DeploymentConfiguration configuration;
private FeaturedMod featuredMod;

public LegacyFeaturedModDeploymentTask(GitWrapper gitWrapper, FeaturedModService featuredModService, FafApiProperties apiProperties) {
this.gitWrapper = gitWrapper;
Expand All @@ -73,14 +73,14 @@ public LegacyFeaturedModDeploymentTask(GitWrapper gitWrapper, FeaturedModService
@Override
@SneakyThrows
public void run() {
Assert.state(configuration != null, "Configuration must be set");
String modName = configuration.getModName();
Assert.state(featuredMod != null, "Configuration must be set");
String modName = featuredMod.getTechnicalName();
Assert.state(VALID_MOD_NAMES.contains(modName), "Unsupported mod: " + modName);

String repositoryUrl = configuration.getRepositoryUrl();
String branch = configuration.getBranch();
boolean replaceExisting = configuration.isReplaceExisting();
String modFilesExtension = configuration.getModFilesExtension();
String repositoryUrl = featuredMod.getGitUrl();
String branch = featuredMod.getGitBranch();
boolean replaceExisting = featuredMod.isAllowOverride();
String modFilesExtension = featuredMod.getFileExtension();
Map<String, Short> fileIds = featuredModService.getFileIds(modName);

log.info("Starting deployment of '{}' from '{}', branch '{}', replaceExisting '{}', modFilesExtension '{}'",
Expand Down Expand Up @@ -193,17 +193,17 @@ private StagedFile renameToFinalFile(StagedFile file) {
}

/**
* Creates a ZIP file with the file ending configured in {@link #configuration}. The content of the ZIP file is the
* Creates a ZIP file with the file ending configured in {@link #featuredMod}. The content of the ZIP file is the
* content of the directory. If no file ID is available, an empty optional is returned.
*/
@SneakyThrows
private Optional<StagedFile> packDirectory(Path directory, Short version, Path targetFolder, Map<String, Short> fileIds) {
String directoryName = directory.getFileName().toString();
Path targetNxtFile = targetFolder.resolve(String.format("%s.%d.%s", directoryName, version, configuration.getModFilesExtension()));
Path targetNxtFile = targetFolder.resolve(String.format("%s.%d.%s", directoryName, version, featuredMod.getFileExtension()));
Path tmpNxtFile = toTmpFile(targetNxtFile);

// E.g. "effects.nx2"
String clientFileName = String.format("%s.%s", directoryName, configuration.getModFilesExtension());
String clientFileName = String.format("%s.%s", directoryName, featuredMod.getFileExtension());
Short fileId = fileIds.get(clientFileName);
if (fileId == null) {
log.debug("Skipping folder '{}' because there's no file ID available", directoryName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import com.faforever.api.data.domain.FeaturedMod;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface FeaturedModRepository extends JpaRepository<FeaturedMod, Integer> {
Optional<FeaturedMod> findByGitUrlAndGitBranch(String url, String branch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class FeaturedModService {
Expand Down Expand Up @@ -40,4 +41,8 @@ public void save(String modName, short version, List<FeaturedModFile> featuredMo
public Map<String, Short> getFileIds(String modName) {
return legacyFeaturedModFileRepository.getFileIds(modName);
}

public Optional<FeaturedMod> findByGitUrlAndGitBranch(String url, String branch) {
return featuredModRepository.findByGitUrlAndGitBranch(url, branch);
}
}
17 changes: 0 additions & 17 deletions src/main/resources/config/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ faf-api:
forged-alliance-exe-path: ${FORGED_ALLIANCE_EXE_PATH}
repositories-directory: ${REPOSITORIES_DIRECTORY:build/cache/repos}
featured-mods-target-directory: ${FEATURED_MODS_TARGET_DIRECTORY:build/cache/deployment}
# TODO make this runtime configuration
configurations:
- repositoryUrl: https://github.com/FAForever/fa.git
branch: deploy/faf
modName: faf
modFilesExtension: nx2
replaceExisting: false
- repositoryUrl: https://github.com/FAForever/fa.git
branch: deploy/fafbeta
modName: fafbeta
modFilesExtension: nx4
replaceExisting: true
- repositoryUrl: https://github.com/FAForever/fa.git
branch: deploy/fafdevelop
modName: fafdevelop
modFilesExtension: nx5
replaceExisting: true
clan:
website-url-format: ${CLAN_WEBSITE_URL_FORMAT:http://clans.test.faforever.com/clan/%s}

Expand Down
22 changes: 0 additions & 22 deletions src/main/resources/config/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,6 @@ faf-api:
forged-alliance-exe-path: ${FORGED_ALLIANCE_EXE_PATH}
repositories-directory: ${REPOSITORIES_DIRECTORY}
featured-mods-target-directory: ${FEATURED_MODS_TARGET_DIRECTORY}
# TODO make this runtime configuration
configurations:
- repositoryUrl: https://github.com/FAForever/fa.git
branch: deploy/faf
modName: faf
modFilesExtension: nx2
replaceExisting: false
- repositoryUrl: https://github.com/FAForever/fa.git
branch: deploy/fafbeta
modName: fafbeta
modFilesExtension: nx4
replaceExisting: true
- repositoryUrl: https://github.com/FAForever/fa.git
branch: deploy/fafdevelop
modName: fafdevelop
modFilesExtension: nx5
replaceExisting: true
- repositoryUrl: https://github.com/FAForever/faf-phantomx.git
branch: master
modName: phantomx
modFilesExtension: phx
replaceExisting: false
registration:
activation-url-format: ${ACTIVATION_URL_FORMAT}
mail:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.faforever.api.deployment;

import com.faforever.api.config.FafApiProperties;
import com.faforever.api.config.FafApiProperties.Deployment.DeploymentConfiguration;
import com.faforever.api.data.domain.FeaturedMod;
import com.faforever.api.featuredmods.FeaturedModService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -14,7 +15,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationContext;

import java.util.Collections;
import java.util.Optional;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
Expand All @@ -28,6 +29,7 @@ public class GitHubDeploymentServiceTest {

private static final String EXAMPLE_REPO_URL = "https://example.com/repo.git";
private static final String ENVIRONMENT = "junit";
private static final String EXAMPLE_BRANCH = "master";
private GitHubDeploymentService instance;

private FafApiProperties apiProperties;
Expand All @@ -36,11 +38,13 @@ public class GitHubDeploymentServiceTest {
private ApplicationContext applicationContext;
@Mock
private ObjectMapper objectMapper;
@Mock
private FeaturedModService featuredModService;

@Before
public void setUp() throws Exception {
apiProperties = new FafApiProperties();
instance = new GitHubDeploymentService(applicationContext, apiProperties, objectMapper);
instance = new GitHubDeploymentService(applicationContext, apiProperties, objectMapper, featuredModService);
}

@Test
Expand All @@ -50,6 +54,7 @@ public void createDeploymentIfEligibleNoConfigurationAvailable() throws Exceptio
GHRepository repository = mock(GHRepository.class);
when(repository.gitHttpTransportUrl()).thenReturn(EXAMPLE_REPO_URL);
when(push.getRepository()).thenReturn(repository);
when(push.getRef()).thenReturn("refs/heads/master");

instance.createDeploymentIfEligible(push);

Expand All @@ -70,18 +75,18 @@ public void createDeploymentIfEligible() throws Exception {
when(deploymentBuilder.payload(anyString())).thenReturn(deploymentBuilder);
when(repository.createDeployment("refs/heads/master")).thenReturn(deploymentBuilder);

when(objectMapper.writeValueAsString(any(DeploymentConfiguration.class))).thenReturn("");
when(featuredModService.findByGitUrlAndGitBranch(EXAMPLE_REPO_URL, EXAMPLE_BRANCH))
.thenReturn(Optional.of(new FeaturedMod()
.setGitBranch(EXAMPLE_BRANCH)
.setFileExtension("nx2")
.setTechnicalName("faf")
.setGitUrl(EXAMPLE_REPO_URL)));

when(objectMapper.writeValueAsString(any(FeaturedMod.class))).thenReturn("");

when(push.getRepository()).thenReturn(repository);

apiProperties.getGitHub().setDeploymentEnvironment(ENVIRONMENT);
apiProperties.getDeployment().setConfigurations(Collections.singletonList(
new DeploymentConfiguration()
.setBranch("master")
.setModFilesExtension(".nx2")
.setModName("faf")
.setRepositoryUrl(EXAMPLE_REPO_URL)
));

instance.createDeploymentIfEligible(push);

Expand All @@ -108,7 +113,7 @@ public void deployEnvironmentMatch() throws Exception {
when(deployment.getEnvironment()).thenReturn(ENVIRONMENT);

LegacyFeaturedModDeploymentTask task = mock(LegacyFeaturedModDeploymentTask.class);
when(task.setConfiguration(any())).thenReturn(task);
when(task.setFeaturedMod(any())).thenReturn(task);
when(applicationContext.getBean(LegacyFeaturedModDeploymentTask.class)).thenReturn(task);

instance.deploy(deployment);
Expand Down

0 comments on commit 885b5b2

Please sign in to comment.