This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runpuppet
executable file
·132 lines (118 loc) · 4.5 KB
/
runpuppet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env ruby
require 'fileutils'
class RunPuppet
PUPPET_MIN_VERSION = '3.5.1'
LIBRARIAN_MIN_VERSION= '1.3.1'
LOG_DIR = '/var/log/puppet'
PUPPET_DIR=File.expand_path(File.dirname(__FILE__))
DEFAULT_PUPPET_CONF = "[main]
confdir=#{PUPPET_DIR}
"
def system(cmd)
puts cmd
unless Kernel.system(cmd)
puts "Running cmd #{cmd} failed"
exit 2
end
end
def init
puts "PUPPET_DIR is #{PUPPET_DIR}"
end
def checkVersionsOfNeededStuff
gem_version = `gem --version`.chomp
unless /^2/.match(gem_version) or /^1\.8/.match(gem_version)
puts "Must install at least gem >= 1.8 (is #{gem_version})"
exit 2
end
# verify some stuff which must be installed before launching
info = `puppet --version`
version = Gem::Version.new(info)
puts version
unless version >= Gem::Version.new(PUPPET_MIN_VERSION)
# we need augeas >= 1.0 for smb.conf read only to work correctly
puts "Must install first puppet >= #{PUPPET_MIN_VERSION}"
puts "look at shell/install_puppet.sh to see how"
else
puts "Found puppet with version #{version}"
end
version = `librarian-puppet version`.chomp
version = version.gsub(/[A-Z\-\s]+/i,'')
version = Gem::Version.new(version)
unless version >= Gem::Version.new(LIBRARIAN_MIN_VERSION)
puts "Must install librarian-puppet >= #{LIBRARIAN_MIN_VERSION}"
puts('gem install --no-ri --no-rdoc librarian-puppet')
exit 2
else
puts "Found librarian-puppet with version #{version}"
end
end
def checkPuppetConfiguration
confFile = File.join(PUPPET_DIR, 'puppet.conf')
puts "Creating default confFile #{confFile}"
File.open(confFile, 'w+') {|f| f.write DEFAULT_PUPPET_CONF }
end
# default configuration for hiera.yaml
# This is not easy and querying the standalone hiera tool for a value will often result in a different value as e.g. you use by default /etc/hiera.yaml and
# not /etc/puppet/hiera.yaml when calling from puppet
# Also it is important that we can use different environments for testing and production (default)
# it is recommended to generate a link for your different environments to something outside this git clone
# e.g ln -s /etc/puppet/hieradata/production /etc/hieradata/production and leave your stuff there
# not using http://docs.puppetlabs.com/puppet/latest/reference/environments.html (as per 17.07.2014 till puppet 4 is out and the problems with hiera are resolved
def checkHieraConfiguration
@defaultHieraConfig =
%(# default configuration written by #{File.expand_path(__FILE__)} with default values for demo of elexis-vagrant
# http://www.glennposton.com/posts/puppet_best_practices__environment_specific_configs
---
:backends:
- yaml
:yaml:
:datadir: #{PUPPET_DIR}/hieradata
:hierarchy:
- '%{::environment}/%{::fqdn}'
- '%{::environment}/%{calling_module}'
- '%{::environment}/%{::environment}'
- 'common/%{calling_module}'
- common
)
@hiera_default_file = File.join(PUPPET_DIR, 'hiera.yaml')
unless File.exists?(@hiera_default_file)
puts "Creating #{@hiera_default_file} with default values for demo of elexis-vagrant"
File.open(@hiera_default_file, 'w+') { |f| f.write @defaultHieraConfig }
end
['/etc/hiera.yaml',
@hiera_default_file,
].each { |candidate|
if File.exists?(candidate)
@hiera_config = candidate
break
end
}
@hiera = ARGV.index("-e")
@hiera ||= " --hiera_config #{@hiera_default_file}"
puts "Using hieraFile #{@hiera} @hiera_default_file #{@hiera_default_file}"
end
public
def run
startTime = Time.now
Dir.chdir(PUPPET_DIR)
checkPuppetConfiguration
checkHieraConfiguration
checkVersionsOfNeededStuff
if File.writable?("#{PUPPET_DIR}/modules")
system('librarian-puppet install')
else
puts "As we cannot write to #{PUPPET_DIR}/modules we assume that you have updated it via another mechanism"
end
FileUtils.makedirs(LOG_DIR, :verbose => true) unless File.directory?(LOG_DIR)
logFile = File.join(LOG_DIR, Time.now.strftime('%Y-%m-%d_%H:%M:%S')+ '.log')
# system("apt-get upgrade --quiet --yes")
cmd = "puppet apply --confdir #{PUPPET_DIR} --modulepath #{PUPPET_DIR}/modules #{@hiera} #{ARGV.join(' ')} 2>&1 | tee #{logFile}"
system(cmd)
endTime = Time.now
diff = (endTime-startTime).to_i
puts " cmd was: #{cmd}"
puts "Running #{__FILE__} #{ARGV.join(' ')} took #{diff} seconds. Details see #{logFile}"
end
end
runner = RunPuppet.new
runner.run