DataSift\Storyplayer\HostLib\PhysicalHosts::createHost PHP Method

createHost() public method

public createHost ( stdClass $groupDef, array $provisioningVars = [] ) : void
$groupDef stdClass
$provisioningVars array
return void
    public function createHost($groupDef, $provisioningVars = array())
    {
        // what are we doing?
        $log = usingLog()->startAction('register physical host(s)');
        // make sure we like the provided details
        if (!isset($groupDef->details)) {
            throw new E5xx_ActionFailed(__METHOD__, "missing groupDef->details");
        }
        if (!isset($groupDef->details->machines)) {
            throw new E5xx_ActionFailed(__METHOD__, "missing groupDef->details->machines");
        }
        if (empty($groupDef->details->machines)) {
            throw new E5xx_ActionFailed(__METHOD__, "groupDef->details->machines cannot be empty");
        }
        foreach ($groupDef->details->machines as $hostId => $machine) {
            // TODO: it would be great to autodetect this one day
            if (!isset($machine->roles)) {
                throw new E5xx_ActionFailed(__METHOD__, "missing groupDef->details->machines['{$hostId}']->roles");
            }
            if (!isset($machine->ipAddress)) {
                throw new E5xx_ActionFailed(__METHOD__, "missing groupDef->details->machines['{$hostId}']->ipAddress");
            }
        }
        // remove any existing hosts table entry
        foreach ($groupDef->details->machines as $hostId => $machine) {
            usingHostsTable()->removeHost($hostId);
            // remove any roles
            usingRolesTable()->removeHostFromAllRoles($hostId);
        }
        // there's nothing to start ... we assume that each host is
        // already up and running
        //
        // if it is not, that is NOT our responsibility
        // store the details
        foreach ($groupDef->details->machines as $hostId => $machine) {
            // we want all the details from the config file
            $vmDetails = clone $machine;
            // this allows the story to perform actions against a single
            // machine if required
            //
            // that said, there isn't much you can do with a PhysicalHost
            $vmDetails->type = 'PhysicalHost';
            // remember the name of this machine
            $vmDetails->hostId = $hostId;
            // mark the box as provisioned
            //
            // this stops Storyplayer thinking that the machine is an
            // invalid host
            $vmDetails->provisioned = true;
            // remember this blackbox
            usingHostsTable()->addHost($vmDetails->hostId, $vmDetails);
            foreach ($vmDetails->roles as $role) {
                usingRolesTable()->addHostToRole($vmDetails, $role);
            }
        }
        // all done
        $log->endAction(count($groupDef->details->machines) . ' machine(s) registered');
    }