Skip to content

Commit

Permalink
Move building the updater container into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurre Stender committed Oct 26, 2021
1 parent c78cf6e commit e739a7a
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {PROXY_IMAGE_NAME, UPDATER_IMAGE_NAME} from '../src/main'
import {ContainerService} from '../src/container-service'
import {ImageService} from '../src/image-service'
import {removeDanglingUpdaterContainers, integration} from './helpers'
import Docker from 'dockerode'
Expand All @@ -8,8 +7,9 @@ import {ProxyBuilder} from '../src/proxy'
import path from 'path'
import fs from 'fs'
import {JobParameters} from '../src/inputs'
import {UpdaterBuilder} from '../src/updater-builder'

integration('ContainerService', () => {
integration('UpdaterBuilder', () => {
const docker = new Docker()
const credentials: Credential[] = [
{
Expand Down Expand Up @@ -65,17 +65,15 @@ integration('ContainerService', () => {
'172.17.0.1',
workingDirectory
)
const container = await ContainerService.createUpdaterContainer(
'updater-image-test',
params,
const container = await new UpdaterBuilder(
docker,
params,
input,
outputPath,
proxy,
repoPath,
'fetch_files',
UPDATER_IMAGE_NAME
)
).run('updater-image-test', 'fetch_files')

const containerInfo = await container.inspect()

Expand Down
152 changes: 103 additions & 49 deletions dist/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

76 changes: 1 addition & 75 deletions src/container-service.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,12 @@
import * as core from '@actions/core'
import Docker, {Container} from 'dockerode'
import {Container} from 'dockerode'
import {pack} from 'tar-stream'
import {FileFetcherInput, FileUpdaterInput, ProxyConfig} from './config-types'
import {JobParameters} from './inputs'
import {Proxy} from './proxy'
import {outStream, errStream} from './utils'

const JOB_OUTPUT_FILENAME = 'output.json'
const JOB_OUTPUT_PATH = '/home/dependabot/dependabot-updater/output'
const JOB_INPUT_FILENAME = 'job.json'
const JOB_INPUT_PATH = `/home/dependabot/dependabot-updater`
const REPO_CONTENTS_PATH = '/home/dependabot/dependabot-updater/repo'
const CA_CERT_INPUT_PATH = '/usr/local/share/ca-certificates'
const CA_CERT_FILENAME = 'dbot-ca.crt'
const UPDATER_MAX_MEMORY = 8 * 1024 * 1024 * 1024 // 8GB in bytes

class ContainerRuntimeError extends Error {}

export const ContainerService = {
async createUpdaterContainer(
containerName: string,
jobParams: JobParameters,
docker: Docker,
input: FileFetcherInput | FileUpdaterInput,
outputHostPath: string,
proxy: Proxy,
repoHostPath: string,
updaterCommand: string,
updaterImage: string
): Promise<Container> {
const cmd = `(echo > /etc/ca-certificates.conf) &&\
rm -Rf /usr/share/ca-certificates/ &&\
/usr/sbin/update-ca-certificates &&\
$DEPENDABOT_HOME/dependabot-updater/bin/run ${updaterCommand}`

const container = await docker.createContainer({
Image: updaterImage,
name: containerName,
AttachStdout: true,
AttachStderr: true,
Env: [
`DEPENDABOT_JOB_ID=${jobParams.jobId}`,
`DEPENDABOT_JOB_TOKEN=${jobParams.jobToken}`,
`DEPENDABOT_JOB_PATH=${JOB_INPUT_PATH}/${JOB_INPUT_FILENAME}`,
`DEPENDABOT_OUTPUT_PATH=${JOB_OUTPUT_PATH}/${JOB_OUTPUT_FILENAME}`,
`DEPENDABOT_REPO_CONTENTS_PATH=${REPO_CONTENTS_PATH}`,
`DEPENDABOT_API_URL=${jobParams.dependabotApiDockerUrl}`,
`SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt`,
`http_proxy=${proxy.url}`,
`HTTP_PROXY=${proxy.url}`,
`https_proxy=${proxy.url}`,
`HTTPS_PROXY=${proxy.url}`
],
Cmd: ['sh', '-c', cmd],
HostConfig: {
Memory: UPDATER_MAX_MEMORY,
NetworkMode: proxy.networkName,
Binds: [
`${outputHostPath}:${JOB_OUTPUT_PATH}:rw`,
`${repoHostPath}:${REPO_CONTENTS_PATH}:rw`
]
}
})

await ContainerService.storeCert(
CA_CERT_FILENAME,
CA_CERT_INPUT_PATH,
container,
proxy.cert
)

await ContainerService.storeInput(
JOB_INPUT_FILENAME,
JOB_INPUT_PATH,
container,
input
)

core.info(`Created ${updaterCommand} container: ${container.id}`)
return container
},

async storeInput(
name: string,
path: string,
Expand Down
81 changes: 81 additions & 0 deletions src/updater-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as core from '@actions/core'
import Docker, {Container} from 'dockerode'
import {ContainerService} from './container-service'
import {FileFetcherInput, FileUpdaterInput} from './config-types'
import {JobParameters} from './inputs'
import {Proxy} from './proxy'

const JOB_OUTPUT_FILENAME = 'output.json'
const JOB_OUTPUT_PATH = '/home/dependabot/dependabot-updater/output'
const JOB_INPUT_FILENAME = 'job.json'
const JOB_INPUT_PATH = `/home/dependabot/dependabot-updater`
const REPO_CONTENTS_PATH = '/home/dependabot/dependabot-updater/repo'
const CA_CERT_INPUT_PATH = '/usr/local/share/ca-certificates'
const CA_CERT_FILENAME = 'dbot-ca.crt'
const UPDATER_MAX_MEMORY = 8 * 1024 * 1024 * 1024 // 8GB in bytes

export class UpdaterBuilder {
constructor(
private readonly docker: Docker,
private readonly jobParams: JobParameters,
private readonly input: FileFetcherInput | FileUpdaterInput,
private readonly outputHostPath: string,
private readonly proxy: Proxy,
private readonly repoHostPath: string,

private readonly updaterImage: string
) {}

async run(containerName: string, updaterCommand: string): Promise<Container> {
const cmd = `(echo > /etc/ca-certificates.conf) &&\
rm -Rf /usr/share/ca-certificates/ &&\
/usr/sbin/update-ca-certificates &&\
$DEPENDABOT_HOME/dependabot-updater/bin/run ${updaterCommand}`

const container = await this.docker.createContainer({
Image: this.updaterImage,
name: containerName,
AttachStdout: true,
AttachStderr: true,
Env: [
`DEPENDABOT_JOB_ID=${this.jobParams.jobId}`,
`DEPENDABOT_JOB_TOKEN=${this.jobParams.jobToken}`,
`DEPENDABOT_JOB_PATH=${JOB_INPUT_PATH}/${JOB_INPUT_FILENAME}`,
`DEPENDABOT_OUTPUT_PATH=${JOB_OUTPUT_PATH}/${JOB_OUTPUT_FILENAME}`,
`DEPENDABOT_REPO_CONTENTS_PATH=${REPO_CONTENTS_PATH}`,
`DEPENDABOT_API_URL=${this.jobParams.dependabotApiDockerUrl}`,
`SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt`,
`http_proxy=${this.proxy.url}`,
`HTTP_PROXY=${this.proxy.url}`,
`https_proxy=${this.proxy.url}`,
`HTTPS_PROXY=${this.proxy.url}`
],
Cmd: ['sh', '-c', cmd],
HostConfig: {
Memory: UPDATER_MAX_MEMORY,
NetworkMode: this.proxy.networkName,
Binds: [
`${this.outputHostPath}:${JOB_OUTPUT_PATH}:rw`,
`${this.repoHostPath}:${REPO_CONTENTS_PATH}:rw`
]
}
})

await ContainerService.storeCert(
CA_CERT_FILENAME,
CA_CERT_INPUT_PATH,
container,
this.proxy.cert
)

await ContainerService.storeInput(
JOB_INPUT_FILENAME,
JOB_INPUT_PATH,
container,
this.input
)

core.info(`Created ${updaterCommand} container: ${container.id}`)
return container
}
}
Loading

0 comments on commit e739a7a

Please sign in to comment.