Completed
Push — master ( 95a54c...7178fa )
by Samuel
10:58
created

DnsDock::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Dock\Installer\DNS\Linux;
4
5
use Dock\Installer\InstallerTask;
6
use Dock\IO\ProcessRunner;
7
use Dock\IO\UserInteraction;
8
use SRIO\ChainOfResponsibility\DependentChainProcessInterface;
9
10
class DnsDock extends InstallerTask implements DependentChainProcessInterface
11
{
12
    const IP = '172.17.42.1';
13
14
    /**
15
     * @var ProcessRunner
16
     */
17
    private $processRunner;
18
    /**
19
     * @var UserInteraction
20
     */
21
    private $userInteraction;
22
23
    /**
24
     * @param UserInteraction $userInteraction
25
     * @param ProcessRunner   $processRunner
26
     */
27
    public function __construct(UserInteraction $userInteraction, ProcessRunner $processRunner)
28
    {
29
        $this->userInteraction = $userInteraction;
30
        $this->processRunner = $processRunner;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function dependsOn()
37
    {
38
        return ['docker'];
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getName()
45
    {
46
        return 'dns';
47
    }
48
49
    public function run()
50
    {
51
        if (!$this->hasDockerOptions()) {
52
            $this->userInteraction->writeTitle('Configuring DNS resolution for Docker containers');
53
54
            $this->processRunner->run('echo \'DOCKER_OPTS="--bip='.self::IP.'/24 --dns '.self::IP.'"\' | sudo tee -a /etc/default/docker');
55
            $this->processRunner->run('sudo service docker restart');
56
        }
57
58
        $this->processRunner->run('sudo docker start dnsdock || sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock --name dnsdock -p 172.17.42.1:53:53/udp tonistiigi/dnsdock');
59
    }
60
61
    private function hasDockerOptions()
62
    {
63
        return $this->processRunner->run('grep "^DOCKER_OPTS" /etc/default/docker', false)->isSuccessful();
64
    }
65
}
66