Sha256: 7ad995002c6f7181d9043b32d943bc88581910ccd46d6419b77db650b973383d

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 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
              -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)

            env.job.add_command_to_output("git clone #{scm.src} #{scm.path}")
            env.job.add_command_to_output("git checkout -q #{scm.sha}")

            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.pre1 lib/evrone/ci/worker/middlewares/docker_fetch_repo.rb
evrone-ci-worker-0.2.0.pre0 lib/evrone/ci/worker/middlewares/docker_fetch_repo.rb