/**
  * Handle a deprovisioning request
  *
  * @param DeprovisionJob $command
  *
  * @return bool|mixed
  */
 public function handle(DeprovisionJob $command)
 {
     $_options = $command->getOptions();
     $this->debug('>>> deprovision "' . $command->getInstanceId() . '" request received');
     try {
         //  Find the instance
         $_instance = $this->_findInstance($command->getInstanceId());
     } catch (\Exception $_ex) {
         $this->error('deprovision request failure: instance not found.');
         return false;
     }
     try {
         $_provisioner = Provision::getProvisioner($_instance->guest_location_nbr);
         if (empty($_provisioner)) {
             throw new \RuntimeException('The provisioner of the request is not valid.');
         }
         $_response = $_provisioner->deprovision(new ProvisionServiceRequest($_instance, null, true, false, $_options));
         if (!$_response) {
             throw new ProvisioningException('deprovision failure');
         }
         $this->info('deprovision request complete in ' . number_format($_response->getElapsedTime(), 4) . 's');
         $this->debug('<<< deprovision "' . $command->getInstanceId() . '" request SUCCESS');
         $command->setResult($_response);
         return $_response;
     } catch (\Exception $_ex) {
         $this->error('deprovision "' . $command->getInstanceId() . '" request exception: ' . $_ex->getMessage());
     }
     $this->debug('<<< deprovision "' . $command->getInstanceId() . '" request FAILURE');
     return false;
 }
 /**
  * A basic functional test example.
  *
  * @covers Provision::getFacadeAccessor
  * @covers DreamFactory\Enterprise\Services\Managers\ProvisioningManager::provisioner
  * @covers DreamFactory\Enterprise\Services\Managers\ProvisioningManager::getDefaultProvisioner
  * @covers DreamFactory\Enterprise\Services\Managers\ProvisioningManager::storageProvisioner
  * @covers DreamFactory\Enterprise\Services\Managers\ProvisioningManager::resolve
  * @covers DreamFactory\Enterprise\Services\Managers\ProvisioningManager::resolveStorage
  * @covers DreamFactory\Enterprise\Services\Managers\ProvisioningManager::resolveDatabase
  * @covers DreamFactory\Enterprise\Services\Managers\ProvisioningManager::_buildTag
  *
  * @return void
  */
 public function testFacade()
 {
     $_provisioner = Provision::getProvisioner();
     $this->assertTrue($_provisioner instanceof ResourceProvisioner);
     $_provisioner = Provision::getStorageProvisioner();
     $this->assertTrue($_provisioner instanceof ResourceProvisioner);
     $_provisioner = Provision::getProvisioner('dreamfactory');
     $this->assertTrue($_provisioner instanceof InstanceProvisioner);
     $_provisioner = Provision::getStorageProvisioner('dreamfactory');
     $this->assertTrue($_provisioner instanceof StorageProvisioner);
 }
 /**
  * Handle a provisioning request
  *
  * @param \DreamFactory\Enterprise\Services\Jobs\ExportJob $job
  *
  * @return array Array version of the snapshot manifest
  *
  */
 public function handle(ExportJob $job)
 {
     $this->registerHandler($job);
     $this->info('export "' . ($_instanceId = $job->getInstanceId()) . '"');
     $this->startTimer();
     try {
         if (false === ($_response = Provision::export($job))) {
             throw new \RuntimeException('Unknown import failure');
         }
     } catch (\RuntimeException $_ex) {
         $this->error('[ERROR] ' . $_ex->getMessage());
         !isset($_response) && ($_response = false);
     }
     $this->info('request complete in ' . number_format($this->getElapsedTime(), 4) . 's');
     return $_response;
 }
 /**
  * Handle a provisioning request
  *
  * @param \DreamFactory\Enterprise\Services\Jobs\ImportJob $job
  *
  * @return mixed
  *
  */
 public function handle(ImportJob $job)
 {
     $this->registerHandler($job);
     $this->info('import "' . ($_instanceId = $job->getInstanceId()) . '"');
     $this->startTimer();
     try {
         if (false === ($_response = Provision::import($job))) {
             throw new \RuntimeException('Unknown import failure');
         }
         if (true === $_response) {
             $this->notice('Partial import of "' . $_instanceId . '". No portable services are available for "guest-location".');
         }
     } catch (\RuntimeException $_ex) {
         $this->error('[ERROR] ' . $_ex->getMessage());
         !isset($_response) && ($_response = false);
     }
     $this->info('instance import complete in ' . number_format($this->getElapsedTime(), 4) . 's');
     return $_response;
 }
 /**
  * Handle a provisioning request
  *
  * @param ProvisionJob $command
  *
  * @return mixed
  */
 public function handle(ProvisionJob $command)
 {
     $_options = $command->getOptions();
     $_guestLocation = array_get($_options, 'guest-location', config('provisioning.default-guest-location'));
     if (is_string($_guestLocation) && !is_numeric($_guestLocation)) {
         $_options['guest-location'] = GuestLocations::resolve($_guestLocation, true);
         \Log::debug('[Provision] guest location "' . $_options['guest-location'] . '" resolved from "' . $_guestLocation . '".');
         $_guestLocation = $_options['guest-location'];
     }
     \Log::info('[Provision] Request [guest=' . $_guestLocation . ']: ' . Json::encode($_options));
     try {
         //  Create the instance record
         $_instance = InstanceManager::make($command->getInstanceId(), $_options);
         if (!$_instance) {
             throw new ProvisioningException('InstanceManager::make() failed');
         }
     } catch (\Exception $_ex) {
         \Log::error('[Provision] failure, exception creating instance: ' . $_ex->getMessage());
         return false;
     }
     try {
         $_guest = array_get($_options, 'guest-location', config('provisioning.default-guest-location'));
         $_provisioner = Provision::getProvisioner($_guest);
         if (empty($_provisioner)) {
             throw new \RuntimeException('The provisioner of the request is not valid.');
         }
         if (false === ($_response = $_provisioner->provision(new ProvisionServiceRequest($_instance)))) {
             throw new ProvisioningException('provisioning error');
         }
         \Log::info('[Provision] completed in ' . number_format($_response->getElapsedTime(), 4) . 's');
         return $_response;
     } catch (\Exception $_ex) {
         \Log::error('[Provision] failure: ' . $_ex->getMessage());
         //  Delete instance record...
         if (!$_instance->delete()) {
             throw new \LogicException('Unable to remove created instance "' . $_instance->instance_id_text . '".');
         }
     }
     return false;
 }
 /**
  * Given an instance and a snapshot ID, replace the data with that of the snapshot.
  *
  * @param int|string $instanceId
  * @param string     $snapshot A snapshot id or path to a snapshot
  *
  * @return array
  */
 public function restore($instanceId, $snapshot)
 {
     $_filename = null;
     $_instance = $this->_findInstance($instanceId);
     $snapshot = trim($snapshot, DIRECTORY_SEPARATOR . ' ');
     //  Determine source of import
     if (file_exists($snapshot)) {
         //  Absolute path
         $_filename = $snapshot;
     } else {
         if (file_exists($_instance->getSnapshotPath() . DIRECTORY_SEPARATOR . $snapshot)) {
             //  Relative to snapshot path
             $_filename = $_instance->getSnapshotPath() . DIRECTORY_SEPARATOR . $snapshot;
         } else {
             //  A snapshot hash given, find related snapshot
             /** @type Snapshot $_snapshot */
             if (null === ($_snapshot = Snapshot::with('route-hash')->bySnapshotId($snapshot)->first())) {
                 throw new \InvalidArgumentException('The snapshot "' . $snapshot . '" is unrecognized or invalid."');
             }
             return Provision::import(new ImportJob(new PortableServiceRequest($_instance, ['target' => $_snapshot->snapshot_id_text])));
         }
     }
     //  Mount the snapshot
     $_workPath = dirname($_filename);
     $_fsSnapshot = $this->mountSnapshot($instanceId, $_filename, $_workPath);
     //  Reconstitute the manifest and grab the services
     $_manifest = SnapshotManifest::createFromFile(config('snapshot.metadata-file-name'), $_fsSnapshot);
     $_services = Provision::getPortableServices($_manifest->get('guest-location'));
     $_result = [];
     foreach ($_fsSnapshot->listContents() as $_item) {
         //  Find the exports in the snapshot
         if (false !== ($_pos = stripos('-export.', $_item['file']))) {
             if (!PortableTypes::has($_type = substr($_item['file'], 0, $_pos))) {
                 continue;
             }
             if (array_key_exists($_type, $_services)) {
                 $_job = new ImportJob(new PortableServiceRequest($_instance, ['target' => $_workPath . DIRECTORY_SEPARATOR . $_item['file']]));
                 $_result[$_type] = $_services[$_type]->import($_job);
             }
         }
     }
     return $_result;
 }
 /**
  * @param ProvisionServiceRequest $request
  * @param array                   $options ['keep-database'=>true|false]
  *
  * @return bool
  * @throws ProvisioningException
  */
 protected function deprovisionInstance($request, $options = [])
 {
     $_keepDatabase = array_get($options, 'keep-database', false);
     $_instance = $request->getInstance();
     $_name = $_instance->instance_id_text;
     $this->info('[deprovisioning:instance] instance "' . $_name . '" begin');
     if ($_keepDatabase) {
         $this->info('[deprovisioning:instance] "keep-database" specified. Keeping existing schema, if any.');
     } else {
         //	Deprovision the database
         $_dbService = Provision::getDatabaseProvisioner($_instance->guest_location_nbr);
         if (false === ($_dbConfig = $_dbService->deprovision($request))) {
             throw new ProvisioningException('Failed to deprovision database. Check logs for error.');
         }
     }
     try {
         if (!$_instance->delete()) {
             throw new \RuntimeException('Instance row deletion failed.');
         }
     } catch (\Exception $_ex) {
         $this->error('[deprovisioning:instance] exception while deleting instance row: ' . $_ex->getMessage());
         return false;
     }
     //  Fire off a "deprovisioned" event...
     \Event::fire('dfe.deprovisioned', [$this, $request]);
     $this->info('[deprovisioning:instance] instance "' . $_name . '" complete');
     return true;
 }
 /**
  * @return ResourceProvisioner|PrivatePathAware
  */
 public function getStorageProvisioner()
 {
     if (null === ($_provisioner = $this->get('storage-provisioner'))) {
         $_provisioner = Provision::getStorageProvisioner($this->getInstance()->guest_location_nbr);
         $this->setStorageProvisioner($_provisioner);
     }
     return $_provisioner;
 }