Skip to content

Commit

Permalink
Dynamically create network per job and cleanup afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurre Stender committed Aug 11, 2021
1 parent c61313c commit e83a05d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion __tests__/updater-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Updater', () => {
await removeDanglingUpdaterContainers()
})

jest.setTimeout(25000)
jest.setTimeout(120000)
it('should run the updater and create a pull request', async () => {
// Skip the test when we haven't preloaded the updater image
if (process.env.SKIP_INTEGRATION_TESTS) {
Expand Down
22 changes: 20 additions & 2 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import Docker, {Container} from 'dockerode'
import Docker, {Container, Network} from 'dockerode'
import crypto from 'crypto'
import {
BasicAuthCredentials,
Expand Down Expand Up @@ -43,15 +43,18 @@ const CERT_SUBJECT = [

export class Proxy {
container?: Container
networkName: string
url: string
cert: string
network?: Network

constructor(
private readonly docker: Docker,
private readonly proxyImage: string
) {
// TODO: this is obviously gnarly, decouple some things so we don't need to
// initialize these as empty strings
this.networkName = ''
this.url = ''
this.cert = ''
}
Expand All @@ -61,7 +64,11 @@ export class Proxy {
const config = this.buildProxyConfig(credentials, details.id)
this.cert = config.ca.cert

this.networkName = `job-${details.id}-network`
this.network = await this.ensureNetwork(this.networkName)

this.container = await this.createContainer(details.id, name)

await ContainerService.storeInput(
CONFIG_FILE_NAME,
CONFIG_FILE_PATH,
Expand All @@ -81,6 +88,17 @@ export class Proxy {
core.info(this.url)
}

private async ensureNetwork(name: string): Promise<Network> {
const networks = await this.docker.listNetworks({
filters: JSON.stringify({name: [name]})
})
if (networks.length > 0) {
return this.docker.getNetwork(networks[0].Id)
} else {
return await this.docker.createNetwork({Name: name})
}
}

private buildProxyConfig(
credentials: Credential[],
jobID: string
Expand Down Expand Up @@ -130,7 +148,7 @@ export class Proxy {
AttachStderr: true,
Env: [`JOB_ID=${jobID}`],
HostConfig: {
NetworkMode: `job-test-network` // TODO: Dynamically generate network
NetworkMode: this.networkName
}
})

Expand Down
4 changes: 2 additions & 2 deletions src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class Updater {
} finally {
await this.proxy.container?.stop()
await this.proxy.container?.remove()
await this.proxy.network?.remove()
}
}

Expand Down Expand Up @@ -123,7 +124,6 @@ export class Updater {
}

private async createContainer(updaterCommand: string): Promise<Container> {
core.info(`Proxy: ${this.proxy.url}`)
const container = await this.docker.createContainer({
Image: this.updaterImage,
AttachStdout: true,
Expand All @@ -147,7 +147,7 @@ export class Updater {
`/usr/sbin/update-ca-certificates && $DEPENDABOT_HOME/dependabot-updater/bin/run ${updaterCommand}`
],
HostConfig: {
NetworkMode: 'job-test-network',
NetworkMode: this.proxy.networkName,
Binds: [
`${path.join(__dirname, '../output')}:${JOB_OUTPUT_PATH}:rw`,
`${path.join(__dirname, '../repo')}:${REPO_CONTENTS_PATH}:rw`
Expand Down

0 comments on commit e83a05d

Please sign in to comment.