DnsDock::configureVirtualMachine()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace Dock\Installer\DNS\Mac;
4
5
use Dock\Docker\Machine\SshClient;
6
use Dock\Docker\Machine\Machine;
7
use Dock\Docker\Machine\SshFileManipulator;
8
use Dock\Installer\InstallerTask;
9
use Dock\IO\ProcessRunner;
10
use Dock\IO\UserInteraction;
11
use Dock\System\Bash\BashFileManipulator;
12
use SRIO\ChainOfResponsibility\DependentChainProcessInterface;
13
14
class DnsDock extends InstallerTask implements DependentChainProcessInterface
15
{
16
    /**
17
     * @var ProcessRunner
18
     */
19
    private $processRunner;
20
    /**
21
     * @var UserInteraction
22
     */
23
    private $userInteraction;
24
    /**
25
     * @var SshClient
26
     */
27
    private $sshClient;
28
    /**
29
     * @var Machine
30
     */
31
    private $machine;
32
33
    /**
34
     * @param SshClient       $sshClient
35
     * @param UserInteraction $userInteraction
36
     * @param ProcessRunner   $processRunner
37
     * @param Machine         $machine
38
     */
39
    public function __construct(SshClient $sshClient, UserInteraction $userInteraction, ProcessRunner $processRunner, Machine $machine)
40
    {
41
        $this->sshClient = $sshClient;
42
        $this->userInteraction = $userInteraction;
43
        $this->processRunner = $processRunner;
44
        $this->machine = $machine;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function run()
51
    {
52
        $this->userInteraction->writeTitle('Configure DNS resolution for Docker containers');
53
54
        $needMachineRestart = $this->configureVirtualMachine();
55
        $this->configureHostMachineResolution();
56
57
        if ($needMachineRestart) {
58
            $this->restartMachine();
59
        }
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    private function dnsDockerIsInStartupConfiguration()
66
    {
67
        return $this->sshClient->runAndCheckOutputWasGenerated('grep dnsdock /var/lib/boot2docker/bootlocal.sh');
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    private function hasDockerExtraArgs()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
74
    {
75
        return $this->sshClient->runAndCheckOutputWasGenerated('grep EXTRA_ARGS /var/lib/boot2docker/profile');
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function dependsOn()
82
    {
83
        return ['machine', 'dnsdock_image'];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getName()
90
    {
91
        return 'dns';
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    private function configureVirtualMachine()
98
    {
99
        $needMachineRestart = false;
100
101
        $daemonArguments = '-H unix:///var/run/docker.sock --bip=172.17.42.1/16 --dns=172.17.42.1';
102
        $bashFileManipulator = new BashFileManipulator(
103
            new SshFileManipulator($this->sshClient, '/var/lib/boot2docker/profile')
104
        );
105
106
        $extraArguments = $bashFileManipulator->getValue('EXTRA_ARGS');
107
108
        if (strpos($extraArguments, $daemonArguments) === false) {
109
            $bashFileManipulator->replaceValue('EXTRA_ARGS', $extraArguments.' '.$daemonArguments);
110
111
            $needMachineRestart = true;
112
        }
113
114
        if (!$this->dnsDockerIsInStartupConfiguration()) {
115
            $bootScript = 'sleep 5'.PHP_EOL.
116
                'docker start dnsdock || docker run -d -v /var/run/docker.sock:/var/run/docker.sock --name dnsdock -p 172.17.42.1:53:53/udp aacebedo/dnsdock:latest-amd64'.PHP_EOL;
117
118
            $this->sshClient->run('echo "'.$bootScript.'" | sudo tee -a /var/lib/boot2docker/bootlocal.sh');
119
            $needMachineRestart = true;
120
        }
121
122
        return $needMachineRestart;
123
    }
124
125
    private function configureHostMachineResolution()
126
    {
127
        $this->processRunner->run('sudo mkdir -p /etc/resolver');
128
        $this->processRunner->run('echo "nameserver 172.17.42.1" | sudo tee /etc/resolver/docker');
129
    }
130
131
    private function restartMachine()
132
    {
133
        if ($this->machine->isRunning()) {
134
            $this->machine->stop();
135
        }
136
137
        $this->machine->start();
138
    }
139
}
140