Exemplo n.º 1
0
 /**
  *
  * @param  HostDetails $hostDetails
  * @param  SupportedHost $host
  * @return string
  */
 public function determineIpAddress($hostDetails, SupportedHost $host)
 {
     // what are we doing?
     $log = usingLog()->startAction("query " . basename(__CLASS__) . " for IP address");
     if (empty($hostDetails->ifaces)) {
         // set default network interfaces
         $hostDetails->ifaces = array('eth1', 'eth0');
     }
     // how do we do this?
     foreach ($hostDetails->ifaces as $iface) {
         $command = "/sbin/ip addr show {$iface}";
         $result = $host->runCommandViaHostManager($hostDetails, $command);
         // NOTE: the above command will return the exit code 0 even if the interface is not found
         if ($result->didCommandFail() || strpos($result->output, 'error fetching') !== false) {
             // no interface found
             //
             // move on to the next interface to check
             continue;
         }
         // reduce the output down to an IP address
         $lines = explode("\n", $result->output);
         $lines = FilterForMatchingString::against($lines, 'inet ');
         $lines = FilterColumns::from($lines, '1', ' ');
         // do we have an IP address?
         if (!isset($lines[0]) || empty(trim($lines[0]))) {
             // no, we do not
             continue;
         }
         // if we get here, then we have an IP address/netmask combo
         $parts = explode("/", $lines[0]);
         $ipAddress = trim($parts[0]);
         $log->endAction("IP address is '{$ipAddress}'");
         return $ipAddress;
     }
     // if we get here, we do not know what the IP address is
     $msg = "could not determine IP address";
     $log->endAction($msg);
     throw new E5xx_ActionFailed(__METHOD__, $msg);
 }
Exemplo n.º 2
0
 /**
  *
  * @param  HostDetails $hostDetails
  * @param  string $packageName
  * @return BaseObject
  */
 public function getInstalledPackageDetails($hostDetails, $packageName)
 {
     // what are we doing?
     $log = usingLog()->startAction("get details for package '{$packageName}' installed in host '{$hostDetails->hostId}'");
     // get the details
     $command = "sudo yum list installed {$packageName}";
     $result = $this->runCommand($hostDetails, $command);
     // any luck?
     if ($result->didCommandFail()) {
         $log->endAction("could not get details ... package not installed?");
         return new BaseObject();
     }
     // study the output
     $parts = explode("\n", $result->output);
     $parts = FilterForMatchingString::against($parts, $packageName);
     $parts = FilterColumns::from($parts, "0-2", ' ');
     if (!isset($parts[0])) {
         $log->endAction("could not get details ... package not installed?");
         return new BaseObject();
     }
     $parts = explode(' ', $result->output);
     if (count($parts) < 3) {
         $log->endAction("could not get details ... package not installed?");
         return new BaseObject();
     }
     if (strtolower($parts[0]) == 'error:') {
         $log->endAction("could not get details ... package not installed?");
         return new BaseObject();
     }
     // we have some information to return
     $return = new BaseObject();
     $return->name = $parts[0];
     $return->version = $parts[1];
     $return->repo = $parts[2];
     // all done
     $log->endAction();
     return $return;
 }
 /**
  * extract a named field from the output of /usr/bin/sw_vers
  *
  * @param  array $lines
  *         the output of /usr/bin/sw_vers
  * @param  string $fieldName
  *         the field that we are looking for
  * @return string|null
  *         the value of the field (if found)
  */
 private static function extractField($lines, $fieldName)
 {
     $matches = FilterForMatchingString::against($lines, $fieldName);
     if (empty($matches)) {
         return null;
     }
     return TrimWhitespace::from(FilterColumns::from($matches[0], '1', ':'));
 }
 /**
  * @covers ::againstString
  * @covers ::matchLine
  * @dataProvider provideStringsToMatch
  */
 public function testCanStaticallyFilterStrings($data, $searchString, $expectedResult)
 {
     // ----------------------------------------------------------------
     // setup your test
     // ----------------------------------------------------------------
     // perform the change
     $actualResult = FilterForMatchingString::againstString($data, $searchString);
     // ----------------------------------------------------------------
     // test the results
     $this->assertEquals($expectedResult, $actualResult);
 }
Exemplo n.º 5
0
 /**
  *
  * @param  HostDetails $hostDetails
  * @param  string $packageName
  * @return BaseObject
  */
 public function getInstalledPackageDetails($hostDetails, $packageName)
 {
     // what are we doing?
     $log = usingLog()->startAction("get details for package '{$packageName}' installed in host '{$hostDetails->hostId}'");
     // get the details
     $command = 'dpkg-query -W --showformat=\'\\${Package} \\${Version}\\t\\${Status}\\n\' ' . $packageName;
     $result = $this->runCommand($hostDetails, $command);
     // any luck?
     if ($result->didCommandFail()) {
         $log->endAction("could not get details ... package not installed?");
         return new BaseObject();
     }
     // study the output
     $lines = explode("\n", $result->output);
     $lines = FilterForMatchingRegex::against($lines, "|^{$packageName} |");
     $lines = FilterForMatchingString::against($lines, "install ok installed");
     if (empty($lines)) {
         $log->endAction("package not installed?");
         return new BaseObject();
     }
     // we have some information to return
     $return = new BaseObject();
     $return->name = FilterColumns::fromString($lines[0], "0", " ");
     $return->version = FilterColumns::fromString($lines[0], "1", "\t");
     $return->repo = "unknown";
     // all done
     $log->endAction();
     return $return;
 }
Exemplo n.º 6
0
 /**
  *
  * @param  VagrantVmDetails $vmDetails
  * @return boolean
  */
 public function isRunning($vmDetails)
 {
     // what are we doing?
     $log = usingLog()->startAction("determine status of Vagrant VM '{$vmDetails->hostId}'");
     // if the box is running, it should have a status of 'running'
     $command = "vagrant status";
     $result = $this->runCommandAgainstHostManager($vmDetails, $command);
     // reduce the output down to the exact host we are looking for
     $lines = explode("\n", $result->output);
     $lines = FilterForMatchingString::against($lines, $vmDetails->hostId);
     $lines = FilterColumns::from($lines, "1", ' ');
     // what is the status?
     $state = trim($lines[0]);
     if ($state != 'running') {
         $log->endAction("VM is not running; state is '{$state}'");
         return false;
     }
     // all done
     $log->endAction("VM is running");
     return true;
 }