require 'net/scp' module Evrone module CI module Worker DockerWebdavCache = Struct.new(:app) do include Helper::Config include Helper::Logger CASHER_URL = "https://raw.github.com/travis-ci/casher/master/bin/casher" CASHER_BIN = "$HOME/.casher/bin/casher" def call(env) if env.ssh && config.webdav_cache_url assign_url(env) && prepare(env) && fetch(env) && add(env) end rs = app.call env if env.ssh && env.webdav_cache_url push(env) end rs end private def assign_url(env) release = nil env.ssh.spawn "lsb_release -s -c" do |out| release ||= out.gsub(/[^a-z0-9]/, '') end release = 'unknown' unless release name = env.job.message.name.dup name << "-#{release}" name << "-#{env.job.message.matrix_keys.join("_")}.tgz" name = name.gsub(/[^a-z0-9_\-.]/, '') env.webdav_cache_url = "#{config.webdav_cache_url.dup}/#{name}" true end def push(env) cmd = %{ #{CASHER_BIN} push #{env.webdav_cache_url} }.compact logger.debug "push cache to #{env.webdav_cache_url.inspect}" env.ssh.spawn cmd, &env.job.method(:add_trace_to_output) true end def add(env) cmd = %{ #{CASHER_BIN} add $HOME/cached }.compact env.ssh.spawn cmd, &env.job.method(:add_trace_to_output) true end def fetch(env) logger.debug "fetch cache from #{env.webdav_cache_url.inspect}" cmd = %{ #{CASHER_BIN} fetch #{env.webdav_cache_url} }.compact env.ssh.spawn cmd, &env.job.method(:add_trace_to_output) true end def prepare(env) cmd = %{ export CASHER_DIR=$HOME/.casher && mkdir -p $CASHER_DIR/bin && curl #{CASHER_URL} -s -o #{CASHER_BIN} && chmod +x #{CASHER_BIN} && echo casher successfuly downloaded }.compact code = env.ssh.spawn cmd, &env.job.method(:add_trace_to_output) code == 0 end end end end end