Skip to content

Commit

Permalink
[skip ci] Completely refactor the DNSMasq configuration (cpriego#100)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cpriego committed Sep 21, 2017
1 parent b346124 commit 87a1b1a
Show file tree
Hide file tree
Showing 27 changed files with 1,120 additions and 143 deletions.
139 changes: 70 additions & 69 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,67 @@ matrix:
# phpunit: false
# distro: arch
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: centos7
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: fedora24
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: fedora25
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: ubuntu1604
run_opts: ""
- php: 7.0
env:
phpunit: false
distro: ubuntu1404
run_opts: ""
allow_failures:
- php: 7.1
env:
phpunit: false
distro: arch
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: centos7
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: fedora24
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: fedora25
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
- php: 7.0
env:
phpunit: false
distro: ubuntu1604
run_opts: ""
- php: 7.0
env:
phpunit: false
distro: ubuntu1404
run_opts: ""
# - php: 7.0
# env:
# phpunit: false
# distro: centos7
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - php: 7.0
# env:
# phpunit: false
# distro: fedora24
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - php: 7.0
# env:
# phpunit: false
# distro: fedora25
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - php: 7.0
# env:
# phpunit: false
# distro: ubuntu1604
# run_opts: ""
# - php: 7.0
# env:
# phpunit: false
# distro: ubuntu1404
# run_opts: ""
# allow_failures:
# - php: 7.1
# env:
# phpunit: false
# distro: arch
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - php: 7.0
# env:
# phpunit: false
# distro: centos7
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - php: 7.0
# env:
# phpunit: false
# distro: fedora24
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - php: 7.0
# env:
# phpunit: false
# distro: fedora25
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - php: 7.0
# env:
# phpunit: false
# distro: ubuntu1604
# run_opts: ""
# - php: 7.0
# env:
# phpunit: false
# distro: ubuntu1404
# run_opts: ""

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

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

# Start the container in detached mode
- >
if [[ -n $distro ]]; then
CONTAINER_ID=$( \
docker run --detach \
--volume="${PWD}":/workspace \
-e "REPOSITORY=/workspace" \
${run_opts} \
thenodi/docker-${distro}-valet:latest
);
fi
# - >
# if [[ -n $distro ]]; then
# CONTAINER_ID=$( \
# docker run --detach \
# --volume="${PWD}":/workspace \
# -e "REPOSITORY=/workspace" \
# ${run_opts} \
# thenodi/docker-${distro}-valet:latest
# );
# fi

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

# Run functional tests
- if [[ -n $distro ]]; then docker exec -it -u valet $CONTAINER_ID /workspace/tests/Functional/run.sh; fi
# - if [[ -n $distro ]]; then docker exec -it -u valet $CONTAINER_ID /workspace/tests/Functional/run.sh; fi
Binary file modified bin/ngrok
Binary file not shown.
2 changes: 1 addition & 1 deletion cli/Valet/Contracts/PackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function setup();
/**
* Restart dnsmasq in distro.
*/
public function dnsmasqRestart($sm);
public function nmRestart($sm);

/**
* Determine if package manager is available on the system.
Expand Down
140 changes: 130 additions & 10 deletions cli/Valet/DnsMasq.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DnsMasq
public $sm;
public $cli;
public $files;
public $rclocal;
public $configPath;
public $nmConfigPath;
public $resolvedConfig;
Expand All @@ -25,11 +26,15 @@ class DnsMasq
*/
public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $files, CommandLine $cli)
{
$this->pm = $pm;
$this->sm = $sm;
$this->cli = $cli;
$this->pm = $pm;
$this->sm = $sm;
$this->cli = $cli;
$this->files = $files;
$this->configPath = '/etc/NetworkManager/dnsmasq.d/valet';
$this->rclocal = '/etc/rc.local';
$this->resolvconf = '/etc/resolv.conf';
$this->dnsmasqconf = '/etc/dnsmasq.conf';
$this->configPath = '/etc/dnsmasq.d/valet';
$this->dnsmasqOpts = '/etc/dnsmasq.d/options';
$this->nmConfigPath = '/etc/NetworkManager/conf.d/valet.conf';
$this->resolvedConfigPath = '/etc/systemd/resolved.conf';
}
Expand All @@ -39,12 +44,94 @@ public function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $
*
* @return void
*/
public function install()
private function lockResolvConf($lock = true)
{
$arg = $lock ? '+i' : '-i';

if (! $this->files->isLink($this->resolvconf)) {
$this->cli->run("chattr {$arg} {$this->resolvconf}", function ($code, $msg) {
warning($msg);
});
}
}

