This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
forked from candlepin/rho
-
Notifications
You must be signed in to change notification settings - Fork 7
/
utilities.py
311 lines (249 loc) · 9.89 KB
/
utilities.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#
# Copyright (c) 2009-2016 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://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
""" A set of reusable methods common to many of the commands """
from __future__ import print_function
import os
import sys
import re
import csv
import tempfile
from shutil import move
import sh
from rho.translation import _
if sys.version_info > (3,):
import _thread
else:
import thread as _thread # pylint: disable=import-error
CREDENTIALS_PATH = 'data/credentials'
PROFILES_PATH = 'data/profiles'
PLAYBOOK_DEV_PATH = 'rho_playbook.yml'
PLAYBOOK_RPM_PATH = '/usr/share/ansible/rho/rho_playbook.yml'
PASSWORD_MASKING = '********'
SENSITIVE_FACTS_TUPLE = ('connection.host',
'connection.port',
'uname.all',
'uname.hostname')
CONNECTION_FACTS_TUPLE = ('connection.host',
'connection.port',
'connection.uuid')
UNAME_FACTS_TUPLE = ('uname.os',
'uname.hostname',
'uname.processor',
'uname.kernel',
'uname.all',
'uname.hardware_platform')
REDHAT_RELEASE_FACTS_TUPLE = ('redhat-release.name',
'redhat-release.version',
'redhat-release.release')
INSTNUM_FACTS_TUPLE = ('instnum.instnum',)
SYSID_FACTS_TUPLE = ('systemid.system_id',
'systemid.username')
CPU_FACTS_TUPLE = ('cpu.count',
'cpu.socket_count',
'cpu.vendor_id',
'cpu.bogomips',
'cpu.cpu_family',
'cpu.model_name',
'cpu.model_ver')
ETC_RELEASE_FACTS_TUPLE = ('etc_release.name',
'etc_release.version',
'etc_release.release')
ETC_ISSUE_FACTS_TUPLE = ('etc-issue.etc-issue',)
DMI_FACTS_TUPLE = ('dmi.bios-vendor',
'dmi.bios-version',
'dmi.system-manufacturer',
'dmi.processor-family')
VIRT_FACTS_TUPLE = ('virt.virt',
'virt.type',
'virt.num_guests',
'virt.num_running_guests')
RH_PKG_FACTS_TUPLE = ('redhat-packages.is_redhat',
'redhat-packages.num_rh_packages',
'redhat-packages.num_installed_packages',
'redhat-packages.last_installed',
'redhat-packages.last_built')
VIRT_WHAT_FACTS_TUPLE = ('virt-what.type',)
DATE_FACTS_TUPLE = ('date.date',
'date.anaconda_log',
'date.machine_id',
'date.filesystem_create',
'date.yum_history')
SUBMAN_FACTS_TUPLE = ('subman.cpu.core(s)_per_socket',
'subman.cpu.cpu(s)',
'subman.cpu.cpu_socket(s)',
'subman.virt.host_type',
'subman.virt.is_guest',
'subman.virt.uuid',
'subman.has_facts_file')
JBOSS_FACTS_TUPLE = ('jboss.installed-versions',
'jboss.deploy-dates',
'jboss.running-versions')
BRMS_FACTS_TUPLE = ('jboss.brms.kie-api-ver',
'jboss.brms.drools-core-ver',
'jboss.brms.kie-war-ver')
FUSE_FACTS_TUPLE = ('jboss.fuse.activemq-ver',
'jboss.fuse.camel-ver',
'jboss.fuse.cxf-ver')
DEFAULT_FACTS_TUPLE = SUBMAN_FACTS_TUPLE + DATE_FACTS_TUPLE \
+ VIRT_WHAT_FACTS_TUPLE + RH_PKG_FACTS_TUPLE + VIRT_FACTS_TUPLE \
+ DMI_FACTS_TUPLE + ETC_ISSUE_FACTS_TUPLE + ETC_RELEASE_FACTS_TUPLE \
+ CPU_FACTS_TUPLE + SYSID_FACTS_TUPLE + INSTNUM_FACTS_TUPLE \
+ REDHAT_RELEASE_FACTS_TUPLE + UNAME_FACTS_TUPLE \
+ JBOSS_FACTS_TUPLE + BRMS_FACTS_TUPLE + FUSE_FACTS_TUPLE \
+ CONNECTION_FACTS_TUPLE
def threaded_tailing(path, ansible_verbosity=0):
"""Follow and provide output using a thread
:param path: path to file to follow
:param ansible_verbosity: the verbosity level
"""
_thread.start_new_thread(tail_and_follow, (path, ansible_verbosity))
def tail_and_follow(path, ansible_verbosity):
"""Follow and provide output
:param path: tuple containing thepath to file to follow
:param ansible_verbosity: the verbosity level
"""
if len(path) > 0: # pylint: disable=len-as-condition
truncate = 1
if ansible_verbosity:
truncate = ansible_verbosity
print_line = truncate
plabook_started = False
truncated = False
# pylint: disable=no-member
for line in sh.tail('-f', '-n', '+0', path, _iter=True):
line = line.strip('\n')
if line.startswith('TASK') or line.startswith('PLAY'):
print(line)
print_line = truncate
plabook_started = True
truncated = False
elif print_line > 0:
line_len = len(line)
char_truncate = truncate * 100
if line_len > char_truncate:
print(line[0:char_truncate] + '...')
else:
print(line)
print_line = print_line - 1
elif print_line == 0 and not truncated and plabook_started:
print(_('-- output truncated --'))
truncated = True
def ensure_config_dir_exists():
"""Ensure the Rho configuration directory exists."""
if not os.path.exists('data'):
os.makedirs('data')
# pylint: disable=unused-argument
def multi_arg(option, opt_str, value, parser):
"""Call back function for arg-parse
for when arguments are multiple
:param option: The option
:param opt_str: The option string
:param value: The value
:param parser: The parser for handling the option
"""
args = []
for arg in parser.rargs:
if arg[0] != "-":
args.append(arg)
else:
del parser.rargs[:len(args)]
break
if getattr(parser.values, option.dest):
args.extend(getattr(parser.values, option.dest))
setattr(parser.values, option.dest, args)
# Read in a file and make it a list
def _read_in_file(filename):
result = None
hosts = None
try:
hosts = open(os.path.expanduser(os.path.expandvars(filename)), "r")
result = hosts.read().splitlines()
hosts.close()
except EnvironmentError as err:
sys.stderr.write('Error reading from %s: %s\n' % (filename, err))
hosts.close()
return result
# Makes sure the hosts passed in are in a format Ansible
# understands.
def _check_range_validity(range_list):
# pylint: disable=anomalous-backslash-in-string
regex_list = ['www\[[0-9]*:[0-9]*\].[a-z]*.[a-z]*',
'[a-z]*-\[[a-z]*:[a-z]*\].[a-z]*.[a-z]*',
'[0-9]*.[0-9]*.[0-9]'
'*.\[[0-9]*:[0-9]*\]',
'^(([0-9]|[1-9][0-9]|1[0-9]'
'{2}|2[0-4][0-9]|25[0-5])\.)'
'{3}']
for reg_item in range_list:
match = False
for reg in regex_list:
if re.match(reg, reg_item):
match = True
if not match:
if len(reg_item) <= 1:
print(_("No such hosts file."))
print(_("Bad host name/range : '%s'") % reg_item)
sys.exit(1)
def validate_port(arg):
"""Check that arg is a valid port.
:param arg: either a string or an integer.
:returns: The arg, as an integer.
:raises: ValueError, if arg is not a valid port.
"""
if isinstance(arg, str):
try:
arg = int(arg)
except ValueError:
raise ValueError('Port value %s'
' should be a positive integer'
' in the valid range (0-65535)' % arg)
elif not isinstance(arg, int):
raise ValueError('Port value %s should be a positive integer'
' in the valid range (0-65535)' % arg)
# We need to support both system and user ports (see
# https://en.wikipedia.org/wiki/Registered_port) because we don't
# know how the user will have configured their system.
if arg < 0 or arg > 65535:
raise ValueError('Port value %s should be a positive integer'
' in the valid range (0-65535)' % arg)
return arg
def str_to_ascii(in_string):
""" Coverts unicode string to ascii string
:param in_string: input string to convert to ascii
:returns: ASCII encoded string
"""
if sys.version_info.major == 2:
return in_string.decode('utf-8').encode('ascii')
return in_string
def iteritems(dictionary):
"""Iterate over a dictionary's (key, value) pairs using Python 2 or 3.
:param dictionary: the dictionary to iterate over.
"""
if sys.version_info.major == 2:
return dictionary.iteritems()
return dictionary.items()
def write_csv_data(keys, data, path):
""" Write csv data with input fieldnames a dictionary of data and
the file path to write to.
:param keys: The field names and keys of the dictionary
:param data: The dictionary of data to convert to csv
:param path: The file path to write to
"""
with tempfile.NamedTemporaryFile(mode='w', delete=False) as data_temp:
# Construct the CSV writer
writer = csv.DictWriter(
data_temp, fieldnames=sorted(keys), delimiter=',')
# Write a CSV header if necessary
writer.writeheader()
# Write the data
for row in data:
writer.writerow(row)
data_temp.close()
move(data_temp.name, path)