Sha256: 177a0d687ef1ab482f649ff477831137e294be321116b0f39152e00510b831d3

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

module Runpuppet
  class Client
    attr_accessor :config
    attr_accessor :run_options
    attr_accessor :agent
    attr_accessor :logger
    def initialize(context)
      @config      = context.config
      @run_options = context.run_options
      @agent       = context.agent
      @logger      = context.logger
    end

    def run
      with_lock(config.lock_file) do
        action, branch = calculate_run_params
        if action == 'run'
          agent.report_start
          log "run #{branch}"

          if sh(run_command(branch))
            agent.report_success
          else
            agent.report_failure
            exit 2
          end
        else
          log "nothing to do"
        end
      end
    end

    def calculate_run_params
      (local_run_params || remote_run_params)
    end

    def local_run_params
      return nil if run_options[:try]
      return 'run', (run_options[:branch] ||config.default_branch)
    end

    def remote_run_params
      agent.check_status
    end

    def run_command(branch)
      cmd = if run_options[:verbose]
        config.verbose_command
      else
        config.command
      end
      cmd.gsub('@@branch@@', branch)
    end

    def with_lock(lock_file)
      recently_locked = (File.exists?(lock_file) and File.mtime(lock_file) > Time.now - 15*60)

      if recently_locked
        log "can not run, lockfile #{lock_file} exists"
      else
        begin
          `touch #{lock_file}`
          yield
        ensure
          `rm #{lock_file}`
        end
      end
    end

    # simplified copy of rake`s sh
    def sh(cmd)
      puts cmd
      IO.popen(cmd) do |pipe|
        while str = pipe.gets
          puts str
        end
      end
      $?.success?
    end

    def log(msg)
      logger.log(msg)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
runpuppet-1.0.2 lib/runpuppet/client.rb
runpuppet-1.0.1 lib/runpuppet/client.rb