Skip to content

Commit

Permalink
devops Config classified
Browse files Browse the repository at this point in the history
  • Loading branch information
pitiscarf committed Nov 12, 2024
1 parent dc1d93f commit 4fec166
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
21 changes: 9 additions & 12 deletions devops/scripts/build-front-app-docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const process = require("node:process");
const UserInput = require("./lib/user-input");
const ScalewayClient = require("./lib/scaleway-client");
const { GetSecrets, SECRET_FORMATS } = require("./get-secrets");
const { getConfig } = require("./lib/config");
const { Config } = require("./lib/config");
const {
childProcess,
childProcessStdin,
Expand All @@ -27,22 +27,19 @@ async function main() {
input.SCW_SECRET_KEY,
input.SCW_ORGANIZATION_ID
);
const { projectName, secretName, registry } = getConfig(
input.environment,
input.application
);
const config = await new GetSecrets(scaleway, {
projectName: projectName,
secretName: secretName,
const config = new Config(input.environment, input.application);
const secrets = await new GetSecrets(scaleway, {
projectName: config.projectName(),
secretName: config.secretName(),
format: SECRET_FORMATS.ENVFILE,
}).execute();

const env = {
...process.env,
};
const values = { ...config, ...env }; // override config from env
const values = { ...secrets, ...env }; // override secrets from env

const image = registryEndpoint(registry, values[RELEASE_KEY]);
const image = registryEndpoint(config.registry(), values[RELEASE_KEY]);

const args = [
"build",
Expand All @@ -54,7 +51,7 @@ async function main() {
`${input.application}/Dockerfile`,
".",
];
for (const key in config) {
for (const key in secrets) {
const value = values[key];
if (SECRET_KEYS.has(key)) {
args.push("--secret");
Expand All @@ -71,7 +68,7 @@ async function main() {
if (input.push) {
await childProcessStdin(
"docker",
["login", registry, "-u", "nologin", "--password-stdin"],
["login", config.registry(), "-u", "nologin", "--password-stdin"],
input.SCW_SECRET_KEY,
{ env }
);
Expand Down
15 changes: 6 additions & 9 deletions devops/scripts/build-front-app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const process = require("node:process");
const UserInput = require("./lib/user-input");
const ScalewayClient = require("./lib/scaleway-client");
const { getConfig } = require("./lib/config");
const { Config } = require("./lib/config");
const { childProcess } = require("./lib/utils");
const { GetSecrets, SECRET_FORMATS } = require("./get-secrets");

Expand All @@ -17,18 +17,15 @@ async function main() {
input.SCW_SECRET_KEY,
input.SCW_ORGANIZATION_ID
);
const { projectName, secretName } = getConfig(
input.environment,
input.application
);
const config = await new GetSecrets(scaleway, {
projectName: projectName,
secretName: secretName,
const config = new Config(input.environment, input.application);
const secrets = await new GetSecrets(scaleway, {
projectName: config.projectName(),
secretName: config.secretName(),
format: SECRET_FORMATS.ENVFILE,
}).execute();

const env = {
...config,
...secrets,
...process.env,
APP_NAME: input.application,
};
Expand Down
17 changes: 10 additions & 7 deletions devops/scripts/deploy-scaleway.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const UserInput = require("./lib/user-input");
const ScalewayClient = require("./lib/scaleway-client");
const { getConfig } = require("./lib/config");
const { Config } = require("./lib/config");
const { registryEndpoint, sleep } = require("./lib/utils");

const POLL_INTERVAL_MS = 5000;
Expand Down Expand Up @@ -29,15 +29,18 @@ async function main() {
input.SCW_SECRET_KEY,
input.SCW_ORGANIZATION_ID
);
const { containerNamespace, containerName, registry } = getConfig(
input.environment,
input.application
const config = new Config(input.environment, input.application);

const namespace = await scaleway.findContainerNamespace(
config.containerNamespace()
);
const container = await scaleway.findContainer(
namespace.id,
config.containerName()
);
const namespace = await scaleway.findContainerNamespace(containerNamespace);
const container = await scaleway.findContainer(namespace.id, containerName);

await scaleway.updateContainer(container.id, {
registry_image: registryEndpoint(registry, input.release),
registry_image: registryEndpoint(config.registry(), input.release),
});

await waitUntilSuccess(scaleway, container.id);
Expand Down
44 changes: 30 additions & 14 deletions devops/scripts/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
const ENVS = new Set(["ci", "staging", "production"]);
const APPS = new Set(["app", "admin"]);
const APPS = new Set(["app", "admin", "api"]);

function getConfig(env, app) {
if (!ENVS.has(env)) {
throw new Error(`Unknown environment: ${env}`);
class Config {
constructor(environment, application) {
if (!ENVS.has(environment)) {
throw new Error(`Unknown environment: ${environment}`);
}
if (!APPS.has(application)) {
throw new Error(`Unknown application: ${application}`);
}
this.env = environment;
this.app = application;
}
if (!APPS.has(app)) {
throw new Error(`Unknown application: ${app}`);

containerName() {
return `${this.env}-${this.app}`;
}

containerNamespace() {
return `snu-${this.env}`;
}

projectName() {
return `snu-${this.env}`;
}

secretName() {
return `${this.env}-${this.app}`;
}

return {
containerNamespace: `snu-${env}`,
containerName: `${env}-${app}`,
projectName: `snu-${env}`,
secretName: `${env}-${app}`,
registry: `rg.fr-par.scw.cloud/snu-${env}/${app}`,
};
registry() {
return `rg.fr-par.scw.cloud/snu-${this.env}/${this.app}`;
}
}

module.exports = {
getConfig,
Config,
};

0 comments on commit 4fec166

Please sign in to comment.