Class: Runpuppet::Client
- Inherits:
-
Object
- Object
- Runpuppet::Client
- Defined in:
- lib/runpuppet/client.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#config ⇒ Object
Returns the value of attribute config.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#run_options ⇒ Object
Returns the value of attribute run_options.
Instance Method Summary collapse
-
#initialize(context) ⇒ Client
constructor
A new instance of Client.
- #log(msg) ⇒ Object
- #run ⇒ Object
- #run_command(branch) ⇒ Object
-
#sh(cmd) ⇒ Object
simplified copy of rake`s sh.
- #should_run?(action) ⇒ Boolean
- #with_lock(lock_file) ⇒ Object
Constructor Details
#initialize(context) ⇒ Client
7 8 9 10 11 12 |
# File 'lib/runpuppet/client.rb', line 7 def initialize(context) @config = context.config @run_options = context. @agent = context.agent @logger = context.logger end |
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
5 6 7 |
# File 'lib/runpuppet/client.rb', line 5 def agent @agent end |
#config ⇒ Object
Returns the value of attribute config.
3 4 5 |
# File 'lib/runpuppet/client.rb', line 3 def config @config end |
#logger ⇒ Object
Returns the value of attribute logger.
6 7 8 |
# File 'lib/runpuppet/client.rb', line 6 def logger @logger end |
#run_options ⇒ Object
Returns the value of attribute run_options.
4 5 6 |
# File 'lib/runpuppet/client.rb', line 4 def @run_options end |
Instance Method Details
#log(msg) ⇒ Object
73 74 75 |
# File 'lib/runpuppet/client.rb', line 73 def log(msg) logger.log(msg) end |
#run ⇒ Object
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 = 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 |
#run_command(branch) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/runpuppet/client.rb', line 33 def run_command(branch) cmd = if [:verbose] config.verbose_command else config.command end cmd.gsub('@@branch@@', branch) end |
#sh(cmd) ⇒ Object
simplified copy of rake`s sh
63 64 65 66 67 68 69 70 71 |
# File 'lib/runpuppet/client.rb', line 63 def sh(cmd) puts cmd IO.popen(cmd) do |pipe| while str = pipe.gets puts str end end $?.success? end |
#should_run?(action) ⇒ Boolean
42 43 44 45 |
# File 'lib/runpuppet/client.rb', line 42 def should_run?(action) ## always run if started without --try (![:try] or action == 'run') end |
#with_lock(lock_file) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/runpuppet/client.rb', line 47 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 |