-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from github/feelepxyz/extract-container-service
Extract container service and helpers from updater
- Loading branch information
Showing
6 changed files
with
139 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import Docker from 'dockerode' | ||
|
|
||
| import {ContainerService} from '../src/container-service' | ||
| import {ImageService} from '../src/image-service' | ||
|
|
||
| describe('ContainerService', () => { | ||
| const docker = new Docker() | ||
| let container: any | ||
|
|
||
| beforeAll(async () => { | ||
| await ImageService.pull('alpine') | ||
| container = await docker.createContainer({ | ||
| Image: 'alpine', | ||
| AttachStdout: true, | ||
| AttachStderr: true, | ||
| Cmd: ['/bin/sh', '-c', 'echo $VAR'], | ||
| Env: ['VAR=env-var'] | ||
| }) | ||
| }) | ||
|
|
||
| test('runs containers', async () => { | ||
| await ContainerService.run(container) | ||
| }) | ||
| }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import {base64DecodeDependencyFile} from '../src/utils' | ||
|
|
||
| describe('base64DecodeDependencyFile', () => { | ||
| test('clones the dependency file', () => { | ||
| const dependencyFile = { | ||
| name: 'package.json', | ||
| content: 'dGVzdCBzdHJpbmc=', | ||
| directory: '/', | ||
| type: 'file', | ||
| support_file: false, | ||
| content_encoding: 'utf-8', | ||
| deleted: false, | ||
| operation: 'add' | ||
| } | ||
|
|
||
| const decoded = base64DecodeDependencyFile(dependencyFile) | ||
| expect(decoded.content).toEqual('test string') | ||
| expect(dependencyFile.content).toEqual('dGVzdCBzdHJpbmc=') | ||
| }) | ||
| }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import * as core from '@actions/core' | ||
| import {Container} from 'dockerode' | ||
| import {pack} from 'tar-stream' | ||
| import {FileFetcherInput, FileUpdaterInput} from './file-types' | ||
|
|
||
| export const ContainerService = { | ||
| async storeInput( | ||
| name: string, | ||
| path: string, | ||
| container: Container, | ||
| input: FileFetcherInput | FileUpdaterInput | ||
| ): Promise<void> { | ||
| const tar = pack() | ||
| tar.entry({name}, JSON.stringify(input)) | ||
| tar.finalize() | ||
| await container.putArchive(tar, {path}) | ||
| }, | ||
|
|
||
| async run(container: Container): Promise<void> { | ||
| try { | ||
| const stream = await container.attach({ | ||
| stream: true, | ||
| stdout: true, | ||
| stderr: true | ||
| }) | ||
| container.modem.demuxStream(stream, process.stdout, process.stderr) | ||
|
|
||
| await container.start() | ||
| await container.wait() | ||
| } finally { | ||
| await container.remove() | ||
| core.info(`Cleaned up container ${container.id}`) | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import {Credential, JobDetails} from './api-client' | ||
|
|
||
| export type FetchedFiles = { | ||
| base_commit_sha: string | ||
| dependency_files: any[] | ||
| base64_dependency_files: any[] | ||
| } | ||
|
|
||
| export type FileFetcherInput = { | ||
| job: JobDetails | ||
| credentials: Credential[] | ||
| } | ||
|
|
||
| export type DependencyFile = { | ||
| name: string | ||
| content: any | ||
| directory: string | ||
| type: string | ||
| support_file: boolean | ||
| content_encoding: string | ||
| deleted: boolean | ||
| operation: string | ||
| } | ||
|
|
||
| export type FileUpdaterInput = FetchedFiles & { | ||
| job: JobDetails | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import {DependencyFile} from './file-types' | ||
|
|
||
| const base64Decode = (str: string): string => | ||
| Buffer.from(str, 'base64').toString('binary') | ||
|
|
||
| export const base64DecodeDependencyFile = ( | ||
| file: DependencyFile | ||
| ): DependencyFile => { | ||
| const fileCopy = JSON.parse(JSON.stringify(file)) | ||
| fileCopy.content = base64Decode(fileCopy.content) | ||
| return fileCopy | ||
| } |