Skip to content

Commit

Permalink
feat: Add organization environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketeerbkw committed Dec 17, 2024
1 parent 17fc987 commit 6813c7b
Show file tree
Hide file tree
Showing 10 changed files with 511 additions and 242 deletions.
5 changes: 5 additions & 0 deletions node-packages/commons/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,11 @@ export async function getOrganizationById(id: number): Promise<any> {
quotaGroup
quotaRoute
quotaNotification
envVariables {
name
value
scope
}
}
}
`);
Expand Down
42 changes: 34 additions & 8 deletions node-packages/commons/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ export const getControllerBuildData = async function(deployData: any) {

// encode some values so they get sent to the controllers nicely
const sshKeyBase64 = new Buffer(deployPrivateKey.replace(/\\n/g, "\n")).toString('base64')
const [routerPattern, envVars, projectVars] = await getEnvironmentsRouterPatternAndVariables(
const [routerPattern, envVars, projectVars, orgVars] = await getEnvironmentsRouterPatternAndVariables(
lagoonProjectData,
environment.addOrUpdateEnvironment,
deployTarget.openshift,
Expand Down Expand Up @@ -653,6 +653,7 @@ export const getControllerBuildData = async function(deployData: any) {
statuspageID: uptimeRobotStatusPageId,
},
variables: {
organization: orgVars,
project: projectVars,
environment: envVars,
},
Expand Down Expand Up @@ -682,7 +683,7 @@ export const getEnvironmentsRouterPatternAndVariables = async function name(
buildPriority: number,
buildVariables: any,
bulkTask: bulkType
): Promise<[string, string, string]> {
): Promise<[string, string, string, string]> {
let projectVars: Array<Pick<EnvKeyValue, 'name' | 'value'> & {
scope: EnvVariableScope | InternalEnvVariableScope;
}> = [...project.envVariables];
Expand Down Expand Up @@ -738,9 +739,17 @@ export const getEnvironmentsRouterPatternAndVariables = async function name(
}
}

let orgVars: Array<Pick<EnvKeyValue, 'name' | 'value'> & {
scope: EnvVariableScope | InternalEnvVariableScope;
}> = [];
if (project.organization) {
// check the environment quota, this prevents environments being deployed by the api or webhooks
const curOrg = await getOrganizationById(project.organization);

orgVars = [
...curOrg.envVariables
]

// check the environment quota, this prevents environments being deployed by the api or webhooks
projectVars = [
...projectVars,
{
Expand All @@ -749,6 +758,20 @@ export const getEnvironmentsRouterPatternAndVariables = async function name(
scope: InternalEnvVariableScope.INTERNAL_SYSTEM
}
];

// @TODO
// For backwards compatibility, also add org vars with project vars until
// the remote side is checking standalone org vars data
for (const orgVar of orgVars) {
const index = projectVars.findIndex((projectVar) =>
projectVar.name === orgVar.name);

// Project vars take precedence, so don't add if one with the same name
// already exists
if (index == -1) {
projectVars.push(orgVar);
}
}
}

// handle any bulk deploy related injections here
Expand Down Expand Up @@ -822,8 +845,9 @@ export const getEnvironmentsRouterPatternAndVariables = async function name(
// encode some values so they get sent to the controllers nicely
const envVarsEncoded = new Buffer(JSON.stringify(lagoonEnvironmentVariables)).toString('base64')
const projectVarsEncoded = new Buffer(JSON.stringify(projectVars)).toString('base64')
const orgVarsEncoded = new Buffer(JSON.stringify(orgVars)).toString('base64')

return [routerPattern, envVarsEncoded, projectVarsEncoded]
return [routerPattern, envVarsEncoded, projectVarsEncoded, orgVarsEncoded]
}

/*
Expand Down Expand Up @@ -1120,13 +1144,13 @@ export const getTaskProjectEnvironmentVariables =async (projectName: string, env
// needing to trigger a full deployment
const result = await getOpenShiftInfoForProject(projectName);
const environment = await getEnvironmentByIdWithVariables(environmentId);
const [_, envVars, projectVars] = await getEnvironmentsRouterPatternAndVariables(
const [_, envVars, projectVars, orgVars] = await getEnvironmentsRouterPatternAndVariables(
result.project,
environment.environmentById,
environment.environmentById.openshift,
null, null, null, null, bulkType.Task // bulk deployments don't apply to tasks yet, but this is future proofing the function call
)
return [projectVars, envVars]
return [projectVars, envVars, orgVars]
}

export const getBaasBucketName = async (
Expand Down Expand Up @@ -1165,11 +1189,12 @@ export const createTaskTask = async function(taskData: any) {
const { project } = taskData;

// inject variables into tasks the same way it is in builds
const [_, envVars, projectVars] = await getTaskProjectEnvironmentVariables(
const [_, envVars, projectVars, orgVars] = await getTaskProjectEnvironmentVariables(
project.name,
taskData.environment.id
)
taskData.project.variables = {
organization: orgVars,
project: projectVars,
environment: envVars,
}
Expand Down Expand Up @@ -1355,11 +1380,12 @@ export const createMiscTask = async function(taskData: any) {
break;
case 'deploytarget:task:advanced':
// inject variables into advanced tasks the same way it is in builds and standard tasks
const [_, envVars, projectVars] = await getTaskProjectEnvironmentVariables(
const [_, envVars, projectVars, orgVars] = await getTaskProjectEnvironmentVariables(
taskData.data.project.name,
taskData.data.environment.id
)
miscTaskData.project.variables = {
organization: orgVars,
project: projectVars,
environment: envVars,
}
Expand Down
23 changes: 23 additions & 0 deletions services/api/database/migrations/20241216000000_org_env_var.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
return knex.schema
.alterTable('env_vars', (table) => {
table.integer('organization');
table.unique(['name', 'organization'], {indexName: 'name_organization'});
table.index('organization', 'organization_index');
})
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
return knex.schema
.alterTable('env_vars', (table) => {
table.dropColumn('organization');
})
};
4 changes: 3 additions & 1 deletion services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ const {
} = require('./resources/backup/resolvers');

const {
getEnvVarsByOrganizationId,
getEnvVarsByProjectId,
getEnvVarsByEnvironmentId,
addEnvVariable,
Expand Down Expand Up @@ -456,7 +457,8 @@ const resolvers = {
environments: getEnvironmentsByOrganizationId,
owners: getOwnersByOrganizationId,
deployTargets: getDeployTargetsByOrganizationId,
notifications: getNotificationsByOrganizationId
notifications: getNotificationsByOrganizationId,
envVariables: getEnvVarsByOrganizationId,
},
OrgProject: {
groups: getGroupsByOrganizationsProject,
Expand Down
Loading

0 comments on commit 6813c7b

Please sign in to comment.