require 'docker' require 'evrone/common/spawn' require 'net/scp' module Evrone module CI module Worker DockerFetchRepo = Struct.new(:app) do include Helper::Logger include Helper::Config def call(env) if env.ssh && env.docker_repo_dir code = -1 code = prepare_ssh_files(env) code = spawn_script(env) if code == 0 if code == 0 app.call env else code * -1 end else app.call env end end private def prepare_ssh_files(env) dir = config.docker.remote_dir scp = ::Net::SCP.new(env.ssh.connection) code = env.ssh.spawn "mkdir -p #{dir}/.ssh && chmod 0700 #{dir}/.ssh", &env.job.method(:add_to_output) if code == 0 config = "Host *\n" config << " StrictHostKeyChecking no\n" config << " UserKnownHostsFile /dev/null\n" scp.upload! StringIO.new(config), "#{dir}/.ssh/config" scp.upload! StringIO.new(env.job.message.deploy_key.to_s), "#{dir}/.ssh/id_rsa" code = env.ssh.spawn "chmod 0600 #{dir}/.ssh/id_rsa", &env.job.method(:add_to_output) end code end def spawn_script(env) scm = create_scm(env) script = "sh -c '#{scm.make_fetch_command}'" env.ssh.spawn script, &env.job.method(:add_to_output) end def create_scm(env) SCM::Git.new( env.job.message.src, env.job.message.sha, env.docker_repo_dir, &env.job.method(:add_to_output) ) end end end end end