Exemple #1
0
 /**
  * @param Build $build
  *
  * @return Docker\Container
  */
 public function run(Build $build, $timeout = null)
 {
     $producer = $this->websocketProducer;
     $logger = $this->logger;
     $docker = $this->docker;
     $em = $this->doctrine->getManager();
     $publish = function ($content) use($build, $producer) {
         $message = new BuildMessage($build, $content);
         $producer->publish((string) $message);
     };
     // $publish('  build started ('.date('r').')');
     if ($this->getOption('dummy')) {
         $logger->info('dummy build, sleeping', ['duration' => $this->getOption('dummy_duration')]);
         sleep($this->getOption('dummy_duration'));
         $build->setPort(42);
         return true;
     }
     $project = $build->getProject();
     $logger->info('starting build', ['build' => $build->getId(), 'project' => $project->getFullName(), 'project_id' => $project->getId(), 'ref' => $build->getRef(), 'hash' => $build->getHash(), 'timeout' => $timeout, 'force_local_build_yml' => $build->getForceLocalBuildYml()]);
     /** Generate build script using Yuhao */
     $logger->info('generating build script');
     // $publish('  generating build script'.PHP_EOL);
     $builder = $project->getDockerContextBuilder();
     $builder->from('stage1/yuhao');
     $docker->build($builder->getContext(), $build->getImageName('yuhao'), false, true, true);
     $prepareContainer = new PrepareContainer($build);
     // @todo move inside PrepareContainer
     if ($build->getForceLocalBuildYml()) {
         $prepareContainer->addEnv(['FORCE_LOCAL_STAGE1_YML=1']);
     }
     $manager = $docker->getContainerManager();
     $manager->create($prepareContainer)->start($prepareContainer)->wait($prepareContainer, $timeout);
     if ($prepareContainer->getExitCode() != 0) {
         $exitCode = $prepareContainer->getExitCode();
         if (isset(Process::$exitCodes[$exitCode])) {
             $exitCodeLabel = Process::$exitCodes[$exitCode];
         } else {
             $exitCodeLabel = '';
         }
         $message = sprintf('failed to generate build scripts (exit code %d (%s))', $exitCode, $exitCodeLabel);
         $publish($message . PHP_EOL);
         $logger->error($message, ['build' => $build->getId(), 'container' => $prepareContainer->getId(), 'container_name' => $prepareContainer->getName(), 'exit_code' => $exitCode, 'exit_code_label' => $exitCodeLabel]);
         $docker->commit($prepareContainer, ['repo' => $build->getImageName('yuhao'), 'tag' => 'failed']);
         throw new Exception($message, $prepareContainer->getExitCode());
     }
     // @todo remove yuhao container
     // $manager->remove($prepareContainer); => 406 ?!
     $logger->info('yuhao finished executing, retrieving build script', ['build' => $build->getId()]);
     $output = '';
     $manager->attach($prepareContainer, true, false, false, true, true)->readAttach(function ($type, $chunk) use(&$output) {
         $output .= $chunk;
     });
     $logger->info('got response from yuhao', ['build' => $build->getId(), 'response' => $output, 'parsed_response' => json_decode($output, true)]);
     $script = BuildScript::fromJson($output);
     $script->setBuild($build);
     $options = $project->getDefaultBuildOptions();
     $options = $options->resolve($script->getConfig());
     $build->setOptions($options);
     $logger->info('resolved options', ['options' => $options]);
     $strategy = array_key_exists('path', $options['dockerfile']) ? new DockerfileStrategy($logger, $docker, $em, $this->websocketProducer, $this->redis, $this->options) : new DefaultStrategy($logger, $docker, $em, $this->websocketProducer, $this->options);
     $logger->info('elected strategy', ['strategy' => get_class($strategy)]);
     $em->persist($build);
     $em->persist($script);
     $em->flush();
     $logger->info('starting build');
     $strategy->build($build, $script, $timeout);
     $logger->info('finished build');
     $em->persist($build);
     $em->persist($script);
     $em->flush();
     /**
      * Launch App container
      */
     $logger->info('starting app container', ['project' => $build->getProject()->getFullName(), 'image' => $build->getImageName()]);
     $ports = new PortCollection(80);
     # @todo DefaultStrategy containers should have an entrypoint
     #       so we don't need to provide an actual command
     $appContainer = new AppContainer($build, $strategy->getCmd());
     $appContainer->addEnv($build->getProject()->getContainerEnv());
     $appContainer->setExposedPorts($ports);
     if ($build->getForceLocalBuildYml()) {
         $appContainer->addEnv(['FORCE_LOCAL_STAGE1_YML=1']);
     }
     try {
         $manager->create($appContainer)->start($appContainer, ['PortBindings' => $ports->toSpec()]);
         $logger->info('running app container', ['build' => $build->getId(), 'container' => $appContainer->getId()]);
     } catch (\Docker\Exception\UnexpectedStatusCodeException $e) {
         if ($e->getCode() === 404) {
             throw new \RuntimeException('Could not start app container (' . $e->getMessage() . ')', 404, $e);
         }
         throw $e;
     }
     // $publish('  build finished ('.date('r').')'.PHP_EOL);
     return $appContainer;
 }
 public function build(Build $build, BuildScript $script, $timeout)
 {
     $logger = $this->logger;
     $docker = $this->docker;
     $objectManager = $this->objectManager;
     $websocketProducer = $this->websocketProducer;
     $publish = function ($content) use($build, $websocketProducer) {
         $message = new BuildMessage($build, $content);
         $websocketProducer->publish((string) $message);
     };
     $options = $build->getOptions();
     $project = $build->getProject();
     /**
      * Launch actual build
      */
     $logger->info('building base build container', ['build' => $build->getId(), 'image_name' => $build->getImageName()]);
     // $publish('  building base container'.PHP_EOL);
     $baseImage = strpos($options['image'], 'stage1/') !== 0 ? 'stage1/' . $options['image'] : $options['image'];
     $builder = $project->getDockerContextBuilder();
     $builder->add('/usr/local/bin/yuhao_build', $script->getBuildScript());
     $builder->add('/usr/local/bin/yuhao_run', $script->getRunScript());
     $builder->run('chmod -R +x /usr/local/bin/');
     $builder->from($baseImage);
     $response = $docker->build($builder->getContext(), $build->getImageName(), false, true, true);
     $buildContainer = new BuildContainer($build);
     $buildContainer->addEnv($options['env']);
     $script->setRuntimeEnv($buildContainer->getEnv());
     if ($build->getForceLocalBuildYml()) {
         $buildContainer->addEnv(['FORCE_LOCAL_STAGE1_YML=1']);
     }
     $manager = $docker->getContainerManager();
     $logger->info('starting actual build', ['build' => $build->getId(), 'timeout' => $timeout]);
     // $publish('  starting actual build'.PHP_EOL);
     $hostConfig = [];
     if ($this->getOption('composer_enable_global_cache')) {
         $logger->info('enabling composer global cache', ['build' => $build->getId()]);
         $hostConfig['Binds'] = [$this->getOption('composer_cache_path') . '/global:/.composer/cache'];
     } elseif ($this->getOption('composer_enable_project_cache')) {
         $cachePath = $this->getOption('composer_cache_path') . '/' . $project->getFullName();
         $logger->info('enabling composer project cache', ['build' => $build->getId(), 'project' => $project->getFullName(), 'cache_path' => $cachePath]);
         if (!is_dir($cachePath)) {
             mkdir($cachePath, 0777, true);
         }
         $hostConfig['Binds'] = [realpath($cachePath) . ':/.composer/cache'];
     }
     $manager->create($buildContainer);
     $build->setContainer($buildContainer);
     $manager->start($buildContainer, $hostConfig);
     $manager->wait($buildContainer, $timeout);
     if ($buildContainer->getExitCode() !== 0) {
         $exitCode = $buildContainer->getExitCode();
         $exitCodeLabel = isset(Process::$exitCodes[$exitCode]) ? Process::$exitCodes[$exitCode] : '';
         $message = sprintf('build container stopped with exit code %d (%s)', $exitCode, $exitCodeLabel);
         $logger->error($message, ['build' => $build->getId(), 'container' => $buildContainer->getId(), 'container_name' => $buildContainer->getName(), 'exit_code' => $exitCode, 'exit_code_label' => $exitCodeLabel]);
         $docker->commit($buildContainer, ['repo' => $build->getImageName(), 'tag' => 'failed']);
         throw new Exception($message, $buildContainer->getExitCode());
     }
     /**
      * Build successful!
      *
      * @todo the commit can timeout for no obvious reason, while actually committing
      *       catch the timeout and check if the image has been committed
      *          if yes, proceed
      *          if not, retry (3 times ?)
      */
     $logger->info('build successful, committing', ['build' => $build->getId(), 'container' => $buildContainer->getId()]);
     // $publish('  committing app container'.PHP_EOL);
     $docker->commit($buildContainer, ['repo' => $build->getImageName()]);
     $logger->info('removing build container', ['build' => $build->getId(), 'container' => $buildContainer->getId()]);
     // $publish('  removing build container'.PHP_EOL);
     $manager->remove($buildContainer);
 }