/** * 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; }
/** * 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; }