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

Commit 788fdf6

Browse files
committed
Merge branch 'development'
2 parents 7492d52 + 9c0d12c commit 788fdf6

File tree

10 files changed

+526
-256
lines changed

10 files changed

+526
-256
lines changed

cli/Valet/DnsMasq.php

Lines changed: 46 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ class DnsMasq
1919
/**
2020
* Create a new DnsMasq instance.
2121
*
22-
* @param PackageManager $pm
23-
* @param ServiceManager $sm
24-
* @param Filesystem $files
22+
* @param PackageManager $pm PackageManager object
23+
* @param ServiceManager $sm ServiceManager object
24+
* @param Filesystem $files Filesystem object
25+
* @param CommandLine $cli CommandLine object
26+
*
2527
* @return void
2628
*/
2729
public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $files, CommandLine $cli)
@@ -42,91 +44,57 @@ public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $
4244
/**
4345
* Install and configure DnsMasq.
4446
*
47+
* @param bool $lock Lock or Unlock the file
48+
*
4549
* @return void
4650
*/
47-
private function lockResolvConf($lock = true)
51+
private function _lockResolvConf($lock = true)
4852
{
4953
$arg = $lock ? '+i' : '-i';
5054

5155
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;
56+
$this->cli->run(
57+
"chattr {$arg} {$this->resolvconf}",
58+
function ($code, $msg) {
59+
warning($msg);
60+
}
61+
);
8162
}
82-
83-
return false;
8463
}
8564

8665
/**
8766
* Enable nameserver merging
8867
*
8968
* @return void
9069
*/
91-
private function mergeDns()
70+
private function _mergeDns()
9271
{
9372
$optDir = '/opt/valet-linux';
94-
$script = $optDir.'/get-dns-servers';
95-
$output = [];
73+
$script = $optDir.'/valet-dns';
9674

9775
$this->pm->ensureInstalled('inotify-tools');
76+
$this->files->remove($optDir);
9877
$this->files->ensureDirExists($optDir);
99-
$this->files->put($script, $this->files->get(__DIR__.'/../stubs/get-dns-servers'));
100-
$this->cli->run("chmod +x {$script}");
78+
$this->files->put($script, $this->files->get(__DIR__.'/../stubs/valet-dns'));
79+
$this->cli->run("chmod +x $script");
80+
$this->sm->installValetDns($this->files);
10181

102-
if (! $this->files->exists($this->rclocal)) {
103-
$this->files->put($this->rclocal, implode("\n", ['exit 0', '']));
104-
}
105-
106-
$rclocal = $this->files->get($this->rclocal);
107-
108-
if (strpos($rclocal, $script) === false) {
109-
$this->files->backup($this->rclocal);
110-
111-
foreach( explode("\n", $rclocal) as $line) {
112-
if ($line == 'exit 0') {
113-
$output[] = $script;
114-
$output[] = '';
115-
}
116-
117-
$output[] = $line;
118-
}
119-
120-
$this->files->put($this->rclocal, implode("\n", $output));
121-
$this->cli->run("chmod +x {$this->rclocal}");
82+
if ($this->files->exists($this->rclocal)) {
83+
$this->files->restore($this->rclocal);
12284
}
85+
86+
$this->files->backup($this->resolvconf);
87+
$this->files->unlink($this->resolvconf);
88+
$this->files->symlink($script, $this->resolvconf);
12389

12490
return true;
12591
}
12692

12793
/**
12894
* Install and configure DnsMasq.
12995
*
96+
* @param string $domain Domain TLD to use
97+
*
13098
* @return void
13199
*/
132100
public function install($domain = 'test')
@@ -136,12 +104,14 @@ public function install($domain = 'test')
136104
$this->createCustomConfigFile($domain);
137105
$this->pm->nmRestart($this->sm);
138106
$this->sm->restart('dnsmasq');
107+
$this->sm->start('valet-dns');
139108
}
140109

141110
/**
142111
* Append the custom DnsMasq configuration file to the main configuration file.
143112
*
144-
* @param string $domain
113+
* @param string $domain Domain TLD to use
114+
*
145115
* @return void
146116
*/
147117
public function createCustomConfigFile($domain)
@@ -156,17 +126,19 @@ public function createCustomConfigFile($domain)
156126
*/
157127
public function fixResolved()
158128
{
159-
$resolved = $this->resolvedConfigPath;
129+
// $resolved = $this->resolvedConfigPath;
160130

161-
$this->files->backup($resolved);
162-
$this->files->putAsUser($resolved, $this->files->get(__DIR__.'/../stubs/resolved.conf'));
131+
// $this->files->backup($resolved);
132+
// $this->files->putAsUser($resolved, $this->files->get(__DIR__.'/../stubs/resolved.conf'));
163133

164134
$this->sm->disable('systemd-resolved');
165135
$this->sm->stop('systemd-resolved');
166136
}
167137

168138
/**
169139
* Setup dnsmasq with Network Manager.
140+
*
141+
* @return void
170142
*/
171143
public function dnsmasqSetup()
172144
{
@@ -178,28 +150,23 @@ public function dnsmasqSetup()
178150

179151
$this->files->uncommentLine('IGNORE_RESOLVCONF', '/etc/default/dnsmasq');
180152

181-
$this->mergeDns();
182-
183-
$this->lockResolvConf(false);
153+
$this->_lockResolvConf(false);
154+
$this->_mergeDns();
184155

185156
$this->files->unlink('/etc/dnsmasq.d/network-manager');
186-
$this->files->backup($this->resolvconf);
187-
$this->files->unlink($this->resolvconf);
188157
$this->files->backup($this->dnsmasqconf);
189158

190-
$this->files->putAsUser($this->resolvconf, 'nameserver 127.0.0.1'.PHP_EOL);
191159
$this->files->putAsUser($this->dnsmasqconf, $this->files->get(__DIR__.'/../stubs/dnsmasq.conf'));
192160
$this->files->putAsUser($this->dnsmasqOpts, $this->files->get(__DIR__.'/../stubs/dnsmasq_options'));
193161
$this->files->putAsUser($this->nmConfigPath, $this->files->get(__DIR__.'/../stubs/networkmanager.conf'));
194-
195-
$this->lockResolvConf();
196-
$this->dnsWatch('restart');
197162
}
198163

199164
/**
200165
* Update the domain used by DnsMasq.
201166
*
202-
* @param string $newDomain
167+
* @param string $oldDomain Old TLD
168+
* @param string $newDomain New TLD
169+
*
203170
* @return void
204171
*/
205172
public function updateDomain($oldDomain, $newDomain)
@@ -215,13 +182,16 @@ public function updateDomain($oldDomain, $newDomain)
215182
*/
216183
public function uninstall()
217184
{
218-
$this->dnsWatch('stop');
185+
$this->sm->stop('valet-dns');
186+
$this->sm->disable('valet-dns');
187+
219188
$this->cli->passthru('rm -rf /opt/valet-linux');
220189
$this->files->unlink($this->configPath);
221190
$this->files->unlink($this->dnsmasqOpts);
222191
$this->files->unlink($this->nmConfigPath);
223192
$this->files->restore($this->resolvedConfigPath);
224-
$this->lockResolvConf(false);
193+
194+
$this->_lockResolvConf(false);
225195
$this->files->restore($this->rclocal);
226196
$this->files->restore($this->resolvconf);
227197
$this->files->restore($this->dnsmasqconf);

0 commit comments

Comments
 (0)