Skip to content

Commit

Permalink
refactor(composer): Use centralized Docker exec option
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Zharinov committed Jan 18, 2020
1 parent d871b4c commit fd64216
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
41 changes: 17 additions & 24 deletions lib/manager/composer/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import is from '@sindresorhus/is';
import URL from 'url';
import fs from 'fs-extra';
import upath from 'upath';
import { exec } from '../../util/exec';
import { exec, ExecOptions } from '../../util/exec';
import { UpdateArtifact, UpdateArtifactsResult } from '../common';
import { logger } from '../../logger';
import * as hostRules from '../../util/host-rules';
import { getChildProcessEnv } from '../../util/exec/env';
import { platform } from '../../platform';
import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../constants/error-messages';
import {
Expand All @@ -22,11 +21,13 @@ export async function updateArtifacts({
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`composer.updateArtifacts(${packageFileName})`);
const env = getChildProcessEnv(['COMPOSER_CACHE_DIR']);
env.COMPOSER_CACHE_DIR =
env.COMPOSER_CACHE_DIR || upath.join(config.cacheDir, './others/composer');
await fs.ensureDir(env.COMPOSER_CACHE_DIR);
logger.debug('Using composer cache ' + env.COMPOSER_CACHE_DIR);

const cacheDir =
process.env.COMPOSER_CACHE_DIR ||
upath.join(config.cacheDir, './others/composer');
await fs.ensureDir(cacheDir);
logger.debug(`Using composer cache ${cacheDir}`);

const lockFileName = packageFileName.replace(/\.json$/, '.lock');
const existingLockFileContent = await platform.getFile(lockFileName);
if (!existingLockFileContent) {
Expand Down Expand Up @@ -99,28 +100,23 @@ export async function updateArtifacts({
const localAuthFileName = upath.join(cwd, 'auth.json');
await fs.outputFile(localAuthFileName, JSON.stringify(authJson));
}
let cmd: string;
const cmd = 'composer';
const execOptions: ExecOptions = {
cwd,
extraEnv: {
COMPOSER_CACHE_DIR: cacheDir,
},
};
if (config.binarySource === BINARY_SOURCE_DOCKER) {
logger.info('Running composer via docker');
cmd = `docker run --rm `;
if (config.dockerUser) {
cmd += `--user=${config.dockerUser} `;
}
const volumes = [config.localDir, env.COMPOSER_CACHE_DIR];
cmd += volumes.map(v => `-v "${v}":"${v}" `).join('');
const envVars = ['COMPOSER_CACHE_DIR'];
cmd += envVars.map(e => `-e ${e} `);
cmd += `-w "${cwd}" `;
cmd += `renovate/composer composer`;
execOptions.docker = { image: 'renovate/composer' };
} else if (
config.binarySource === BINARY_SOURCE_AUTO ||
config.binarySource === BINARY_SOURCE_GLOBAL
) {
logger.info('Running composer via global composer');
cmd = 'composer';
} else {
logger.warn({ config }, 'Unsupported binarySource');
cmd = 'composer';
}
let args;
if (config.isLockFileMaintenance) {
Expand All @@ -134,10 +130,7 @@ export async function updateArtifacts({
args += ' --no-scripts --no-autoloader';
}
logger.debug({ cmd, args }, 'composer command');
await exec(`${cmd} ${args}`, {
cwd,
env,
});
await exec(`${cmd} ${args}`, execOptions);
const status = await platform.getRepoStatus();
if (!status.modified.includes(lockFileName)) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion test/manager/composer/__snapshots__/artifacts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ 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/composer\\":\\"/tmp/renovate/cache/others/composer\\" -e COMPOSER_CACHE_DIR -w \\"/tmp/github/some/repo\\" renovate/composer composer update --with-dependencies --ignore-platform-reqs --no-ansi --no-interaction --no-scripts --no-autoloader",
"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/composer\\":\\"/tmp/renovate/cache/others/composer\\" -e COMPOSER_CACHE_DIR -w \\"/tmp/github/some/repo\\" renovate/composer bash -l -c \\"composer update --with-dependencies --ignore-platform-reqs --no-ansi --no-interaction --no-scripts --no-autoloader\\"",
"options": Object {
"cwd": "/tmp/github/some/repo",
"encoding": "utf-8",
Expand Down
10 changes: 7 additions & 3 deletions test/manager/composer/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 composer from '../../../lib/manager/composer/artifacts';
Expand All @@ -6,6 +7,7 @@ import { mocked } from '../../util';
import { StatusResult } from '../../../lib/platform/git/storage';
import { envMock, mockExecAll } from '../../execUtil';
import * as _env from '../../../lib/util/exec/env';
import { setDockerConfig } from '../../../lib/util/exec/docker';
import {
BINARY_SOURCE_DOCKER,
BINARY_SOURCE_GLOBAL,
Expand All @@ -24,15 +26,17 @@ 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'),
};

describe('.updateArtifacts()', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
env.getChildProcessEnv.mockReturnValue(envMock.basic);
setDockerConfig({});
});
it('returns if no composer.lock found', async () => {
expect(
Expand Down Expand Up @@ -120,6 +124,7 @@ describe('.updateArtifacts()', () => {
expect(execSnapshots).toMatchSnapshot();
});
it('supports docker mode', async () => {
setDockerConfig({ ...config, dockerUser: 'foobar' });
platform.getFile.mockResolvedValueOnce('Current composer.lock');

const execSnapshots = mockExecAll(exec);
Expand All @@ -133,7 +138,6 @@ describe('.updateArtifacts()', () => {
config: {
...config,
binarySource: BINARY_SOURCE_DOCKER,
dockerUser: 'foobar',
},
})
).not.toBeNull();
Expand Down

0 comments on commit fd64216

Please sign in to comment.