Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pipenv): Use centralized docker execution #5062

Merged
merged 7 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/manager/pipenv/__snapshots__/artifacts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ Array [
exports[`.updateArtifacts() supports docker mode 1`] = `
Array [
Object {
"cmd": "docker run --rm --user=foobar -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/renovate/cache/others/pipenv\\":\\"/tmp/renovate/cache/others/pipenv\\" -e LC_ALL -e LANG -e PIPENV_CACHE_DIR -w \\"/tmp/github/some/repo\\" renovate/pipenv pipenv lock",
"cmd": "docker pull renovate/pipenv",
"options": Object {
"encoding": "utf-8",
},
},
Object {
"cmd": "docker run --rm --user=foobar -v \\"/tmp/github/some/repo\\":\\"/tmp/github/some/repo\\" -v \\"/tmp/renovate/cache\\":\\"/tmp/renovate/cache\\" -v \\"/tmp/renovate/cache/others/pipenv\\":\\"/tmp/renovate/cache/others/pipenv\\" -e PIPENV_CACHE_DIR -w \\"/tmp/github/some/repo\\" renovate/pipenv bash -l -c \\"pipenv lock\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
Expand Down
27 changes: 19 additions & 8 deletions lib/manager/pipenv/artifacts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'upath';
import _fs from 'fs-extra';
import { exec as _exec } from 'child_process';
import * as pipenv from './artifacts';
Expand All @@ -7,6 +8,8 @@ import { StatusResult } from '../../platform/git/storage';
import { envMock, mockExecAll } from '../../../test/execUtil';
import * as _env from '../../util/exec/env';
import { BinarySource } from '../../util/exec/common';
import { resetPrefetchedImages } from '../../util/exec/docker';
import { setUtilConfig } from '../../util';

jest.mock('fs-extra');
jest.mock('child_process');
Expand All @@ -19,14 +22,25 @@ const env = mocked(_env);
const platform = mocked(_platform);

const config = {
localDir: '/tmp/github/some/repo',
cacheDir: '/tmp/renovate/cache',
// `join` fixes Windows CI
localDir: join('/tmp/github/some/repo'),
cacheDir: join('/tmp/renovate/cache'),
dockerUser: 'foobar',
};

const dockerConfig = { ...config, binarySource: BinarySource.Docker };

describe('.updateArtifacts()', () => {
beforeEach(() => {
jest.resetAllMocks();
env.getChildProcessEnv.mockReturnValue(envMock.basic);
env.getChildProcessEnv.mockReturnValue({
...envMock.basic,
LANG: 'en_US.UTF-8',
LC_ALL: 'en_US',
});

setUtilConfig(config);
resetPrefetchedImages();
});

it('returns if no Pipfile.lock found', async () => {
Expand Down Expand Up @@ -71,6 +85,7 @@ describe('.updateArtifacts()', () => {
expect(execSnapshots).toMatchSnapshot();
});
it('supports docker mode', async () => {
setUtilConfig(dockerConfig);
platform.getFile.mockResolvedValueOnce('Current Pipfile.lock');
const execSnapshots = mockExecAll(exec);
platform.getRepoStatus.mockResolvedValue({
Expand All @@ -82,11 +97,7 @@ describe('.updateArtifacts()', () => {
packageFileName: 'Pipfile',
updatedDeps: [],
newPackageFileContent: '{}',
config: {
...config,
binarySource: BinarySource.Docker,
dockerUser: 'foobar',
},
config: dockerConfig,
})
).not.toBeNull();
expect(execSnapshots).toMatchSnapshot();
Expand Down
51 changes: 18 additions & 33 deletions lib/manager/pipenv/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ensureDir, outputFile, readFile } from 'fs-extra';
import { join, dirname } from 'upath';
import { exec } from '../../util/exec';
import { getChildProcessEnv } from '../../util/exec/env';
import { join } from 'upath';
import { exec, ExecOptions } from '../../util/exec';
import { logger } from '../../logger';
import { UpdateArtifactsResult, UpdateArtifact } from '../common';
import { platform } from '../../platform';
import { BinarySource } from '../../util/exec/common';

export async function updateArtifacts({
packageFileName: pipfileName,
Expand All @@ -15,46 +13,33 @@ export async function updateArtifacts({
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`pipenv.updateArtifacts(${pipfileName})`);

const env = getChildProcessEnv(['LC_ALL', 'LANG', 'PIPENV_CACHE_DIR']);
env.PIPENV_CACHE_DIR =
env.PIPENV_CACHE_DIR || join(config.cacheDir, './others/pipenv');
await ensureDir(env.PIPENV_CACHE_DIR);
logger.debug('Using pipenv cache ' + env.PIPENV_CACHE_DIR);
const cacheDir =
process.env.PIPENV_CACHE_DIR || join(config.cacheDir, './others/pipenv');
await ensureDir(cacheDir);
logger.debug('Using pipenv cache ' + cacheDir);

const lockFileName = pipfileName + '.lock';
const existingLockFileContent = await platform.getFile(lockFileName);
if (!existingLockFileContent) {
logger.debug('No Pipfile.lock found');
return null;
}
const cwd = join(config.localDir, dirname(pipfileName));
try {
const localPipfileFileName = join(config.localDir, pipfileName);
await outputFile(localPipfileFileName, newPipfileContent);
const localLockFileName = join(config.localDir, lockFileName);
let cmd: string;
if (config.binarySource === BinarySource.Docker) {
logger.info('Running pipenv via docker');
cmd = `docker run --rm `;
if (config.dockerUser) {
cmd += `--user=${config.dockerUser} `;
}
const volumes = [config.localDir, env.PIPENV_CACHE_DIR];
cmd += volumes.map(v => `-v "${v}":"${v}" `).join('');
const envVars = ['LC_ALL', 'LANG', 'PIPENV_CACHE_DIR'];
cmd += envVars.map(e => `-e ${e} `).join('');
cmd += `-w "${cwd}" `;
cmd += `renovate/pipenv pipenv`;
} else {
logger.info('Running pipenv via global command');
cmd = 'pipenv';
}
const args = 'lock';
logger.debug({ cmd, args }, 'pipenv lock command');
await exec(`${cmd} ${args}`, {
cwd,
env,
});
const cmd = 'pipenv lock';
const execOptions: ExecOptions = {
extraEnv: {
PIPENV_CACHE_DIR: cacheDir,
},
docker: {
image: 'renovate/pipenv',
volumes: [cacheDir],
},
};
logger.debug({ cmd }, 'pipenv lock command');
await exec(cmd, execOptions);
const status = await platform.getRepoStatus();
if (!(status && status.modified.includes(lockFileName))) {
return null;
Expand Down