Sha256: e1ffb0975f114447e14f031c0c8fa5b7c953bdfdd114af8b24fc74ee5092d2a4

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

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


Version data entries

3 entries across 3 versions & 1 rubygems

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