Skip to content

Commit 8a360ef

Browse files
committed
create better isolation for networks
1 parent cfdf1bd commit 8a360ef

File tree

7 files changed

+165
-83
lines changed

7 files changed

+165
-83
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ jobs:
176176
id: deploy
177177
run: |
178178
cd ./${{ github.event.repository.name }}
179+
yarn install
179180
yarn deploy \
180181
--clear_data=no \
181182
--environment=${{ inputs.environment }} \
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as fs from 'fs'
2+
import * as yaml from 'yaml'
3+
4+
interface DockerCompose {
5+
version: string
6+
services: Record<string, Service>
7+
networks: Record<string, Network>
8+
}
9+
10+
interface Service {
11+
image?: string
12+
environment?: { [key: string]: string }
13+
volumes?: string[]
14+
ports?: string[]
15+
networks: string[]
16+
}
17+
18+
interface Network {
19+
driver?: string
20+
external?: boolean
21+
}
22+
23+
// Function to add networks to services and networks section
24+
function addNetworksToCompose(composeFile: string, networksList: string) {
25+
// Read and parse the existing docker-compose YAML file
26+
const fileContent = fs.readFileSync(composeFile, 'utf8')
27+
const composeObject = yaml.parse(fileContent) as DockerCompose
28+
29+
// Convert the comma-separated networks list into an array
30+
const networksArray = networksList
31+
.split(',')
32+
.map((network) => network.trim())
33+
.filter((network) => network.length > 0)
34+
.map((stack) => `${stack}_dependencies_net`)
35+
.concat('traefik_net')
36+
37+
// Add networks to each service
38+
for (const serviceName in composeObject.services) {
39+
if (serviceName in composeObject.services) {
40+
const service = composeObject.services[serviceName]
41+
if (!service.networks) {
42+
service.networks = []
43+
}
44+
networksArray.forEach((network) => {
45+
if (!service.networks.includes(network)) {
46+
service.networks.push(network)
47+
}
48+
})
49+
}
50+
}
51+
52+
// Add networks to the global networks section
53+
if (!composeObject.networks) {
54+
composeObject.networks = {}
55+
}
56+
57+
networksArray.forEach((network) => {
58+
if (!composeObject.networks[network]) {
59+
composeObject.networks[network] = { driver: 'overlay' }
60+
}
61+
})
62+
63+
// Convert the updated object back to YAML and output it
64+
const updatedComposeYaml = yaml.stringify(composeObject)
65+
console.log(updatedComposeYaml)
66+
}
67+
68+
// Parse arguments from the command line
69+
const [composeFile, networksList] = process.argv.slice(2)
70+
71+
if (!composeFile || !networksList) {
72+
console.error(
73+
'Usage: ts-node script.ts <docker-compose-file> <networks-list>'
74+
)
75+
process.exit(1)
76+
}
77+
78+
// Call the function to update the compose file
79+
addNetworksToCompose(composeFile, networksList)

infrastructure/deployment/deploy.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ docker_stack_deploy() {
300300

301301
echo "Updating docker swarm stack with new compose files"
302302

303+
EXISTING_STACKS=$(configured_ssh 'docker stack ls --format "{{ .Name }}" | grep -v "dependencies" | paste -sd "," -')
304+
305+
configured_rsync -rlD $SSH_USER@$SSH_HOST:/opt/opencrvs/infrastructure/docker-compose.dependencies.yml ./infrastructure/docker-compose.dependencies.yml
306+
307+
if echo $EXISTING_STACKS | grep -w $STACK > /dev/null; then
308+
echo "Stack $STACK exists"
309+
npx tsx infrastructure/deployment/add-networks.ts infrastructure/docker-compose.dependencies.yml "$EXISTING_STACKS" > ./docker-compose.dependencies.yml
310+
else
311+
echo "Stack $STACK doesnt exist. Creating"
312+
UPDATE_DEPENDENCIES=true
313+
npx tsx infrastructure/deployment/add-networks.ts infrastructure/docker-compose.dependencies.yml "$EXISTING_STACKS,$STACK" > ./docker-compose.dependencies.yml
314+
fi
315+
316+
configured_rsync -rlD ./docker-compose.dependencies.yml $SSH_USER@$SSH_HOST:/opt/opencrvs/infrastructure/docker-compose.dependencies.yml
317+
318+
303319
if [ "$UPDATE_DEPENDENCIES" = true ]; then
304320
echo "Updating dependency stack"
305321
configured_ssh 'cd /opt/opencrvs && \
@@ -379,7 +395,6 @@ echo "Deploying COUNTRY_CONFIG_VERSION $COUNTRY_CONFIG_VERSION to $SSH_HOST..."
379395
echo
380396
echo "Syncing configuration files to the target server"
381397

382-
383398
configured_rsync -rlD $PROJECT_ROOT/infrastructure $SSH_USER@$SSH_HOST:/opt/opencrvs/ --delete --no-perms --omit-dir-times --verbose
384399

385400
echo "Logging to Dockerhub"

0 commit comments

Comments
 (0)