Skip to content

Commit

Permalink
add branchHasConfigDirectory functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
acwhite211 committed Nov 21, 2024
1 parent 03a6ac2 commit 7db53ad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/lib/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type Deployment = Partial<DeploymentDetails> & {
readonly group?: string;
readonly branch: string;
readonly digest?: string;
readonly hasInteralSp7ConfigDirectory?: boolean;
};

export type DeploymentWithInfo = Deployment & {
Expand Down
3 changes: 2 additions & 1 deletion app/lib/dockerCompose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const resolveVersion = (deployment: Deployment) =>
export const createDockerConfig = (
deployments: RA<ActiveDeployment>,
// This is used just to make docker Nginx container if config changed
nginxConfigHash: number
nginxConfigHash: number,
sp7HasConfig: boolean = false
): string => `
version: '3.9'
services:
Expand Down
8 changes: 5 additions & 3 deletions app/lib/nginx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ server {
proxy_read_timeout 300s;
client_max_body_size 0;
location /static/ {
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
Expand All @@ -30,8 +29,11 @@ server {
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
root /volumes;
# rewrite ^/static/config/(.*)$ /specify${deployment.schemaVersion}/config/$1 break;
rewrite ^/static/config/(.*)$ /${deployment.hostname}-static-files/specify-config/config/$1 break;
${
deployment.hasInteralSp7ConfigDirectory
? `rewrite ^/static/config/(.*)$ /${deployment.hostname}-static-files/specify-config/config/$1 break;`
: `rewrite ^/static/config/(.*)$ /specify${deployment.schemaVersion}/config/$1 break;`
}
rewrite ^/static/depository/(.*)$ /${deployment.hostname}-static-files/depository/$1 break;
rewrite ^/static/(.*)$ /${deployment.hostname}-static-files/frontend-static/$1 break;
}
Expand Down
32 changes: 28 additions & 4 deletions app/pages/api/state/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import fs from 'node:fs';
import path from 'node:path';
import fetch from 'node-fetch';

import {
nginxConfDirectory as nginxConfigDirectory,
Expand Down Expand Up @@ -45,6 +46,23 @@ const getHash = (string: string): number =>
0
);

// Given a branch name, chech GitHub if that branch has a 'config/' directory.
// Do this by doing a GET request to 'github.com/specify/specify7/tree/{deployment.branch}/config'.
// IF the request returns a 200 status code, then the branch has a 'config/' directory,
// otherwise it does not.
// Return true if it does, false otherwise
// #HackyAF
const branchHasConfigDirectory = async (branch: string): Promise<boolean> => {
const url = `https://github.com/specify/specify7/tree/${branch}/config`;
try {
const response = await fetch(url);
return response.status === 200;
} catch (error) {
console.error(`Error checking config directory for branch ${branch}:`, error);
return false;
}
};

export async function setState(
deployments: RA<Deployment>,
user: User,
Expand All @@ -58,10 +76,16 @@ export async function setState(
);

const branches = await fetchTagsForImage('specify7-service');
const state = rawState.map((deployment) => ({
...deployment,
digest: branches[deployment.branch]?.digest ?? deployment.digest,
}));
const state = await Promise.all(
rawState.map(async (deployment) => {
const hasInteralSp7ConfigDirectory = await branchHasConfigDirectory(deployment.branch);
return {
...deployment,
digest: branches[deployment.branch]?.digest ?? deployment.digest,
hasInteralSp7ConfigDirectory,
};
})
);

await fs.promises.writeFile(configurationFile, JSON.stringify(state));
const nginxConfig = createNginxConfig(state, new URL(origin).host);
Expand Down

0 comments on commit 7db53ad

Please sign in to comment.