Class: Runpuppet::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/runpuppet/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
# File 'lib/runpuppet/client.rb', line 7

def initialize(context)
  @config      = context.config
  @run_options = context.run_options
  @agent       = context.agent
  @logger      = context.logger
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



5
6
7
# File 'lib/runpuppet/client.rb', line 5

def agent
  @agent
end

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/runpuppet/client.rb', line 3

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/runpuppet/client.rb', line 6

def logger
  @logger
end

#run_optionsObject

Returns the value of attribute run_options.



4
5
6
# File 'lib/runpuppet/client.rb', line 4

def run_options
  @run_options
end

Instance Method Details

#calculate_run_paramsObject



33
34
35
# File 'lib/runpuppet/client.rb', line 33

def calculate_run_params
  (local_run_params || remote_run_params)
end

#local_run_paramsObject



37
38
39
40
# File 'lib/runpuppet/client.rb', line 37

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

#log(msg) ⇒ Object



81
82
83
# File 'lib/runpuppet/client.rb', line 81

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

#remote_run_paramsObject



42
43
44
# File 'lib/runpuppet/client.rb', line 42

def remote_run_params
  agent.check_status
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/runpuppet/client.rb', line 14

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

#run_command(branch) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/runpuppet/client.rb', line 46

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

#sh(cmd) ⇒ Object

simplified copy of rake`s sh



71
72
73
74
75
76
77
78
79
# File 'lib/runpuppet/client.rb', line 71

def sh(cmd)
  puts cmd
  IO.popen(cmd) do |pipe|
    while str = pipe.gets
      puts str
    end
  end
  $?.success?
end

#with_lock(lock_file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/runpuppet/client.rb', line 55

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