forked from vvaldez/astapor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreman-setup.rb
executable file
·158 lines (139 loc) · 4.12 KB
/
foreman-setup.rb
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
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/ruby
#
# Copyright 2013 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http:https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
require 'rubygems'
require 'json'
require 'logger'
require 'rest-client'
require 'securerandom'
# Default
@params_file_name = 'foreman-params.json'
@log_file = STDOUT
def param?(key)
@params.key?(key) && !@params[key].empty?
end
def get_env_id(env)
envs = JSON.load(@client['environments'].get)
env = envs.detect { |e| e['environment'].key?('name') }
env['environment']['id']
end
def get_class(name)
res = @client['puppetclasses'].get(:params => { :search => name })
JSON.load(res)
end
def get_class_ids(classes)
ids = []
classes.each do |puppetclasses|
get_class(puppetclasses).each do |puppetclass|
puppetclass[1].each do |details|
ids << details['puppetclass']['id']
end
end
end
ids
end
def create(cmd, data)
response = @client[cmd].post(data)
rescue RestClient::UnprocessableEntity => e
@log.warn("POST KO: #{data} => #{e.response}")
else
@log.info("POST OK: #{data}")
end
def create_proxy(proxy)
raise 'Incorrect or missing Proxy definitions' unless param?('proxy')
data = { :smart_proxy => { :name => proxy['name'], :url => proxy['host'] } }
create('smart_proxies', data)
end
def create_globals(globals)
raise 'Incorrect or missing Global Parameters definitions' unless param?('globals')
globals.each do |name, value|
data = { :common_parameter => { :name => name, :value => value } }
create('common_parameters', data)
end
end
def create_hostgroups(hostgroups)
raise 'Incorrect or missing Hostgroups definitions' unless param?('hostgroups')
hostgroups.each do |hostgroup|
env_id = get_env_id(hostgroup[1]['environment'])
classes_ids = get_class_ids(hostgroup[1]['puppetclasses'])
create('hostgroups', :hostgroup => {
:name => hostgroup[0],
:environment_id => env_id,
:puppetclass_ids => classes_ids
})
end
end
def create_puppetos(puppetos)
raise 'Incorrect or missing Puppet Modules definitions' unless puppetos
puppetos.each do |e|
options = e['options'] if e.has_key?('options') && !e['options'].empty?
puts "git clone #{options} #{e['source']} #{e['destination']}"
end
end
def gen_password
puts SecureRandom.hex
end
def usage
puts "Usage: #{File.basename($0)} proxy | globals | hostgroups"
puts " Multiple commands can be used at same time"
exit
end
def init
# Logs
@log = Logger.new(@log_file)
@log.datetime_format = "%d/%m/%Y %H:%M:%S"
$DEBUG ? @log.level = Logger::DEBUG : @log.level = Logger::INFO
# JSON parameters
raise "Missing file #{@params_file_name}" if !File.exist?(@params_file_name)
params_file = File.open(@params_file_name)
@params = JSON.load(params_file.read)
# Session
raise 'Incorrect or missing Host definitions' unless param?('host')
@client = RestClient::Resource.new('https://' + @params['host']['name'] + '/api',
:user => @params['host']['user'],
:password => @params['host']['passwd'],
:headers => { :accept => :json })
end
begin
# Options
usage unless ARGV[0]
until ARGV[0] !~ /^-./
case ARGV[0]
when '-c', '--config-file='
ARGV.shift
@params_file_name = ARGV[0]
when '-l', '--log-file='
ARGV.shift
@log_file = ARGV[0]
end
ARGV.shift
end
init
# Command
usage unless ARGV[0]
until ARGV.empty?
case ARGV[0]
when 'proxy'
create_proxy(@params['proxy'])
when 'globals'
create_globals(@params['globals'])
when 'hostgroups'
create_hostgroups(@params['hostgroups'])
when 'password'
gen_password
else
usage
end
ARGV.shift
end
rescue RuntimeError => e
@log.fatal(e)
exit
end