Skip to content

Commit

Permalink
v2.0.1-alpha merge (#40)
Browse files Browse the repository at this point in the history
* refactor to 7.2, typed params
* fix phpdoc
* add feature completeness for ansible 2
* remove travis support for older versions
* remove hhvm completely
  • Loading branch information
maschmann authored Dec 17, 2018
1 parent ce8f56e commit 386909e
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Ansible/Command/AbstractAnsibleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ protected function checkParam($param, string $glue = ' '): string
/**
* Creates process with processBuilder builder and executes it.
*
* @param null $callback
* @param callable|null $callback
* @return int|string
*/
protected function runProcess($callback)
protected function runProcess($callback = null)
{
$process = $this->processBuilder
->setArguments(
Expand Down
2 changes: 1 addition & 1 deletion Ansible/Command/AnsibleCommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface AnsibleCommandInterface
* Executes a command process.
* Returns either exitcode or string output if no callback is given.
*
* @param null $callback
* @param callable|null $callback
* @return integer|string
*/
public function execute($callback = null);
Expand Down
2 changes: 1 addition & 1 deletion Ansible/Command/AnsibleGalaxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class AnsibleGalaxy extends AbstractAnsibleCommand implements AnsibleGalax
* Executes a command process.
* Returns either exitcode or string output if no callback is given.
*
* @param null $callback
* @param callable|null $callback
* @return integer|string
*/
public function execute($callback = null)
Expand Down
78 changes: 40 additions & 38 deletions Ansible/Command/AnsiblePlaybook.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class AnsiblePlaybook extends AbstractAnsibleCommand implements AnsiblePla
* Executes a command process.
* Returns either exitcode or string output if no callback is given.
*
* @param null $callback
* @param callable|null $callback
* @return integer|string
*/
public function execute($callback = null)
Expand Down Expand Up @@ -98,6 +98,32 @@ public function askVaultPass(): AnsiblePlaybookInterface
return $this;
}

/**
* Enable privilege escalation
*
* @return AnsiblePlaybookInterface
* @see http://docs.ansible.com/ansible/become.html
*/
public function become(): AnsiblePlaybookInterface
{
$this->addParameter('--become');

return $this;
}

/**
* Desired sudo user (default=root).
*
* @param string $user
* @return AnsiblePlaybookInterface
*/
public function becomeUser(string $user = 'root'): AnsiblePlaybookInterface
{
$this->addOption('--become-user', $user);

return $this;
}

/**
* Don't make any changes; instead, try to predict some of the changes that may occur.
*
Expand Down Expand Up @@ -274,7 +300,7 @@ public function noCows(): AnsiblePlaybookInterface
*/
public function colors(bool $colors = true): AnsiblePlaybookInterface
{
$this->processBuilder->setEnv('ANSIBLE_FORCE_COLOR', $colors);
$this->processBuilder->setEnv('ANSIBLE_FORCE_COLOR', intval($colors));
}

/**
Expand All @@ -299,7 +325,6 @@ public function privateKey(string $file): AnsiblePlaybookInterface
public function skipTags($tags = ''): AnsiblePlaybookInterface
{
$tags = $this->checkParam($tags, ',');

$this->addOption('--skip-tags', $tags);

return $this;
Expand Down Expand Up @@ -355,32 +380,6 @@ public function suUser(string $user = 'root'): AnsiblePlaybookInterface
return $this;
}

/**
* Enable privilege escalation
*
* @return AnsiblePlaybookInterface
* @see http://docs.ansible.com/ansible/become.html
*/
public function become(): AnsiblePlaybookInterface
{
$this->addParameter('--become');

return $this;
}

/**
* Desired sudo user (default=root).
*
* @param string $user
* @return AnsiblePlaybookInterface
*/
public function becomeUser(string $user = 'root'): AnsiblePlaybookInterface
{
$this->addOption('--become-user', $user);

return $this;
}

/**
* Perform a syntax check on the playbook, but do not execute it.
*
Expand All @@ -402,7 +401,6 @@ public function syntaxCheck(): AnsiblePlaybookInterface
public function tags($tags): AnsiblePlaybookInterface
{
$tags = $this->checkParam($tags, ',');

$this->addOption('--tags', $tags);

return $this;
Expand Down Expand Up @@ -513,11 +511,12 @@ public function newVaultPasswordFile(string $passwordFile): AnsiblePlaybookInter
/**
* specify extra arguments to pass to scp only (e.g. -l)
*
* @param string $scpExtraArgs
* @param string|array $scpExtraArgs
* @return AnsiblePlaybookInterface
*/
public function scpExtraArgs(string $scpExtraArgs): AnsiblePlaybookInterface
public function scpExtraArgs($scpExtraArgs): AnsiblePlaybookInterface
{
$scpExtraArgs = $this->checkParam($scpExtraArgs, ',');
$this->addOption('--scp-extra-args', $scpExtraArgs);

return $this;
Expand All @@ -526,11 +525,12 @@ public function scpExtraArgs(string $scpExtraArgs): AnsiblePlaybookInterface
/**
* specify extra arguments to pass to sftp only (e.g. -f, -l)
*
* @param string $sftpExtraArgs
* @param string|array $sftpExtraArgs
* @return AnsiblePlaybookInterface
*/
public function sftpExtraArgs(string $sftpExtraArgs): AnsiblePlaybookInterface
public function sftpExtraArgs($sftpExtraArgs): AnsiblePlaybookInterface
{
$sftpExtraArgs = $this->checkParam($sftpExtraArgs, ',');
$this->addOption('--sftp-extra-args', $sftpExtraArgs);

return $this;
Expand All @@ -539,11 +539,12 @@ public function sftpExtraArgs(string $sftpExtraArgs): AnsiblePlaybookInterface
/**
* specify common arguments to pass to sftp/scp/ssh (e.g. ProxyCommand)
*
* @param string $sshArgs
* @param string|array $sshArgs
* @return AnsiblePlaybookInterface
*/
public function sshCommonArgs(string $sshArgs): AnsiblePlaybookInterface
public function sshCommonArgs($sshArgs): AnsiblePlaybookInterface
{
$sshArgs = $this->checkParam($sshArgs, ',');
$this->addOption('--ssh-common-args', $sshArgs);

return $this;
Expand All @@ -552,11 +553,12 @@ public function sshCommonArgs(string $sshArgs): AnsiblePlaybookInterface
/**
* specify extra arguments to pass to ssh only (e.g. -R)
*
* @param string $extraArgs
* @param string|array $extraArgs
* @return AnsiblePlaybookInterface
*/
public function sshExtraArgs(string $extraArgs): AnsiblePlaybookInterface
public function sshExtraArgs($extraArgs): AnsiblePlaybookInterface
{
$extraArgs = $this->checkParam($extraArgs, ',');
$this->addOption('--ssh-extra-args', $extraArgs);

return $this;
Expand Down
16 changes: 8 additions & 8 deletions Ansible/Command/AnsiblePlaybookInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,34 +248,34 @@ public function suUser(string $user = 'root'): AnsiblePlaybookInterface;
/**
* specify extra arguments to pass to scp only (e.g. -l)
*
* @param string $scpExtraArgs
* @param string|array $scpExtraArgs
* @return AnsiblePlaybookInterface
*/
public function scpExtraArgs(string $scpExtraArgs): AnsiblePlaybookInterface;
public function scpExtraArgs($scpExtraArgs): AnsiblePlaybookInterface;

/**
* specify extra arguments to pass to sftp only (e.g. -f, -l)
*
* @param string $sftpExtraArgs
* @param string|array $sftpExtraArgs
* @return AnsiblePlaybookInterface
*/
public function sftpExtraArgs(string $sftpExtraArgs): AnsiblePlaybookInterface;
public function sftpExtraArgs($sftpExtraArgs): AnsiblePlaybookInterface;

/**
* specify common arguments to pass to sftp/scp/ssh (e.g. ProxyCommand)
*
* @param string $sshArgs
* @param string|array $sshArgs
* @return AnsiblePlaybookInterface
*/
public function sshCommonArgs(string $sshArgs): AnsiblePlaybookInterface;
public function sshCommonArgs($sshArgs): AnsiblePlaybookInterface;

/**
* specify extra arguments to pass to ssh only (e.g. -R)
*
* @param string $extraArgs
* @param string|array $extraArgs
* @return AnsiblePlaybookInterface
*/
public function sshExtraArgs(string $extraArgs): AnsiblePlaybookInterface;
public function sshExtraArgs($extraArgs): AnsiblePlaybookInterface;

/**
* Perform a syntax check on the playbook, but do not execute it.
Expand Down
25 changes: 24 additions & 1 deletion Ansible/Process/ProcessBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class ProcessBuilder implements ProcessBuilderInterface
private $path;

/**
* @var array
*/
private $envVars;

/**
* ProcessBuilder constructor.
*
* @param string $prefix
Expand All @@ -47,6 +54,7 @@ public function __construct(string $prefix, string $path)
$this->arguments = [$prefix];
$this->path = $path;
$this->timeout = 900;
$this->envVars = [];
}

/**
Expand All @@ -72,6 +80,19 @@ public function setTimeout(int $timeout): ProcessBuilderInterface
}

/**
* @param string $name name of ENV VAR
* @param string $value
* @return ProcessBuilderInterface
*/
public function setEnv(string $name, string $value): ProcessBuilderInterface
{
$this->envVars[$name] = $value;

return $this;
}

/**
* @return Process
*/
public function getProcess(): Process
Expand All @@ -81,6 +102,8 @@ public function getProcess(): Process
$this->arguments,
$this->path
)
)->setTimeout($this->timeout);
)
->setTimeout($this->timeout)
->setEnv($this->envVars);
}
}
7 changes: 7 additions & 0 deletions Ansible/Process/ProcessBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function setArguments(array $arguments): ProcessBuilderInterface;
*/
public function setTimeout(int $timeout): ProcessBuilderInterface;

/**
* @param string $name name of ENV VAR
* @param string $value
* @return ProcessBuilderInterface
*/
public function setEnv(string $name, string $value): ProcessBuilderInterface;

/**
* @return Process
*/
Expand Down
3 changes: 3 additions & 0 deletions Tests/Ansible/Command/AnsibleGalaxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function testCreateInstance()
public function testExecute(AnsibleGalaxyInterface $command)
{
$command->execute();

// if command executes without exception
$this->assertTrue(true);
}

public function testInit()
Expand Down
4 changes: 2 additions & 2 deletions Tests/Ansible/Command/AnsiblePlaybookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,9 @@ public function testExecuteWithCallback(AnsiblePlaybookInterface $command)
$exitcode = $command
->execute(function ($type, $buffer) {
if (Process::ERR === $type) {
echo 'ERR > '.$buffer;
$out = 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
$out = 'OUT > '.$buffer;
}
});

Expand Down
1 change: 1 addition & 0 deletions Tests/Ansible/Process/ProcessBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testGetProcess(ProcessBuilderInterface $processBuilder)
{
$process = $processBuilder
->setArguments([])
->setEnv('SOME', 'value')
->setTimeout(5)
->getProcess();

Expand Down

0 comments on commit 386909e

Please sign in to comment.