require 'net/scp' module Evrone module CI module Worker DockerScript = Struct.new(:app) do include Helper::Logger def call(env) if env.ssh && env.docker_repo_dir code = run_script(env) if code == 0 app.call env else case read_state(env) when "script" code else code * -1 end end else app.call env end end private def run_script(env) scp = ::Net::SCP.new(env.ssh.connection) script = [env.docker_repo_dir, ".ci_build.sh"].join("/") scp.upload! StringIO.new(content(env)), script env.ssh.spawn "env - HOME=$HOME bash #{script}", chdir: env.docker_repo_dir, &env.job.method(:add_to_output) end def content(env) buf = ["set -e"] buf << "echo before_script > #{env.docker_repo_dir}/.ci_state" buf << env.job.message.before_script buf << "echo script > #{env.docker_repo_dir}/.ci_state" buf << env.job.message.script buf.join("\n") end def read_state(env) buf = "" state_file = env.docker_repo_dir.join(".ci_state") env.ssh.spawn "cat #{state_file}" do |out| buf << out end buf.strip end end end end end