1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 00:20:08 +01:00

run commands as sudo

This commit is contained in:
Taylor Otwell
2016-05-11 19:40:44 -05:00
parent e0003c6176
commit fd595b5c71
7 changed files with 55 additions and 28 deletions

View File

@@ -88,9 +88,9 @@ function installCaddyDaemon()
*/
function restart()
{
$this->cli->quietly('launchctl unload '.$this->daemonPath);
$this->cli->quietly('sudo launchctl unload '.$this->daemonPath);
$this->cli->quietly('launchctl load '.$this->daemonPath);
$this->cli->quietly('sudo launchctl load '.$this->daemonPath);
}
/**
@@ -100,7 +100,7 @@ function restart()
*/
function stop()
{
$this->cli->quietly('launchctl unload '.$this->daemonPath);
$this->cli->quietly('sudo launchctl unload '.$this->daemonPath);
}
/**

View File

@@ -8,10 +8,24 @@ class CommandLine
{
/**
* Simple global function to run commands.
*
* @param string $command
* @return void
*/
public function quietly($command)
{
(new Process($command))->run();
$this->runCommand($command.' > /dev/null 2>&1');
}
/**
* Simple global function to run commands.
*
* @param string $command
* @return void
*/
public function quietlyAsUser($command)
{
$this->quietly('sudo -u '.user().' '.$command.' > /dev/null 2>&1');
}
/**
@@ -26,7 +40,7 @@ public function passthru($command)
}
/**
* Run the given command.
* Run the given command as the non-root user.
*
* @param string $command
* @param callable $onError
@@ -34,17 +48,29 @@ public function passthru($command)
*/
public function run($command, callable $onError = null)
{
return $this->runAsRoot('sudo -u '.$_SERVER['SUDO_USER'].' '.$command, $onError);
return $this->runCommand($command, $onError);
}
/**
* Run the given command as root.
* Run the given command.
*
* @param string $command
* @param callable $onError
* @return string
*/
public function runAsRoot($command, callable $onError = null)
public function runAsUser($command, callable $onError = null)
{
return $this->runCommand('sudo -u '.user().' '.$command, $onError);
}
/**
* Run the given command.
*
* @param string $command
* @param callable $onError
* @return string
*/
protected function runCommand($command, callable $onError = null)
{
$onError = $onError ?: function () {};

View File

@@ -128,7 +128,7 @@ function createDomainResolver($domain)
*/
function updateDomain($oldDomain, $newDomain)
{
$this->cli->quietly('rm '.$this->resolverPath.'/'.$oldDomain);
$this->files->unlink($this->resolverPath.'/'.$oldDomain);
$this->install($newDomain);
}

View File

@@ -223,7 +223,9 @@ function symlink($target, $link, $owner = null)
*/
function unlink($path)
{
@unlink($path);
if ($this->exists($path)) {
@unlink($path);
}
}
/**

View File

@@ -6,17 +6,19 @@
class Site
{
var $config, $files;
var $config, $cli, $files;
/**
* Create a new Site instance.
*
* @param Configuration $config
* @param CommandLine $cli
* @param Filesystem $files
* @return void
*/
function __construct(Configuration $config, Filesystem $files)
function __construct(Configuration $config, CommandLine $cli, Filesystem $files)
{
$this->cli = $cli;
$this->files = $files;
$this->config = $config;
}
@@ -36,7 +38,7 @@ function link($target, $link)
$this->config->prependPath($linkPath);
$this->files->symlink($target, $linkPath.'/'.$link);
$this->cli->runAsUser('ln -s '.$target.' '.$linkPath.'/'.$link);
return $linkPath.'/'.$link;
}
@@ -81,7 +83,7 @@ function logs($paths)
$logPath = $path.'/'.$directory.'/storage/logs/laravel.log';
if ($this->files->isDir(dirname($logPath))) {
return $this->files->touch($logPath);
return $this->files->touchAsUser($logPath);
}
})->filter());
}

View File

@@ -28,9 +28,9 @@ function __construct(CommandLine $cli, Filesystem $files)
*/
function symlinkToUsersBin()
{
$this->cli->quietly('rm '.$this->valetBin);
$this->cli->quietlyAsUser('rm '.$this->valetBin);
$this->cli->run('ln -s '.realpath(__DIR__.'/../../valet').' '.$this->valetBin);
$this->cli->runAsUser('ln -s '.realpath(__DIR__.'/../../valet').' '.$this->valetBin);
}
/**

21
valet
View File

@@ -23,23 +23,19 @@ fi
# If the command is one of the commands that requires "sudo" privileges
# then we'll proxy the incoming CLI request using sudo, which allows
# us to never require the end users to manually specify each sudo.
if [[ "$1" = "install" ]] || [[ "$1" = "domain" ]] || \
[[ "$1" = "start" ]] || [[ "$1" = "restart" ]] || \
[[ "$1" = "stop" ]] || [[ "$1" = "uninstall" ]]
if [[ "$EUID" -ne 0 ]]
then
if [[ "$EUID" -ne 0 ]]
then
sudo $SOURCE "$@"
else
php "$DIR/cli/valet.php" "$@"
fi
sudo $SOURCE "$@"
exit
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.
elif [[ "$1" = "update" ]]
if [[ "$1" = "update" ]]
then
bash $DIR/cli/scripts/update.sh
exit
# 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
@@ -58,11 +54,12 @@ then
# Fetch Ngrok URL In Background...
bash "$DIR/cli/scripts/fetch-share-url.sh" &
"$DIR/bin/ngrok" http -host-header=rewrite "$HOST.$DOMAIN:80"
sudo -u $(logname) "$DIR/bin/ngrok" http -host-header=rewrite "$HOST.$DOMAIN:80"
exit
# Finally, for every other command we will just proxy into the PHP tool
# and let it handle the request. These are commands which can be run
# without sudo and don't require taking over terminals like Ngrok.
else
php "$DIR/cli/valet.php" $@
php "$DIR/cli/valet.php" "$@"
fi