/**
  * Upload the deployment package to the server.
  * @param string $packageFile
  * @return array
  * @throws ServerException
  */
 public function deploy($packageFile)
 {
     $url = $this->config->get('remote.endpoint') . '?cmd=deploy';
     $config = $this->getRequestConfig();
     $config['package_md5'] = md5_file($packageFile);
     $this->logger->info("Sending package to remote server...");
     try {
         $response = $this->getClient()->request('POST', $url, ['headers' => $this->getHeaders(), 'multipart' => [['name' => 'config', 'contents' => json_encode($config)], ['name' => 'package', 'contents' => fopen($packageFile, 'r'), 'headers' => ['Content-Type' => 'application/gzip']]], 'progress' => function ($downloadSize, $downloaded, $uploadSize, $uploaded) {
             $p = $uploaded == 0 ? 0 : intval($uploaded * 100 / $uploadSize);
             $this->logger->plain($p . '% uploaded (' . $uploaded . ' of ' . $uploadSize . " bytes)\r");
         }]);
         $this->logger->plain("\n");
         return json_decode($response->getBody()->getContents(), true);
     } catch (ClientException $e) {
         throw new ServerException("Deploy error: " . $this->parseClientError($e), 0, $e);
     } catch (RequestException $e) {
         throw new ServerException("An error ocurred in the request while deploying the package: " . $e->getMessage(), 0, $e);
     }
 }
 /**
  * Upload the deployment package to the server.
  * @param string $packageFile
  * @return array
  * @throws ServerException
  */
 public function deploy($packageFile)
 {
     $url = $this->config->get('remote.endpoint') . '?cmd=deploy';
     $config = $this->getRequestConfig();
     $config['package_md5'] = md5_file($packageFile);
     $this->logger->info("Sending package to remote server...");
     try {
         $body = new PostBody();
         $body->setField('config', json_encode($config));
         $body->addFile(new PostFile('package', fopen($packageFile, 'r'), null, ['Content-Type' => 'application/gzip']));
         $request = $this->getClient()->createRequest('POST', $url, ['headers' => $this->getHeaders(), 'body' => $body]);
         $request->getEmitter()->on('progress', function (ProgressEvent $e) {
             $p = $e->uploaded == 0 ? 0 : intval($e->uploaded * 100 / $e->uploadSize);
             $this->logger->plain($p . '% uploaded (' . $e->uploaded . ' of ' . $e->uploadSize . " bytes)\r");
         });
         $response = $this->getClient()->send($request);
         return json_decode($response->getBody()->getContents(), true);
     } catch (ClientException $e) {
         throw new ServerException("Deploy error: " . $this->parseClientError($e), 0, $e);
     } catch (RequestException $e) {
         throw new ServerException("An error ocurred in the request while deploying the package: " . $e->getMessage(), 0, $e);
     }
 }
 private function applyBeforeScripts()
 {
     $this->logger->info("Applying before scripts...");
     foreach ($this->beforeScripts as $script) {
         $cmd = sprintf("cd %s && %s", $this->local, $script);
         $out = "";
         $ret = 0;
         exec($cmd, $out, $ret);
         if ($ret != 0) {
             throw new HttpDeployerException("An error ocurred while executing the before scipts: " . $out);
         }
         $this->logger->info($cmd . ": " . $out);
     }
 }