Sha256: 95566c727261b90260e5a9d28848a76d2dd56210ad9b037d2ecaa3c546491bba

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 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 = agent.check_status
        if should_run?(action)
          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 run_command(branch)
      cmd = if run_options[:verbose]
        config.verbose_command
      else
        config.command
      end
      cmd.gsub('@@branch@@', branch)
    end

    def should_run?(action)
      ## always run if started without --try
      (!run_options[:try] or action == 'run')
    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

4 entries across 4 versions & 1 rubygems

Version Path
runpuppet-1.0.0 lib/runpuppet/client.rb
runpuppet-1.0.0.rc6 lib/runpuppet/client.rb
runpuppet-1.0.0.rc5 lib/runpuppet/client.rb
runpuppet-1.0.0.rc4 lib/runpuppet/client.rb