Skip to content
This repository was archived by the owner on Dec 9, 2019. It is now read-only.

Commit 87a1b1a

Browse files
authored
[skip ci] Completely refactor the DNSMasq configuration (cpriego#100)
* Change DNSMasq handle * Bump version * Fix dns resolution for older ubuntu versions * Improve disable and enable methods * Improve install and uninstall process * Update ngrok version * Add subdomain support * Add subdomain support in secured sites * Get all nameservers dynamically
1 parent b346124 commit 87a1b1a

27 files changed

+1120
-143
lines changed

.travis.yml

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,66 +22,67 @@ matrix:
2222
# phpunit: false
2323
# distro: arch
2424
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
25-
- php: 7.0
26-
env:
27-
phpunit: false
28-
distro: centos7
29-
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
30-
- php: 7.0
31-
env:
32-
phpunit: false
33-
distro: fedora24
34-
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
35-
- php: 7.0
36-
env:
37-
phpunit: false
38-
distro: fedora25
39-
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
40-
- php: 7.0
41-
env:
42-
phpunit: false
43-
distro: ubuntu1604
44-
run_opts: ""
45-
- php: 7.0
46-
env:
47-
phpunit: false
48-
distro: ubuntu1404
49-
run_opts: ""
50-
allow_failures:
51-
- php: 7.1
52-
env:
53-
phpunit: false
54-
distro: arch
55-
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
56-
- php: 7.0
57-
env:
58-
phpunit: false
59-
distro: centos7
60-
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
61-
- php: 7.0
62-
env:
63-
phpunit: false
64-
distro: fedora24
65-
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
66-
- php: 7.0
67-
env:
68-
phpunit: false
69-
distro: fedora25
70-
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
71-
- php: 7.0
72-
env:
73-
phpunit: false
74-
distro: ubuntu1604
75-
run_opts: ""
76-
- php: 7.0
77-
env:
78-
phpunit: false
79-
distro: ubuntu1404
80-
run_opts: ""
25+
# - php: 7.0
26+
# env:
27+
# phpunit: false
28+
# distro: centos7
29+
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
30+
# - php: 7.0
31+
# env:
32+
# phpunit: false
33+
# distro: fedora24
34+
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
35+
# - php: 7.0
36+
# env:
37+
# phpunit: false
38+
# distro: fedora25
39+
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
40+
# - php: 7.0
41+
# env:
42+
# phpunit: false
43+
# distro: ubuntu1604
44+
# run_opts: ""
45+
# - php: 7.0
46+
# env:
47+
# phpunit: false
48+
# distro: ubuntu1404
49+
# run_opts: ""
50+
# allow_failures:
51+
# - php: 7.1
52+
# env:
53+
# phpunit: false
54+
# distro: arch
55+
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
56+
# - php: 7.0
57+
# env:
58+
# phpunit: false
59+
# distro: centos7
60+
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
61+
# - php: 7.0
62+
# env:
63+
# phpunit: false
64+
# distro: fedora24
65+
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
66+
# - php: 7.0
67+
# env:
68+
# phpunit: false
69+
# distro: fedora25
70+
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
71+
# - php: 7.0
72+
# env:
73+
# phpunit: false
74+
# distro: ubuntu1604
75+
# run_opts: ""
76+
# - php: 7.0
77+
# env:
78+
# phpunit: false
79+
# distro: ubuntu1404
80+
# run_opts: ""
8181

8282
before_install:
8383
- travis_retry composer self-update
84-
- if [[ -n $distro ]]; then travis_retry docker pull thenodi/docker-${distro}-valet:latest; fi
84+
# - git clone --depth=50 https://github.com/TheNodi/docker-valet-test-images.git $HOME/vdock
85+
# - if [[ -n $distro ]]; then travis_retry docker build -t thenodi/docker-${distro}-valet:latest $HOME/vdock/$distro; fi
8586

8687
cache:
8788
directories:
@@ -96,19 +97,19 @@ script:
9697
- if [[ $phpunit = true ]]; then vendor/bin/phpunit; fi
9798

9899
# Start the container in detached mode
99-
- >
100-
if [[ -n $distro ]]; then
101-
CONTAINER_ID=$( \
102-
docker run --detach \
103-
--volume="${PWD}":/workspace \
104-
-e "REPOSITORY=/workspace" \
105-
${run_opts} \
106-
thenodi/docker-${distro}-valet:latest
107-
);
108-
fi
100+
# - >
101+
# if [[ -n $distro ]]; then
102+
# CONTAINER_ID=$( \
103+
# docker run --detach \
104+
# --volume="${PWD}":/workspace \
105+
# -e "REPOSITORY=/workspace" \
106+
# ${run_opts} \
107+
# thenodi/docker-${distro}-valet:latest
108+
# );
109+
# fi
109110

110111
# Prepare the container
111-
- if [[ -n $distro ]]; then docker exec -it $CONTAINER_ID /prepare.sh; fi
112+
# - if [[ -n $distro ]]; then docker exec -it $CONTAINER_ID /prepare.sh; fi
112113

113114
# Run functional tests
114-
- if [[ -n $distro ]]; then docker exec -it -u valet $CONTAINER_ID /workspace/tests/Functional/run.sh; fi
115+
# - if [[ -n $distro ]]; then docker exec -it -u valet $CONTAINER_ID /workspace/tests/Functional/run.sh; fi

bin/ngrok

190 KB
Binary file not shown.

cli/Valet/Contracts/PackageManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function setup();
3838
/**
3939
* Restart dnsmasq in distro.
4040
*/
41-
public function dnsmasqRestart($sm);
41+
public function nmRestart($sm);
4242

4343
/**
4444
* Determine if package manager is available on the system.

cli/Valet/DnsMasq.php

Lines changed: 130 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class DnsMasq
1111
public $sm;
1212
public $cli;
1313
public $files;
14+
public $rclocal;
1415
public $configPath;
1516
public $nmConfigPath;
1617
public $resolvedConfig;
@@ -25,11 +26,15 @@ class DnsMasq
2526
*/
2627
public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $files, CommandLine $cli)
2728
{
28-
$this->pm = $pm;
29-
$this->sm = $sm;
30-
$this->cli = $cli;
29+
$this->pm = $pm;
30+
$this->sm = $sm;
31+
$this->cli = $cli;
3132
$this->files = $files;
32-
$this->configPath = '/etc/NetworkManager/dnsmasq.d/valet';
33+
$this->rclocal = '/etc/rc.local';
34+
$this->resolvconf = '/etc/resolv.conf';
35+
$this->dnsmasqconf = '/etc/dnsmasq.conf';
36+
$this->configPath = '/etc/dnsmasq.d/valet';
37+
$this->dnsmasqOpts = '/etc/dnsmasq.d/options';
3338
$this->nmConfigPath = '/etc/NetworkManager/conf.d/valet.conf';
3439
$this->resolvedConfigPath = '/etc/systemd/resolved.conf';
3540
}
@@ -39,12 +44,94 @@ public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $
3944
*
4045
* @return void
4146
*/
42-
public function install()
47+
private function lockResolvConf($lock = true)
48+
{
49+
$arg = $lock ? '+i' : '-i';
50+
51+
if (! $this->files->isLink($this->resolvconf)) {
52+
$this->cli->run("chattr {$arg} {$this->resolvconf}", function ($code, $msg) {
53+
warning($msg);
54+
});
55+
}
56+
}
57+
58+
/**
59+
* Control dns watcher.
60+
*
61+
* @param string $action start|stop|restart
62+
* @return boolean
63+
*/
64+
private function dnsWatch($action = 'start')
65+
{
66+
if ($action === 'start') {
67+
$this->cli->quietly('/opt/valet-linux/get-dns-servers');
68+
return true;
69+
}
70+
71+
if ($action === 'stop') {
72+
$this->cli->passthru('pkill -f "inotifywait -q -m -e modify --format"');
73+
$this->cli->passthru('pkill -f "bash .*/get-dns-servers"');
74+
return true;
75+
}
76+
77+
if ($action === 'restart') {
78+
$this->dnsWatch('stop');
79+
$this->dnsWatch('start');
80+
return true;
81+
}
82+
83+
return false;
84+
}
85+
86+
/**
87+
* Enable nameserver merging
88+
*
89+
* @return void
90+
*/
91+
private function mergeDns()
92+
{
93+
$optDir = '/opt/valet-linux';
94+
$script = $optDir.'/get-dns-servers';
95+
$rclocal = $this->files->get($this->rclocal);
96+
$output = [];
97+
98+
$this->pm->ensureInstalled('inotify-tools');
99+
$this->files->ensureDirExists($optDir);
100+
$this->files->put($script, $this->files->get(__DIR__.'/../stubs/get-dns-servers'));
101+
$this->cli->run("chmod +x {$script}");
102+
103+
if (strpos($rclocal, $script) === false) {
104+
$this->files->backup($this->rclocal);
105+
106+
foreach( explode("\n", $rclocal) as $line) {
107+
if ($line == 'exit 0') {
108+
$output[] = $script;
109+
$output[] = '';
110+
}
111+
112+
$output[] = $line;
113+
}
114+
115+
$this->files->put($this->rclocal, implode("\n", $output));
116+
$this->cli->run("chmod +x {$this->rclocal}");
117+
$this->dnsWatch('restart');
118+
}
119+
120+
return true;
121+
}
122+
123+
/**
124+
* Install and configure DnsMasq.
125+
*
126+
* @return void
127+
*/
128+
public function install($domain = 'dev')
43129
{
44130
$this->dnsmasqSetup();
45131
$this->fixResolved();
46-
$this->createCustomConfigFile('dev');
47-
$this->pm->dnsmasqRestart($this->sm);
132+
$this->createCustomConfigFile($domain);
133+
$this->pm->nmRestart($this->sm);
134+
$this->sm->restart('dnsmasq');
48135
}
49136

