require 'docker' require 'evrone/common/spawn' require 'socket' require 'pathname' module Evrone module CI module Worker DockerStartContainer = Struct.new(:app) do include Helper::Config include Helper::Logger include Evrone::Common::Spawn def call(env) spawn_container(env) do open_ssh_session(env) do app.call env end end end private def open_ssh_session(env) host = env.container_addr user = config.docker.ssh.user pass = config.docker.ssh.password ssh_options = { password: pass, port: config.docker.ssh.port || 22, paranoid: false, forward_agent: false } sleep 2 env.job.add_to_output "open ssh session to #{user}@#{host}:#{ssh_options[:port]}\n" open_ssh(host, "ci", ssh_options) do |ssh| begin env.ssh = ssh logger.tagged "SSH" do yield end end end end def spawn_container(env) env.container = ::Docker::Container.create create_options env.container.start env.job.add_to_output "using #{Socket.gethostname}/#{env.container.id}\n" env.container_addr = config.docker.ssh.host env.container_addr ||= env.container.json['NetworkSettings']['IPAddress'] env.docker_repo_dir = Pathname.new(config.docker.remote_dir).join(env.job.message.name) logger.tagged "DOCKER #{env.container.id}" do begin logger.info "start container" sleep 3 yield ensure env.container.stop logger.info "stop container" end end end def create_options { 'Cmd' => config.docker.init, 'Image' => config.docker.image, }.merge(config.docker.create_options || {}) end end end end end