Skip to content

Commit

Permalink
refactor: change approach of adding hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Sep 26, 2019
1 parent fdce244 commit 6d1f0a0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exports[`createComposeObjFromRunners should create composeObj from all initializ
Object {
"services": Object {
"general": Object {
"extra_hosts": Array [],
"image": "general/image:123",
"ports": Array [],
},
Expand All @@ -20,7 +19,6 @@ Object {
"KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR": 1,
"KAFKA_ZOOKEEPER_CONNECT": "zookeeper:2181",
},
"extra_hosts": Array [],
"image": "kafka/image:123",
"ports": Array [
Object {
Expand All @@ -35,7 +33,6 @@ Object {
"POSTGRES_PASSWORD": "_",
"POSTGRES_USER": "_",
},
"extra_hosts": Array [],
"image": "postgres/image:123",
"ports": Array [
Object {
Expand All @@ -45,7 +42,6 @@ Object {
],
},
"redis": Object {
"extra_hosts": Array [],
"image": "redis/image:123",
"ports": Array [
Object {
Expand All @@ -58,7 +54,6 @@ Object {
"environment": Object {
"ZOOKEEPER_CLIENT_PORT": 2181,
},
"extra_hosts": Array [],
"image": "zookeeper/image:123",
"ports": Array [
Object {
Expand All @@ -81,7 +76,6 @@ Object {
"POSTGRES_PASSWORD": "_",
"POSTGRES_USER": "_",
},
"extra_hosts": Array [],
"image": "postgres/image:123",
"ports": Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import execa from 'execa'
import { ComposeService, DependsOn, ComposeFile } from '../../runners/@types'
import { DockestConfig } from '../../index'

Expand All @@ -19,16 +18,6 @@ export default (config: DockestConfig, dockerComposeFileVersion: string) => {
services: {},
}

const extra_hosts: string[] = []

if (!config.$.isInsideDockerContainer) {
if (process.platform === 'linux') {
const command = `ip -4 addr show docker0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
const result = execa.sync(command, { reject: false })
extra_hosts.push(`host.docker.internal:${result.stdout}`)
}
}

config.runners.forEach(runner => {
const {
runnerConfig: { service, dependsOn },
Expand All @@ -40,7 +29,7 @@ export default (config: DockestConfig, dockerComposeFileVersion: string) => {

composeObj.services = {
...composeObj.services,
[service]: { ...composeService, extra_hosts },
[service]: { ...composeService },
...depComposeServices,
}
})
Expand Down
10 changes: 10 additions & 0 deletions src/onRun/waitForRunnersReadiness/fixRunnerHostAccessOnLinux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Runner } from '../../runners/@types'
import { execa } from '../../index'

export default async (runner: Runner) => {
const command = ` \
docker exec ${runner.containerId} \
/bin/sh -c "ip -4 route list match 0/0 | awk '{print \\$3\\" host.docker.internal\\"}' >> /etc/hosts"
`
await execa(command)
}
4 changes: 4 additions & 0 deletions src/onRun/waitForRunnersReadiness/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _resolveContainerId from './resolveContainerId'
import _checkConnection from './checkConnection'
import _checkResponsiveness from './checkResponsiveness'
import _runRunnerCommands from './runRunnerCommands'
import _fixRunnerHostAccessOnLinux from './fixRunnerHostAccessOnLinux'
import testUtils from '../../testUtils'

const {
Expand All @@ -15,11 +16,13 @@ const checkConnection = _checkConnection as jest.Mock
const checkResponsiveness = _checkResponsiveness as jest.Mock
const resolveContainerId = _resolveContainerId as jest.Mock
const runRunnerCommands = _runRunnerCommands as jest.Mock
const fixRunnerHostAccessOnLinux = _fixRunnerHostAccessOnLinux as jest.Mock

jest.mock('./resolveContainerId')
jest.mock('./checkConnection')
jest.mock('./checkResponsiveness')
jest.mock('./runRunnerCommands')
jest.mock('./fixRunnerHostAccessOnLinux')

describe('waitForRunnersReadiness', () => {
const runners = [generalPurposeRunner]
Expand All @@ -29,6 +32,7 @@ describe('waitForRunnersReadiness', () => {
checkConnection.mockClear()
checkResponsiveness.mockClear()
runRunnerCommands.mockClear()
fixRunnerHostAccessOnLinux.mockClear()
})

it('should work for a simple one-runner application', async () => {
Expand Down
6 changes: 6 additions & 0 deletions src/onRun/waitForRunnersReadiness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import checkConnection from './checkConnection'
import checkResponsiveness from './checkResponsiveness'
import resolveContainerId from './resolveContainerId'
import runRunnerCommands from './runRunnerCommands'
import fixRunnerHostAccessOnLinux from './fixRunnerHostAccessOnLinux'
import { Runner } from '../../runners/@types'
import { DockestConfig } from '../../index'
import joinBridgeNetwork from '../joinBridgeNetwork'
Expand All @@ -13,6 +14,11 @@ const setupRunner = async (runner: Runner, initializer: string) => {
if (runner.isBridgeNetworkMode) {
await joinBridgeNetwork(runner.containerId, runner.runnerConfig.service)
}

if (process.platform === 'linux' && !runner.isBridgeNetworkMode) {
await fixRunnerHostAccessOnLinux(runner)
}

await checkConnection(runner)
await checkResponsiveness(runner)
await runRunnerCommands(runner)
Expand Down

0 comments on commit 6d1f0a0

Please sign in to comment.