/**
  * Installs or updates the Composer binary.
  *
  * @param WorkspaceEvent           $event
  * @param string                   $eventName
  * @param EventDispatcherInterface $eventDispatcher
  *
  * @throws TaskRuntimeException when installing the Composer binary has failed.
  * @throws TaskRuntimeException when the workspace hasn't been created.
  */
 public function onPrepareWorkspaceInstallComposer(WorkspaceEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     $host = $event->getHost();
     $connection = $this->ensureConnection($host);
     $workspace = $event->getWorkspace();
     if ($workspace instanceof Workspace === false) {
         throw new TaskRuntimeException('The workspace of the host has not been created.', $this);
     }
     if ($connection->isFile($host->getPath() . '/composer.phar') === false) {
         $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Installing the Composer binary...', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_IN_PROGRESS)));
         $connection->changeWorkingDirectory($host->getPath());
         $connection->putFile(__DIR__ . '/../Resources/Composer/composer.phar', $host->getPath() . '/composer.phar');
         if ($connection->isFile($host->getPath() . '/composer.phar')) {
             $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Installed the Composer binary.', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_COMPLETED, 'output.resetLine' => true)));
         } else {
             throw new TaskRuntimeException('Failed installing the Composer binary.', $this);
         }
     }
     $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Updating the Composer binary.', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_IN_PROGRESS)));
     $connection->changeWorkingDirectory($host->getPath());
     $result = $connection->executeCommand('php composer.phar self-update');
     if ($result->isSuccessful()) {
         $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Updated the Composer binary.', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_COMPLETED, 'output.resetLine' => true)));
     } else {
         $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::WARNING, 'Failed updating the Composer binary.', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_FAILED)));
     }
     $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::DEBUG, "{separator} Command output:{separator}\n{command.result}{separator}", $eventName, $this, array('command.result' => $result->getOutput(), 'separator' => "\n=================\n")));
 }
 /**
  * Initializes the SSH agent and adds the configured keys.
  *
  * @param WorkspaceEvent           $event
  * @param string                   $eventName
  * @param EventDispatcherInterface $eventDispatcher
  */
 public function onPrepareWorkspaceInitializeSSHAgent(WorkspaceEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     $this->host = $event->getHost();
     $connection = $this->ensureConnection($this->host);
     $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::NOTICE, 'Initializing SSH agent...', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_IN_PROGRESS)));
     $result = $connection->executeCommand('eval $(ssh-agent)');
     if ($result->isSuccessful()) {
         $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::NOTICE, 'Initialized SSH agent. {output}', $eventName, $this, array('output' => trim($result->getOutput()), 'event.task.action' => TaskInterface::ACTION_COMPLETED, 'output.resetLine' => true)));
         foreach ($this->keys as $key) {
             $this->addKeyToSSHAgent($event->getWorkspace(), $connection, $key, $eventName, $eventDispatcher);
         }
     } else {
         $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::WARNING, 'Failed initializing SSH agent.', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_FAILED, 'output.resetLine' => true)));
     }
 }
 /**
  * Uploads the maintenance page (and related resources).
  *
  * @param WorkspaceEvent           $event
  * @param string                   $eventName
  * @param EventDispatcherInterface $eventDispatcher
  */
 public function onPrepareWorkspaceUploadMaintenancePage(WorkspaceEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     $host = $event->getWorkspace()->getHost();
     $connection = $this->ensureConnection($host);
     $directory = sprintf('%s/maintenance/%s', $host->getPath(), $this->documentRoot);
     $context = array('directory' => $directory, 'event.task.action' => TaskInterface::ACTION_IN_PROGRESS);
     $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::NOTICE, 'Creating directory "{directory}".', $eventName, $this, $context));
     if ($connection->isDirectory($directory) === false) {
         if ($connection->createDirectory($directory)) {
             $context['event.task.action'] = TaskInterface::ACTION_COMPLETED;
             $context['output.resetLine'] = true;
             $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::NOTICE, 'Created directory "{directory}".', $eventName, $this, $context));
         } else {
             $context['event.task.action'] = TaskInterface::ACTION_FAILED;
             $context['output.resetLine'] = true;
             $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::WARNING, 'Failed creating directory "{directory}".', $eventName, $this, $context));
         }
     } else {
         $context['event.task.action'] = TaskInterface::ACTION_COMPLETED;
         $context['output.resetLine'] = true;
         $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::NOTICE, 'Directory "{directory}" exists.', $eventName, $this, $context));
     }
     if ($connection->isDirectory($directory)) {
         $files = array_diff(scandir($this->localMaintenanceDirectory), array('.', '..'));
         foreach ($files as $file) {
             $localFile = $this->localMaintenanceDirectory . '/' . $file;
             if (is_file($localFile)) {
                 $context = array('file' => $localFile, 'event.task.action' => TaskInterface::ACTION_COMPLETED);
                 $uploaded = $connection->putFile($localFile, $directory . '/' . $file);
                 if ($uploaded === true) {
                     $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Uploaded file "{file}".', $eventName, $this, $context));
                 }
             }
         }
     }
 }
 /**
  * Creates the workspace directories when not already existing.
  *
  * @param WorkspaceEvent           $event
  * @param string                   $eventName
  * @param EventDispatcherInterface $eventDispatcher
  *
  * @throws TaskRuntimeException when the workspace path doesn't exist and can't be created.
  */
 public function onPrepareWorkspaceCreateWorkspace(WorkspaceEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     $workspace = $event->getWorkspace();
     $connection = $this->ensureConnection($workspace->getHost());
     $workspacePath = $workspace->getHost()->getPath();
     if ($connection->isDirectory($workspacePath) === false && $connection->createDirectory($workspacePath) === false) {
         throw new TaskRuntimeException(sprintf('The workspace path "%s" does not exist and could not be created.', $workspacePath), $this);
     }
     $directories = array_merge(array($workspace->getReleasesDirectory(), $workspace->getDataDirectory(), $workspace->getCacheDirectory()), $workspace->getOtherDirectories());
     foreach ($directories as $directory) {
         $context = array('directory' => $directory, 'event.task.action' => TaskInterface::ACTION_IN_PROGRESS);
         $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Creating directory "{directory}".', $eventName, $this, $context));
         if ($connection->isDirectory($directory) === false) {
             if ($connection->createDirectory($directory)) {
                 $context['event.task.action'] = TaskInterface::ACTION_COMPLETED;
                 $context['output.resetLine'] = true;
                 $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Created directory "{directory}".', $eventName, $this, $context));
             } else {
                 $context['event.task.action'] = TaskInterface::ACTION_FAILED;
                 $context['output.resetLine'] = true;
                 $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::WARNING, 'Failed creating directory "{directory}".', $eventName, $this, $context));
             }
         } else {
             $context['event.task.action'] = TaskInterface::ACTION_COMPLETED;
             $context['output.resetLine'] = true;
             $eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Directory "{directory}" exists.', $eventName, $this, $context));
         }
     }
 }