-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpuppet-run
executable file
·145 lines (113 loc) · 3.49 KB
/
puppet-run
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'date'
require 'fileutils'
OPT_PUPPET_BIN = '/opt/puppetlabs/puppet/bin'
GEM_PATH = '/opt/puppetlabs/gem'
ROOT_PATH = '/opt/halyard'
LOCKDIR_NAME = '.lock'
PAUSEDIR_NAME = '.pause'
PUPPET_ENV = ENV['PUPPET_ENV'] || 'production'
PUPPET_DEBUG = ENV['PUPPET_DEBUG'] ? true : false
PUPPET_PROFILE = ENV['PUPPET_PROFILE'] ? true : false
HALYARD_DEBUG = ENV['HALYARD_DEBUG'] ? true : false
ENV['PATH'] = "#{OPT_PUPPET_BIN}:#{ENV['PATH']}"
ENV['AWS_CONFIG_FILE'] = '/root/.aws/credentials'
ENV['GEM_PATH'] = "#{GEM_PATH}:#{`gem env path`.chomp}"
ENV['GEM_HOME'] = GEM_PATH
LOG_FILE = "#{ROOT_PATH}/logs/puppet-run.#{Date.today.strftime '%Y%m%d'}"
REDIRECTS = HALYARD_DEBUG ? {} : { %i[out err] => '/dev/null' }
FileUtils.mkdir_p "#{ROOT_PATH}/logs"
FileUtils.mkdir_p "#{ROOT_PATH}/environments"
unless Dir.exist? "#{ROOT_PATH}/environments/production"
FileUtils.ln_s "#{ROOT_PATH}/repo", "#{ROOT_PATH}/environments/production"
end
Dir.chdir "#{ROOT_PATH}/repo"
def debug(msg)
stamp = DateTime.now.strftime('%Y-%m-%d %H:%M:%S')
puts("#{stamp} #{msg}") if HALYARD_DEBUG
end
debug('Locking')
begin
FileUtils.mkdir LOCKDIR_NAME
rescue Errno::EEXIST
puts 'It appears a puppet-run is already in progress'
puts "If this is not the case, run: rm -r #{ROOT_PATH}/repo/.lock"
exit 1
end
def clean_exit(code = 1)
FileUtils.rm_r "#{ROOT_PATH}/repo/#{LOCKDIR_NAME}"
exit code
end
Signal.trap('INT') { clean_exit }
Signal.trap('TERM') { clean_exit }
def run_or_fail(cmd, err)
return if system(cmd, REDIRECTS)
puts err
clean_exit
end
if ARGV.first == 'disable'
puts 'Pausing puppet runs'
FileUtils.mkdir_p PAUSEDIR_NAME
clean_exit 0
elsif ARGV.first == 'enable'
puts 'Enabling puppet runs'
FileUtils.rm_rf PAUSEDIR_NAME
clean_exit 0
elsif Dir.exist? PAUSEDIR_NAME
puts 'Puppet runs paused'
clean_exit 1
end
debug('Checking repo')
if `git status -s 2>&1`.empty?
debug('Updating repo')
run_or_fail 'git pull', "Failed to update repo: #{ROOT_PATH}/repo"
run_or_fail 'git submodule update --init', "Failed to update submodules: #{ROOT_PATH}/repo"
else
puts "Repo is unclean: #{ROOT_PATH}/repo"
end
unless File.exist? "#{OPT_PUPPET_BIN}/bundle"
debug('Installing bundler')
FileUtils.mkdir_p OPT_PUPPET_BIN
system('gem', 'install', '--no-user-install', '--no-document', '--bindir', OPT_PUPPET_BIN, 'bundler')
end
debug('Checking bundled gems')
system('bundle check', REDIRECTS) || run_or_fail('bundle install', 'Failed to update bundle')
debug('Purging puppet modules')
run_or_fail('bundle exec r10k puppetfile purge', 'Failed to purge puppet modules') if Dir.exist? 'modules'
debug('Installing puppet modules')
run_or_fail('bundle exec r10k puppetfile install', 'Failed to update puppet modules')
## Logger copies lines to logfile and stdout
class Logger
def initialize
@handle = File.open(LOG_FILE, 'a')
@stream = $stdout
end
def write(line)
stamp = DateTime.now.strftime('%Y-%m-%d %H:%M:%S')
@handle << "#{stamp} #{line}"
@stream << line
end
end
logger = Logger.new
debug('Starting run')
logger.write("STARTING RUN\n")
cmd = [
'bundle',
'exec',
'puppet',
'apply',
"--confdir=#{ROOT_PATH}/repo",
"--environment=#{PUPPET_ENV}",
"#{ROOT_PATH}/environments/#{PUPPET_ENV}/manifests"
]
cmd << '--debug' if PUPPET_DEBUG
cmd << '--profile' if PUPPET_PROFILE
IO.popen(cmd, err: %i[child out]) do |io|
while (line = io.gets)
logger.write(line)
end
end
debug('Ending run')
logger.write("ENDING RUN\n")
clean_exit 0