Skip to content

Commit

Permalink
Merge pull request #61 from cpriego/dnsmasq
Browse files Browse the repository at this point in the history
Fix various errors, fixes #55
  • Loading branch information
cpriego authored May 1, 2017
2 parents 53f7fec + 4f8d0bc commit 74cfb2d
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 61 deletions.
22 changes: 11 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ matrix:
env: phpunit=true
- php: 7.1
env: phpunit=true
- php: 7.1
env:
phpunit: false
distro: arch
run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"
# - 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
Expand All @@ -47,12 +47,12 @@ matrix:
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'"
# allow_failures:
# - php: 7.1
# env:
# phpunit: false
# distro: arch
# run_opts: "'--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro'"

before_install:
- travis_retry composer self-update
Expand Down
7 changes: 3 additions & 4 deletions cli/Valet/DnsMasq.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function __construct(PackageManager $pm, ServiceManager $sm, Filesystem $files,
function install()
{
$this->dnsmasqSetup();
$this->sm->disableServices();
$this->createCustomConfigFile('dev');
$this->pm->dnsmasqRestart($this->sm);
}
Expand All @@ -58,10 +59,8 @@ function dnsmasqSetup()
$this->pm->ensureInstalled('dnsmasq');
$this->files->ensureDirExists('/etc/NetworkManager/conf.d');

$this->files->putAsUser(
$this->nmConfigPath,
$this->files->get(__DIR__.'/../stubs/networkmanager.conf')
);
$this->files->putAsUser($this->nmConfigPath, $this->files->get(__DIR__.'/../stubs/networkmanager.conf'));
$this->files->putAsUser('/etc/NetworkManager/dnsmasq.d/dnsmasq.conf', 'listen-address=127.0.0.1'.PHP_EOL);
}

/**
Expand Down
1 change: 1 addition & 0 deletions cli/Valet/Nginx.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function __construct(PackageManager $pm, ServiceManager $sm, CommandLine $cli, F
function install()
{
$this->pm->ensureInstalled('nginx');
$this->sm->enable('nginx');
$this->files->ensureDirExists('/etc/nginx/sites-available');
$this->files->ensureDirExists('/etc/nginx/sites-enabled');

Expand Down
2 changes: 1 addition & 1 deletion cli/Valet/PackageManagers/Apt.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function setup()
*/
function dnsmasqRestart($sm)
{
$sm->restart('network-manager');
$sm->restart(['network-manager']);
}

/**
Expand Down
1 change: 1 addition & 0 deletions cli/Valet/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function install()
{
if (! $this->pm->installed("php{$this->version}-fpm")) {
$this->pm->ensureInstalled("php{$this->version}-fpm");
$this->sm->enable($this->fpmServiceName());
}

$this->files->ensureDirExists('/var/log', user());
Expand Down
22 changes: 22 additions & 0 deletions cli/Valet/ServiceManagers/LinuxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ public function status($service)
return $this->cli->run('service '.$this->getRealService($service).' status');
}

/**
* Disable services.
*
* @param
* @return void
*/
public function disableServices()
{
return;
}

/**
* Enable services.
*
* @param
* @return void
*/
public function enable($services)
{
return;
}

/**
* Determine if service manager is available on the system.
*
Expand Down
34 changes: 34 additions & 0 deletions cli/Valet/ServiceManagers/Systemd.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ public function status($service)
return $this->cli->run('systemctl status '.$this->getRealService($service));
}

/**
* Disable services.
*
* @param
* @return void
*/
public function disableServices()
{
$services = ['dnsmasq','systemd-resolved'];

foreach ($services as $service) {
info("Disabling {$service}.service ...");
$this->cli->quietly('sudo systemctl disable ' . $this->getRealService($service));
}

$this->stop($services);
}

/**
* Enable services.
*
* @param
* @return void
*/
public function enable($services)
{
$services = is_array($services) ? $services : func_get_args();

foreach ($services as $service) {
info("Enabling {$service}.service ...");
$this->cli->quietly('sudo systemctl enable ' . $this->getRealService($service));
}
}

