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
204 lines (164 loc) · 6.61 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
#
# 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
from rho.translation import _
CREDENTIALS_PATH = 'data/credentials'
PROFILES_PATH = 'data/profiles'
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 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):
arg = int(arg)
elif not isinstance(arg, int):
raise ValueError('Port %s not of recognized type '
'(should be string or int)' % 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 %s not in 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
"""
return in_string.decode('utf-8').encode('ascii')