Class: Runpuppet

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

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Class Method Details

.get(path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/runpuppet.rb', line 35

def self.get(path)
  path += (path.include?('?') ? '&' : '?')
  path += "hostname=#{Socket.gethostname}&ip=#{Config.local_ip}"
  begin
    open(Config.puppet_controller_url + path, {:http_basic_authentication =>Config.puppet_controller_auth} )
  rescue Exception => e
    puts e.inspect
    puts "WARNING: error connecting to puppet controller!"
  end
end

.post(path, params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/runpuppet.rb', line 47

def self.post(path, params)
  params[:hostname] = Socket.gethostname
  params[:ip] = Config.local_ip
  begin
    RestClient.post "#{Config.puppet_controller_url}/puppet/#{path}", params
  rescue Exception => e
    puts e.inspect
    puts "WARNING: error connecting to puppet controller!"
  end
end

.report_facts(options = {}) ⇒ Object



82
83
84
85
86
# File 'lib/runpuppet.rb', line 82

def self.report_facts(options={})
  require 'facter/application'
  facts = Facter::Application.run([])
  post('facts', :facts => facts.to_hash)
end

.run(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/runpuppet.rb', line 59

def self.run(options={})
  with_lock(Config.lock_file) do
    time = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S")

    todo, branch = get("/puppet/run").read.split('-') rescue []
    if todo == 'run' or !options[:try] # try allows not running
      get '/puppet/status?status=started'
      branch ||= Config.default_branch
      cmd = Config.command.gsub('@@branch@@', branch)
      puts "#{time}: run #{branch}"

      if sh(cmd)
        get '/puppet/status?status=finished'
      else
        get '/puppet/status?status=error'
        exit 2
      end
    else
      puts "#{time}: nothing to do"
    end
  end
end

.sh(cmd) ⇒ Object

simplified copy of rake`s sh



24
25
26
27
28
29
30
31
32
# File 'lib/runpuppet.rb', line 24

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

.with_lock(lock_file) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/runpuppet.rb', line 8

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

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