Sha256: 3f9e890022e1937ecf24304ab76c150a61835fa3e08d86da2c9958882b7f188a

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

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


Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
evrone-ci-worker-0.2.0.pre4 lib/evrone/ci/worker/middlewares/docker_fetch_repo.rb
evrone-ci-worker-0.2.0.pre3 lib/evrone/ci/worker/middlewares/docker_fetch_repo.rb