-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbackup_foreman
executable file
·255 lines (214 loc) · 9.53 KB
/
backup_foreman
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Backup Foreman configuration
"""
import sys
import getopt
import os
import yaml
from foreman.foreman import *
def ensure_dir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
def remove_keys_from_dict(keys, data):
for key in keys:
if key in data:
data.pop(key)
return data
def clear_data(data, invalid_keys):
if isinstance(data, list):
for i in range(len(data)):
data[i] = clear_data(data=data[i], invalid_keys=invalid_keys)
elif isinstance(data, dict):
data = remove_keys_from_dict(keys=invalid_keys, data=data)
for key in data:
data[key] = clear_data(data[key], invalid_keys=invalid_keys)
return data
class ForemanBackup:
def __init__(self, **kwargs):
self.foreman = Foreman(kwargs.get('hostname'),
kwargs.get('port'),
kwargs.get('username'),
kwargs.get('password'),
string2bool(kwargs.get('ssl')))
self.backup_dir = kwargs.get('backup_dir', '.')
self.katello_support = kwargs.get('katello_support', False)
def get_resources(self, type, resource_function):
result = list()
try:
resources = resource_function()
for i in range(len(resources)):
item = resources[i]
if 'id' in item:
try:
resource = self.get_resource(type=type, id=resources[i].get('id'))
except ForemanError as e:
# There seems to be a bug in Foreman 1.7.3 whereas the API reports 404
# while executing a get request on organizations/:id
# API: http://theforeman.org/api/apidoc/v2/organizations/show.html
if e.status_code == 404:
resource = item
else:
resource = item
result.append(resource)
return result
except ForemanError as e:
print('Error on getting resource {0}'.format(e.message))
exit(1)
def get_resource(self, type, id):
return self.foreman.get_resource(resource_type=type, resource_id=id)
def write_resources(self, type, items, backup_dir):
"""Backup Foreman type as YAML file into a directory.
A new directory named <type> will be created inside <backup_dir>. Each
resource fetched by <resource_function> will be saved in an own YAML file
called <resource_name>.yaml in <backup_dir>/<type>.
Args:
backup_dir (str): Directory where to create the backup files
type (str): Name of the resource to backup (e.g. 'architectures')
resource_function (def): Name of function to call to get a dict of resources
"""
backup_dir = os.path.join(self.backup_dir, type)
ensure_dir(dir=backup_dir)
for resource in items:
if 'title' in resource:
file_name = resource.get('title')
elif 'login' in resource:
file_name = resource.get('login')
elif 'name' in resource:
file_name = resource.get('name')
else:
print('Can\'t backup {0}'.format(resource))
continue
backup_file_name = os.path.join(backup_dir, '{name}'.format(name=file_name.replace('/', '_') + '.yaml'))
with open(backup_file_name, 'w') as backup_file:
yaml.safe_dump(resource, backup_file, default_flow_style=False, encoding='utf-8', allow_unicode=True)
def backup(self, resource_type, resource_function):
resources = self.get_resources(type=resource_type, resource_function=resource_function)
print('Backing up {count} {resource_type}'.format(count=str(len(resources)), resource_type=resource_type))
self.write_resources(type=resource_type, items=resources, backup_dir=self.backup_dir)
def run(self):
ensure_dir(self.backup_dir)
self.backup_resources()
def backup_resources(self):
self.backup(resource_type='architectures',
resource_function=self.foreman.get_architectures)
self.backup(resource_type='common_parameters',
resource_function=self.foreman.get_common_parameters)
self.backup(resource_type='compute_resources',
resource_function=self.foreman.get_compute_resources)
self.backup(resource_type='compute_profiles',
resource_function=self.foreman.get_compute_profiles)
self.backup(resource_type='config_templates',
resource_function=self.foreman.get_config_templates)
self.backup(resource_type='domains',
resource_function=self.foreman.get_domains)
self.backup(resource_type='environments',
resource_function=self.foreman.get_environments)
self.backup(resource_type='hosts',
resource_function=self.foreman.get_hosts)
self.backup(resource_type='hostgroups',
resource_function=self.foreman.get_hostgroups)
self.backup(resource_type='media',
resource_function=self.foreman.get_media)
self.backup(resource_type='operatingsystems',
resource_function=self.foreman.get_operatingsystems)
self.backup(resource_type='ptables',
resource_function=self.foreman.get_partition_tables)
self.backup(resource_type='roles',
resource_function=self.foreman.get_roles)
self.backup(resource_type='smart_proxies',
resource_function=self.foreman.get_smart_proxies)
self.backup(resource_type='subnets',
resource_function=self.foreman.get_subnets)
self.backup(resource_type='users',
resource_function=self.foreman.get_users)
self.backup(resource_type='auth_source_ldaps',
resource_function=self.foreman.get_auth_source_ldaps)
if self.katello_support:
self.backup(resource_type='locations',
resource_function=self.foreman.get_locations)
self.backup(resource_type='organizations',
resource_function=self.foreman.get_organizations)
class AnsibleBackup(ForemanBackup):
invalid_keys = ['created_at', 'updated_at', 'id']
def __init__(self, **kwargs):
ForemanBackup.__init__(self, **kwargs)
def write_resources(self, type, items, backup_dir):
backup_file_name = os.path.join(backup_dir, type)
resources = list()
for i in range(len(items)):
item = self.get_resource(type=type, id=items[i].get('id'))
item = clear_data(item, invalid_keys=self.invalid_keys)
item['state'] = 'present'
resources.append(item)
with open(backup_file_name, 'w') as f:
f.write('---\n')
f.write('foreman_{resource}:'.format(resource=type))
if len(resources) > 0:
f.write('\n')
else:
f.write(' ')
with open(backup_file_name, 'a') as f:
yaml.safe_dump(resources, f, default_flow_style=False, encoding='utf-8', allow_unicode=True)
def show_help():
"""Print on screen how to use this script.
"""
print('foreman.py -f <foreman_host> -p <port> -u <username> -s <secret> -S <ssl>')
def string2bool(s):
"""
:rtype : bool
"""
return str(s).lower() in ['true', 'yes', '1', 'enable']
def main(argv):
""" Main
Backup Foreman resources
"""
foreman_host = os.environ.get('FOREMAN_HOST', '127.0.0.1')
foreman_port = os.environ.get('FOREMAN_PORT', '443')
foreman_user = os.environ.get('FOREMAN_USER', 'foreman')
foreman_pass = os.environ.get('FOREMAN_PASS', 'changme')
foreman_ssl = string2bool(os.environ.get('FOREMAN_SSL', True))
ansible_format = os.environ.get('FOREMAN_BACKUP_ANSIBLE_FORMAT', False)
backup_dir = os.environ.get('FOREMAN_BACKUP_DIR', '.')
katello_support = string2bool(os.environ.get('FOREMAN_KATELLO_SUPPORT', False))
try:
opts, args = getopt.getopt(argv,
"ab:f:hu:p:s:S:k",
["foreman=", "username=", "port=", "secret=", "ssl="])
except getopt.GetoptError:
show_help()
sys.exit(2)
for opt, arg in opts:
if opt in ('-f', '--foreman'):
foreman_host = arg
elif opt == '-h':
show_help()
sys.exit()
elif opt == '-k':
katello_support = True
elif opt == '-a':
ansible_format = True
elif opt == '-b':
backup_dir = arg
elif opt in ('-u', '--username'):
foreman_user = arg
elif opt in ('-p', '--port'):
foreman_port = arg
elif opt in ('-s', '--secret'):
foreman_pass = arg
elif opt in ('-S', '--ssl'):
foreman_ssl = arg
if ansible_format:
backup_class = AnsibleBackup
else:
backup_class = ForemanBackup
backup = backup_class(hostname=foreman_host,
port=foreman_port,
username=foreman_user,
password=foreman_pass,
ssl=foreman_ssl,
katello_support=katello_support,
backup_dir=backup_dir)
backup.run()
if __name__ == '__main__':
main(sys.argv[1:])