forked from redhat-openstack/packstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessors.py
More file actions
159 lines (135 loc) · 5.14 KB
/
Copy pathprocessors.py
File metadata and controls
159 lines (135 loc) · 5.14 KB
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
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import netaddr
import os
import uuid
from .utils import force_ip
from .utils import ScriptRunner
from .exceptions import NetworkError
from .exceptions import ParamProcessingError
__all__ = ('ParamProcessingError', 'process_cidr', 'process_host',
'process_ssh_key', 'process_add_quotes_around_values',
'process_password', 'process_string_nofloat', 'process_bool')
def process_cidr(param, param_name, config=None):
"""
Corrects given CIDR if necessary.
"""
if '/' not in param:
# we need to skip this if single IP address has been given
return param
try:
return str(netaddr.IPNetwork(param).cidr)
except Exception as ex:
raise ParamProcessingError(str(ex))
def process_host(param, param_name, config=None):
"""
Tries to change given parameter to IP address, if it is in hostname
format
"""
try:
return force_ip(param, allow_localhost=True)
except NetworkError as ex:
raise ParamProcessingError(str(ex))
def process_ssh_key(param, param_name, config=None):
"""
Generates SSH key if given key in param doesn't exist. In case param
is an empty string it generates default SSH key ($HOME/.ssh/id_rsa).
"""
def create_key(path):
# make path absolute
path = os.path.expanduser(path)
path = os.path.abspath(path)
# create new ssh key
local = ScriptRunner()
local.append('ssh-keygen -f "%s" -N ""' % path)
local.execute()
if not param:
key_file = '%s/.ssh/id_rsa' % os.environ["HOME"]
param = '%s.pub' % key_file
if not os.path.isfile(param):
create_key(key_file)
elif not os.path.isfile(param):
key_file = param.endswith('.pub') and param[:-4] or param
param = param.endswith('.pub') and param or ('%s.pub' % param)
create_key(key_file)
return param
def process_add_quotes_around_values(param, param_name, config=None):
"""
Add a single quote character around each element of a comma
separated list of values
"""
params_list = param.split(',')
for index, elem in enumerate(params_list):
if not elem.startswith("'"):
elem = "'" + elem
if not elem.endswith("'"):
elem = elem + "'"
params_list[index] = elem
param = ','.join(params_list)
return param
def process_password(param, param_name, config=None):
"""
Process passwords, checking the following:
1- If there is a user-entered password, use it
2- Otherwise, check for a global default password, and use it if available
3- As a last resort, generate a random password
"""
if not hasattr(process_password, "pw_dict"):
process_password.pw_dict = {}
if param == "PW_PLACEHOLDER":
if config["CONFIG_DEFAULT_PASSWORD"] != "":
param = config["CONFIG_DEFAULT_PASSWORD"]
else:
# We need to make sure we store the random password we provide
# and return it once we are asked for it again
if param_name.endswith("_CONFIRMED"):
unconfirmed_param = param_name[:-10]
if unconfirmed_param in process_password.pw_dict:
param = process_password.pw_dict[unconfirmed_param]
else:
param = uuid.uuid4().hex[:16]
process_password.pw_dict[unconfirmed_param] = param
elif param_name not in process_password.pw_dict:
param = uuid.uuid4().hex[:16]
process_password.pw_dict[param_name] = param
else:
param = process_password.pw_dict[param_name]
return param
def process_heat(param, param_name, config=None):
if config["CONFIG_SAHARA_INSTALL"] == 'y':
param = 'y'
return param
def process_string_nofloat(param, param_name, config=None):
"""
Process a string, making sure it is *not* convertible into a float
If it is, change it into a random 16 char string, and check again
"""
while True:
try:
float(param)
except ValueError:
return param
else:
param = uuid.uuid4().hex[:16]
def process_bool(param, param_name, config=None):
"""Converts param to appropriate boolean representation.
Retunrs True if answer == y|yes|true, False if answer == n|no|false.
"""
if param.lower() in ('y', 'yes', 'true'):
return True
elif param.lower() in ('n', 'no', 'false'):
return False
# Define silent processors
for proc_func in (process_bool, process_add_quotes_around_values):
proc_func.silent = True