/**
* Control dns watcher.
*
* @param string $action start|stop|restart
* @return boolean
*/
private function dnsWatch($action = 'start')
{
if ($action === 'start') {
$this->cli->quietly('/opt/valet-linux/get-dns-servers');
return true;
}

if ($action === 'stop') {
$this->cli->passthru('pkill -f "inotifywait -q -m -e modify --format"');
$this->cli->passthru('pkill -f "bash .*/get-dns-servers"');
return true;
}

if ($action === 'restart') {
$this->dnsWatch('stop');
$this->dnsWatch('start');
return true;
}

return false;
}

/**
* Enable nameserver merging
*
* @return void
*/
private function mergeDns()
{
$optDir = '/opt/valet-linux';
$script = $optDir.'/get-dns-servers';
$rclocal = $this->files->get($this->rclocal);
$output = [];

$this->pm->ensureInstalled('inotify-tools');
$this->files->ensureDirExists($optDir);
$this->files->put($script, $this->files->get(__DIR__.'/../stubs/get-dns-servers'));
$this->cli->run("chmod +x {$script}");

if (strpos($rclocal, $script) === false) {
$this->files->backup($this->rclocal);

foreach( explode("\n", $rclocal) as $line) {
if ($line == 'exit 0') {
$output[] = $script;
$output[] = '';
}

$output[] = $line;
}

$this->files->put($this->rclocal, implode("\n", $output));
$this->cli->run("chmod +x {$this->rclocal}");
$this->dnsWatch('restart');
}

return true;
}

/**
* Install and configure DnsMasq.
*
* @return void
*/
public function install($domain = 'dev')
{
$this->dnsmasqSetup();
$this->fixResolved();
$this->createCustomConfigFile('dev');
$this->pm->dnsmasqRestart($this->sm);
$this->createCustomConfigFile($domain);
$this->pm->nmRestart($this->sm);
$this->sm->restart('dnsmasq');
}

/**
Expand All @@ -69,6 +156,9 @@ public function fixResolved()

$this->files->backup($resolved);
$this->files->putAsUser($resolved, $this->files->get(__DIR__.'/../stubs/resolved.conf'));

$this->sm->disable('systemd-resolved');
$this->sm->stop('systemd-resolved');
}

/**
Expand All @@ -77,9 +167,28 @@ public function fixResolved()
public function dnsmasqSetup()
{
$this->pm->ensureInstalled('dnsmasq');
$this->sm->enable('dnsmasq');

$this->files->ensureDirExists('/etc/NetworkManager/conf.d');
$this->files->ensureDirExists('/etc/dnsmasq.d');

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

$this->mergeDns();

$this->lockResolvConf(false);

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

$this->files->putAsUser($this->resolvconf, 'nameserver 127.0.0.1'.PHP_EOL);
$this->files->putAsUser($this->dnsmasqconf, $this->files->get(__DIR__.'/../stubs/dnsmasq.conf'));
$this->files->putAsUser($this->dnsmasqOpts, $this->files->get(__DIR__.'/../stubs/dnsmasq_options'));
$this->files->putAsUser($this->nmConfigPath, $this->files->get(__DIR__.'/../stubs/networkmanager.conf'));

$this->lockResolvConf();
}

/**
Expand All @@ -90,9 +199,8 @@ public function dnsmasqSetup()
*/
public function updateDomain($oldDomain, $newDomain)
{
$this->fixResolved();
$this->createCustomConfigFile($newDomain);
$this->pm->dnsmasqRestart($this->sm);
$this->sm->restart('dnsmasq');
}

/**
Expand All @@ -102,10 +210,22 @@ public function updateDomain($oldDomain, $newDomain)
*/
public function uninstall()
{
$this->dnsWatch('stop');
$this->cli->passthru('rm -rf /opt/valet-linux');
$this->files->unlink($this->configPath);
$this->files->unlink($this->dnsmasqOpts);
$this->files->unlink($this->nmConfigPath);
$this->files->restore($this->resolvedConfigPath);
$this->lockResolvConf(false);
$this->files->restore($this->rclocal);
$this->files->restore($this->resolvconf);
$this->files->restore($this->dnsmasqconf);
$this->files->commentLine('IGNORE_RESOLVCONF', '/etc/default/dnsmasq');

$this->pm->nmRestart($this->sm);
$this->sm->restart('dnsmasq');

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

0 comments on commit 87a1b1a

Please sign in to comment.