Example #1
0
 /**
  * Handle the command
  *
  * @return mixed
  */
 public function fire()
 {
     parent::fire();
     $_instanceId = $this->argument('instance-id');
     //	Check the name here for quicker response...
     if (false === ($_instanceName = Instance::isNameAvailable($_instanceId)) || is_numeric($_instanceName[0])) {
         $this->error('The name of your instance cannot be "' . $_instanceId . '".  It is either currently in-use, or otherwise invalid.');
         exit(1);
     }
     $_ownerType = OwnerTypes::USER;
     $_ownerId = $this->argument('owner-id');
     $_guestLocation = $this->argument('guest-location');
     $_owner = $this->_locateOwner($_ownerId, $_ownerType);
     $this->writeln('Provisioning instance <comment>"' . $_instanceId . '"</comment>.');
     return \Queue::push(new ProvisionJob($_instanceId, ['guest-location' => $_guestLocation, 'owner-id' => $_owner->id, 'owner-type' => $_ownerType ?: OwnerTypes::USER, 'cluster-id' => $this->option('cluster-id')]));
 }
 /**
  * @param string $instanceId
  * @param bool   $trial
  * @param bool   $remote If true, create instance on user's account
  *
  * @return bool|mixed|\stdClass
  */
 public function provisionInstance($instanceId, $trial = false, $remote = false)
 {
     Flasher::forget();
     $_provisioner = $this->request->input('_provisioner', GuestLocations::DFE_CLUSTER);
     //	Check the name here for quicker UI response...
     if (false === ($_instanceName = Instance::isNameAvailable($instanceId)) || is_numeric($_instanceName[0])) {
         Flasher::setIf('The name of your instance cannot be "' . $instanceId . '".  It is either currently in-use, or otherwise invalid.', false);
         return ErrorPacket::create(null, Response::HTTP_BAD_REQUEST, 'Invalid instance name.');
     }
     if (false === ($_clusterConfig = $this->getClusterConfig())) {
         Flasher::setIf('The DFE Console is not currently available. Please try your request later.', false);
         return ErrorPacket::create(null, Response::HTTP_INTERNAL_SERVER_ERROR, 'Cluster server configuration error.');
     }
     $_payload = array_merge(['instance-id' => $_instanceName, 'trial' => $trial, 'remote' => $remote, 'ram-size' => $this->request->input('ram-size'), 'disk-size' => $this->request->input('disk-size'), 'vendor-id' => $this->request->input('vendor-id'), 'vendor-secret' => $this->request->input('vendor-secret'), 'owner-id' => \Auth::id(), 'owner-type' => OwnerTypes::USER, 'guest-location' => $_provisioner], $_clusterConfig);
     $_result = $this->callConsole('provision', $_payload);
     if ($_result && is_object($_result) && isset($_result->success)) {
         if ($_result->success) {
             Flasher::setIf('Instance provisioning requested successfully.');
         } else {
             if (isset($_result->error)) {
                 $_message = isset($_result->error->message) ? $_result->error->message : 'Unknown error';
             } else {
                 $_message = 'Unknown server error';
             }
             Flasher::setIf($_message, false);
             return ErrorPacket::create(null, Response::HTTP_INTERNAL_SERVER_ERROR, 'Provisioning error.');
         }
     } else {
         Flasher::setIf('The DFE Console is not currently available. Please try your request later.', false);
         $this->error('Error calling ops console api: ' . print_r($_result, true));
         return ErrorPacket::create(null, Response::HTTP_INTERNAL_SERVER_ERROR, 'Cannot connect to ops console.');
     }
     return SuccessPacket::create($_result);
 }
 /**
  * Create a new instance record
  *
  * @param string $instanceName
  * @param array  $options Array of options for creation. Options are:
  *
  *                        owner-id      The id of the instance owner
  *                        cluster-id    The cluster that owns this instance
  *                        trial         If true, the "trial" flagged is set for the instance
  *
  * @return Instance
  * @throws DuplicateInstanceException
  * @throws ProvisioningException
  */
 public function make($instanceName, $options = [])
 {
     try {
         //  Basic checks...
         if (null === ($_ownerId = array_get($options, 'owner-id'))) {
             throw new \InvalidArgumentException('No "owner-id" given. Cannot create instance.');
         }
         if (null == ($_ownerType = array_get($options, 'owner-type'))) {
             $_ownerType = OwnerTypes::USER;
         }
         try {
             $_owner = OwnerTypes::getOwner($_ownerId, $_ownerType);
         } catch (ModelNotFoundException $_ex) {
             throw new \InvalidArgumentException('The "owner-id" and/or "owner-type" specified is/are invalid.');
         }
         //$this->debug('owner validated: ' . $_owner->id . ($_owner->admin_ind ? ' (admin)' : ' (non-admin)'));
         if (false === ($_sanitized = Instance::isNameAvailable($instanceName, $_owner->admin_ind))) {
             throw new DuplicateInstanceException('The instance name "' . $instanceName . '" is not available.');
         }
         //  Get the proper location
         $_guestLocation = array_get($options, 'guest-location', config('provisioning.default-guest-location'));
         //  Validate the cluster and pull component ids
         $_clusterId = array_get($options, 'cluster-id', config('provisioning.default-cluster-id'));
         $_clusterConfig = $this->getServersForCluster($_clusterId);
         $_ownerId = $_owner->id;
         $_attributes = ['user_id' => (int) $_ownerId, 'instance_id_text' => $_sanitized, 'instance_name_text' => $_sanitized, 'guest_location_nbr' => $_guestLocation, 'cluster_id' => (int) $_clusterConfig['cluster-id'], 'db_server_id' => (int) $_clusterConfig['db-server-id'], 'app_server_id' => (int) $_clusterConfig['app-server-id'], 'web_server_id' => (int) $_clusterConfig['web-server-id'], 'state_nbr' => ProvisionStates::CREATED];
         $_guestAttributes = ['instance_id' => null, 'vendor_id' => $_guestLocation, 'vendor_image_id' => array_get($options, 'vendor-image-id', config('provisioning.default-vendor-image-id')), 'vendor_credentials_id' => array_get($options, 'vendor-credentials-id', config('provisioning.default-vendor-credentials-id'))];
         //  Write it out
         return \DB::transaction(function () use($_ownerId, $_attributes, $_guestAttributes) {
             $_instance = Instance::create($_attributes);
             //$this->debug('created instance row id#' . $_instance->id);
             $_guestAttributes['instance_id'] = $_instance->id;
             $_guest = InstanceGuest::create($_guestAttributes);
             //$this->debug('created guest row id#' . $_guest->id);
             if (!$_instance || !$_guest) {
                 throw new \RuntimeException('Instance creation failed');
             }
             return $_instance;
         });
     } catch (\Exception $_ex) {
         throw new ProvisioningException('Error creating new instance: ' . $_ex->getMessage());
     }
 }