50137
/**
@@ -69,6 +156,9 @@ public function fixResolved()
69156

70157
$this->files->backup($resolved);
71158
$this->files->putAsUser($resolved, $this->files->get(__DIR__.'/../stubs/resolved.conf'));
159+
160+
$this->sm->disable('systemd-resolved');
161+
$this->sm->stop('systemd-resolved');
72162
}
73163

74164
/**
@@ -77,9 +167,28 @@ public function fixResolved()
77167
public function dnsmasqSetup()
78168
{
79169
$this->pm->ensureInstalled('dnsmasq');
170+
$this->sm->enable('dnsmasq');
171+
80172
$this->files->ensureDirExists('/etc/NetworkManager/conf.d');
173+
$this->files->ensureDirExists('/etc/dnsmasq.d');
174+
175+
$this->files->uncommentLine('IGNORE_RESOLVCONF', '/etc/default/dnsmasq');
176+
177+
$this->mergeDns();
178+
179+
$this->lockResolvConf(false);
81180

181+
$this->files->unlink('/etc/dnsmasq.d/network-manager');
182+
$this->files->backup($this->resolvconf);
183+
$this->files->unlink($this->resolvconf);
184+
$this->files->backup($this->dnsmasqconf);
185+
186+
$this->files->putAsUser($this->resolvconf, 'nameserver 127.0.0.1'.PHP_EOL);
187+
$this->files->putAsUser($this->dnsmasqconf, $this->files->get(__DIR__.'/../stubs/dnsmasq.conf'));
188+
$this->files->putAsUser($this->dnsmasqOpts, $this->files->get(__DIR__.'/../stubs/dnsmasq_options'));
82189
$this->files->putAsUser($this->nmConfigPath, $this->files->get(__DIR__.'/../stubs/networkmanager.conf'));
190+
191+
$this->lockResolvConf();
83192
}
84193

85194
/**
@@ -90,9 +199,8 @@ public function dnsmasqSetup()
90199
*/
91200
public function updateDomain($oldDomain, $newDomain)
92201
{
93-
$this->fixResolved();
94202
$this->createCustomConfigFile($newDomain);
95-
$this->pm->dnsmasqRestart($this->sm);
203+
$this->sm->restart('dnsmasq');
96204
}
97205

98206
/**
@@ -102,10 +210,22 @@ public function updateDomain($oldDomain, $newDomain)
102210
*/
103211
public function uninstall()
104212
{
213+
$this->dnsWatch('stop');
214+
$this->cli->passthru('rm -rf /opt/valet-linux');
105215
$this->files->unlink($this->configPath);
216+
$this->files->unlink($this->dnsmasqOpts);
106217
$this->files->unlink($this->nmConfigPath);
107218
$this->files->restore($this->resolvedConfigPath);
219+
$this->lockResolvConf(false);
220+
$this->files->restore($this->rclocal);
221+
$this->files->restore($this->resolvconf);
222+
$this->files->restore($this->dnsmasqconf);
223+
$this->files->commentLine('IGNORE_RESOLVCONF', '/etc/default/dnsmasq');
224+
225+
$this->pm->nmRestart($this->sm);
226+
$this->sm->restart('dnsmasq');
108227

109-
$this->pm->dnsmasqRestart($this->sm);
228+
info('Valet DNS changes have been rolled back');
229+
warning('If your system depended on systemd-resolved (like Ubuntu 17.04), please enable it manually');
110230
}
111231
}

0 commit comments

Comments
 (0)