Class: Runpuppet::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Config

Returns a new instance of Config.



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

def initialize(config_path)
  @config_path = config_path
  load_data
end

Instance Attribute Details

#config_pathObject

Returns the value of attribute config_path.



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

def config_path
  @config_path
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.find_system_configObject



16
17
18
19
20
21
22
23
24
# File 'lib/runpuppet/config.rb', line 16

def self.find_system_config
  home = File.expand_path('~')
  pathes = ["#{home}/.runpuppet.yml", '/etc/runpuppet.yml']
  pathes.each do |file|
    return Runpuppet::Config.new(file) if File.exist?(file)
  end
  puts "No runpuppet.yml found in #{pathes.join(' or ')}"
  exit
end

Instance Method Details

#commandObject



44
45
46
47
# File 'lib/runpuppet/config.rb', line 44

def command
  # "cd /etc/puppet/repo && git fetch && git checkout -f origin/@@branch@@ && puppet -v --debug --logdest console --modulepath /etc/puppet/repo/modules /etc/puppet/repo/manifests/site.pp"
  data['command']
end

#default_branchObject



57
58
59
# File 'lib/runpuppet/config.rb', line 57

def default_branch
  data['branch'] || 'master'
end

#extract_auth_from_url!(url) ⇒ Object



34
35
36
37
38
# File 'lib/runpuppet/config.rb', line 34

def extract_auth_from_url!(url)
  url.sub!(%r{//(.*?):(.*?)@}, '//')
  auth = [$1, $2].compact
  auth.empty? ? nil : auth
end

#hostnameObject



76
77
78
# File 'lib/runpuppet/config.rb', line 76

def hostname
  Socket.gethostname
end

#is_ec2?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/runpuppet/config.rb', line 86

def is_ec2?
  read_shell('arp -n -i eth0') =~ /fe:ff:ff:ff:ff/
end

#load_dataObject



11
12
13
14
# File 'lib/runpuppet/config.rb', line 11

def load_data
  raise "No runpuppet.yml found in #{config_path}" unless File.exist?(config_path)
  @data = YAML.load(File.read(config_path))
end

#local_ipObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/runpuppet/config.rb', line 61

def local_ip
  if is_ec2?
    require 'facter'
    require 'facter/ec2'
    ip = Facter.value("ec2_public_ipv4")
  elsif data['local_ip_interface']
    parse_local_ip_from_interface
  else
    ifconfig = read_shell('ifconfig')
    data['local_ip'] or
      ifconfig[/192.168.\d+.\d+/] or
      ifconfig[/10.10.\d+.\d+/]
  end
end

#lock_fileObject



40
41
42
# File 'lib/runpuppet/config.rb', line 40

def lock_file
  data['lock_file'] || '/tmp/runpuppet.lock'
end

#parse_local_ip_from_interfaceObject



80
81
82
83
84
# File 'lib/runpuppet/config.rb', line 80

def parse_local_ip_from_interface
  cmd = "ip addr list #{data['local_ip_interface']}"
  read_shell(cmd).match(/inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?/)
  return $1
end

#puppet_controller_authObject



30
31
32
# File 'lib/runpuppet/config.rb', line 30

def puppet_controller_auth
  extract_auth_from_url!(puppet_controller_url)
end

#puppet_controller_urlObject



26
27
28
# File 'lib/runpuppet/config.rb', line 26

def puppet_controller_url
  data['puppet_controller_url'].sub(%r{/$},'')
end

#read_shell(cmd) ⇒ Object



90
91
92
# File 'lib/runpuppet/config.rb', line 90

def read_shell(cmd)
  %x(#{cmd})
end

#verbose_commandObject



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

def verbose_command
  unless data['verbose_command']
    puts "No verbose_command param in #{config_path} found! using normal command."
    return self.command
  end
  data['verbose_command']
end