/**
* Determine if service manager is available on the system.
*
Expand Down
4 changes: 2 additions & 2 deletions cli/Valet/Valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function onLatestVersion($currentVersion)
{
$response = \Httpful\Request::get($this->github)->send();

return version_compare($currentVersion, trim($response->body->tag_name, 'v'), '>=');
return version_compare($currentVersion, trim($response->body->tag_name), '>=');
}

/**
Expand Down Expand Up @@ -151,8 +151,8 @@ function serviceManagerSetup()
function getAvailableServiceManager()
{
return collect([
LinuxService::class,
Systemd::class,
LinuxService::class,
])->first(function ($pm) {
return resolve($pm)->isAvailable();
}, function () {
Expand Down
51 changes: 45 additions & 6 deletions cli/scripts/update.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
#!/usr/bin/env bash

# Determine if the port config key exists, if not, create it
CONFIG="$HOME/.valet/config.json"
PORT=$(jq -r ".port" "$CONFIG")
function fix-config() {
local CONFIG="$HOME/.valet/config.json"

if [[ "$PORT" = "null" ]]
if [[ -f $CONFIG ]]
then
local PORT=$(jq -r ".port" "$CONFIG")

if [[ "$PORT" = "null" ]]
then
echo "Fixing valet config file..."
CONTENTS=$(jq '. + {port: "80"}' "$CONFIG")
echo -n $CONTENTS >| "$CONFIG"
fi
fi
}

function cleanup {
local NM="/etc/NetworkManager"
local TMP="/tmp/nm.conf"

if [[ -f "$NM"/dnsmasq.d/valet ]]
then
echo "Removing old dnsmasq config file..."
sudo rm "$NM"/dnsmasq.d/valet
fi

if [[ -f "$NM"/conf.d/valet.conf ]]
then
echo "Removing old NetworkManager config file..."
sudo rm "$NM"/conf.d/valet.conf
fi

if grep -xq "dns=dnsmasq" "$NM/NetworkManager.conf"
then
echo "Removing dnsmasq control from NetworkManager..."
sudo grep -v "dns=dnsmasq" "$NM/NetworkManager.conf" > "$TMP" && sudo mv "$TMP" "$NM/NetworkManager.conf"
fi

echo "Cleanup done."
}

if [[ "$1" = "update" ]]
then
CONTENTS=$(jq '. + {port: "80"}' "$CONFIG")
echo -n $CONTENTS >| "$CONFIG"
exit
composer global update "cpriego/valet-linux"
fi

fix-config
# cleanup
20 changes: 15 additions & 5 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
Container::setInstance(new Container);

$version = '2.0.14';
$version = 'v2.0.15';

$app = new Application('Valet', $version);

Expand All @@ -31,6 +31,8 @@
* Allow Valet to be run more conveniently by allowing the Node proxy to run password-less sudo.
*/
$app->command('install [--ignore-selinux]', function ($ignoreSELinux) {
passthru(dirname(__FILE__).'/scripts/update.sh'); // Clean up cruft

Requirements::setIgnoreSELinux($ignoreSELinux)->check();
Configuration::install();
Nginx::install();
Expand Down Expand Up @@ -268,13 +270,21 @@
/**
* Determine if this is the latest release of Valet.
*/
$app->command('on-latest-version', function () use ($version) {
$app->command('update', function () use ($version) {
$script = dirname(__FILE__).'/scripts/update.sh';

if (Valet::onLatestVersion($version)) {
output('YES');
info('You have the latest version of Valet Linux');
passthru($script);
} else {
output('NO');
warning('There is a new release of Valet Linux');
warning('Updating now...');
passthru($script.' update');
}
})->descriptions('Determine if this is the latest version of Valet');
DnsMasq::restart();
PhpFpm::restart();
Nginx::restart();
})->descriptions('Update Valet Linux and clean up cruft');
}

/**
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
"phpunit/phpunit": "~5.5"
},
"scripts": {
"post-update-cmd": "cli/scripts/update.sh"
"post-install-cmd": [
"chmod +x cli/scripts/update.sh && bash cli/scripts/update.sh"
],
"post-update-cmd": [
"chmod +x cli/scripts/update.sh && bash cli/scripts/update.sh"
]
},
"bin": [
"valet"
Expand Down
24 changes: 2 additions & 22 deletions tests/Integration/DnsMasqTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,10 @@ public function test_install_calls_the_right_methods_and_restarts()
$dnsMasq->shouldReceive('dnsmasqSetup')->once();
$dnsMasq->shouldReceive('createCustomConfigFile')->once()->with('dev');
$pm->shouldReceive('dnsmasqRestart')->once()->with($sm);
$sm->shouldReceive('disableServices')->once();
$dnsMasq->install();
}

public function test_dnsmasqSetup_correctly_installs_and_configures_dnsmasq_control_to_networkmanager()
{
$pm = Mockery::mock(PackageManager::class);
$pm->shouldReceive('ensureInstalled')->once()->with('dnsmasq');
$sm = Mockery::mock(ServiceManager::class);
$files = resolve(StubForFiles::class);

swap(PackageManager::class, $pm);
swap(ServiceManager::class, $sm);
swap(Filesystem::class, $files);

$dnsMasq = resolve(DnsMasq::class);
$dnsMasq->nmConfigPath = __DIR__ . '/output/valet.conf';

$dnsMasq->dnsmasqSetup();

$this->assertSame('[main]
dns=dnsmasq
', file_get_contents(__DIR__ . '/output/valet.conf'));
}

public function test_createCustomConfigFile_correctly_creates_valet_dns_config_file()
{
$pm = Mockery::mock(PackageManager::class);
Expand Down Expand Up @@ -100,4 +80,4 @@ function ensureDirExists($path, $owner = null, $mode = 0755)
{
return;
}
}
}
1 change: 1 addition & 0 deletions tests/Integration/NginxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function test_install_calls_the_right_methods()
$pm->shouldReceive('ensureInstalled')->with('nginx')->once();

$sm = Mockery::mock(ServiceManager::class);
$sm->shouldReceive('enable')->with('nginx')->once();
$sm->shouldReceive('stop')->with('nginx')->once();

$nginx = Mockery::mock(Nginx::class.'[installConfiguration,installServer,installNginxDirectory]', [$pm, $sm, $cli, $files, $conf, $site]);
Expand Down
10 changes: 1 addition & 9 deletions valet
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,10 @@ then
fi
fi

# If the command is to run the updater we'll run the updater script and
# let it handle this entire update. It will download a fresh copy of
# Valet and replace the current install with the "fresh" download.
if [[ "$1" = "update" ]]
then
bash $DIR/cli/scripts/update.sh
exit 0

# If the command is the "share" command we will need to resolve out any
# symbolic links for the site. Before starting Ngrok, we will fire a
# process to retrieve the live Ngrok tunnel URL in the background.
elif [[ "$1" = "share" ]]
if [[ "$1" = "share" ]]
then
HOST="${PWD##*/}"
DOMAIN=$(php "$DIR/cli/valet.php" domain)
Expand Down

0 comments on commit 74cfb2d

Please sign in to comment.