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
|
# Copyright (c) 2018 Cloudbase Solutions Srl
#
# 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 argparse
import json
import os
import uuid
from coriolisclient import constants
def add_storage_mappings_arguments_to_parser(parser):
""" Given an `argparse.ArgumentParser` instance, add the arguments required
for the 'storage_mappings' field for both Migrations and Replicas:
* '--default-storage-backend' will be under 'default_storage_backend'
* '--disk-storage-mapping's will be under 'disk_storage_mappings'
* '--storage-backend-mapping's will be under 'storage_backend_mappings'
"""
parser.add_argument(
"--default-storage-backend",
dest='default_storage_backend',
help="Name of a storage backend on the destination platform to "
"default to using.")
# NOTE: arparse will just call whatever 'type=' was supplied on a value
# so we can pass in a single-arg function to have it modify the value:
def _split_disk_arg(arg):
disk_id, dest = arg.split('=')
return {
"disk_id": disk_id.strip('\'"'),
"destination": dest.strip('\'"')}
parser.add_argument(
"--disk-storage-mapping", action='append', type=_split_disk_arg,
dest='disk_storage_mappings',
help="Mappings between IDs of the source VM's disks and the names of "
"storage backends on the destination platform as seen by running "
"`coriolis endpoint storage list $DEST_ENDPOINT_ID`. "
"Values should be fomatted with '=' (ex: \"id#1=lvm)\"."
"Can be specified multiple times for multiple disks.")
def _split_backend_arg(arg):
src, dest = arg.split('=')
return {
"source": src.strip('\'"'),
"destination": dest.strip('\'"')}
parser.add_argument(
"--storage-backend-mapping", action='append', type=_split_backend_arg,
dest='storage_backend_mappings',
help="Mappings between names of source and destination storage "
"backends as seen by running `coriolis endpoint storage "
"list $DEST_ENDPOINT_ID`. Values should be fomatted with '=' "
"(ex: \"id#1=lvm)\". Can be specified multiple times for "
"multiple backends.")
def get_storage_mappings_dict_from_args(args):
storage_mappings = {}
if args.default_storage_backend:
storage_mappings["default"] = args.default_storage_backend
if args.disk_storage_mappings:
storage_mappings["disk_mappings"] = args.disk_storage_mappings
if args.storage_backend_mappings:
storage_mappings["backend_mappings"] = args.storage_backend_mappings
return storage_mappings
def format_mapping(mapping):
""" Given a str-str mapping, formats it as a string. """
return ", ".join(
["'%s'='%s'" % (k, v) for k, v in mapping.items()])
def parse_storage_mappings(storage_mappings):
""" Given the 'storage_mappings' API field, returns a tuple with the
'default' option, the 'backend_mappings' and 'disk_mappings'.
"""
# NOTE: the 'storage_mappings' property is Nullable:
if storage_mappings is None:
return None, {}, {}
backend_mappings = {
mapping['source']: mapping['destination']
for mapping in storage_mappings.get("backend_mappings", [])}
disk_mappings = {
mapping['disk_id']: mapping['destination']
for mapping in storage_mappings.get("disk_mappings", [])}
return (
storage_mappings.get("default"), backend_mappings, disk_mappings)
def format_json_for_object_property(obj, prop_name):
""" Returns the property given by `prop_name` of the given
API object as a nicely-formatted JSON string (if it exists) """
prop = getattr(obj, prop_name, None)
if prop is None:
# NOTE: return an empty JSON object string to
# clearly-indicate it's a JSON
return "{}"
if not isinstance(prop, dict) and hasattr(prop, 'to_dict'):
prop = prop.to_dict()
return json.dumps(prop, indent=2)
def validate_uuid_string(uuid_obj, uuid_version=4):
""" Checks whether the provided string is a valid UUID string
:param uuid_obj: A string or stringable object containing the UUID
:param uuid_version: The UUID version to be used
"""
uuid_string = str(uuid_obj).lower()
try:
uuid.UUID(uuid_string, version=uuid_version)
except ValueError:
# If it's a value error, then the string
# is not a valid hex code for a UUID.
return False
return True
def add_args_for_json_option_to_parser(parser, option_name):
""" Given an `argparse.ArgumentParser` instance, dynamically add a group of
arguments for the option for both an '--option-name' and
'--option-name-file'.
"""
option_name = option_name.replace('_', '-')
option_label_name = option_name.replace('-', ' ')
arg_group = parser.add_mutually_exclusive_group()
arg_group.add_argument('--%s' % option_name,
help='JSON encoded %s data' % option_label_name)
arg_group.add_argument('--%s-file' % option_name,
type=argparse.FileType('r'),
help='Relative/full path to a file containing the '
'%s data in JSON format' % option_label_name)
return parser
def get_option_value_from_args(args, option_name, error_on_no_value=True):
""" Returns a dict with the value from of the option from the given
arguments as set up by calling `add_args_for_json_option_to_parser`
('--option-name' and '--option-name-file')
"""
value = None
raw_value = None
option_name = option_name.replace('-', '_')
option_label_name = option_name.replace('_', ' ')
option_file_name = "%s_file" % option_name
option_arg_name = "--%s" % option_name.replace('_', '-')
raw_arg = getattr(args, option_name)
file_arg = getattr(args, option_file_name)
if raw_arg:
raw_value = raw_arg
elif file_arg:
with file_arg as fin:
raw_value = fin.read()
if not value and raw_value:
try:
value = json.loads(raw_value)
except ValueError as ex:
raise ValueError(
"Error while parsing %s JSON: %s" % (
option_label_name, str(ex)))
if not value and error_on_no_value:
raise ValueError(
"No '%s[-file]' parameter was provided." % option_arg_name)
return value
def compose_user_scripts(global_scripts, instance_scripts):
ret = {
"global": {},
"instances": {}
}
global_scripts = global_scripts or []
instance_scripts = instance_scripts or []
for glb in global_scripts:
split = glb.split("=", 1)
if len(split) != 2:
continue
if split[0] not in constants.OS_LIST:
raise ValueError(
"Invalid OS %s. Available options are: %s" % (
split[0], ", ".join(constants.OS_LIST)))
if not split[1]:
# removing script
ret["global"][split[0]] = None
continue
if os.path.isfile(split[1]) is False:
raise ValueError("Could not find %s" % split[1])
with open(split[1]) as sc:
ret["global"][split[0]] = sc.read()
for inst in instance_scripts:
split = inst.split("=", 1)
if len(split) != 2:
continue
if not split[1]:
# removing script
ret['instances'][split[0]] = None
continue
if os.path.isfile(split[1]) is False:
raise ValueError("Could not find %s" % split[1])
with open(split[1]) as sc:
ret["instances"][split[0]] = sc.read()
return ret
def add_minion_pool_args_to_parser(
parser, include_origin_pool_arg=True,
include_destination_pool_arg=True,
include_osmorphing_pool_mappings_arg=True):
if include_origin_pool_arg:
parser.add_argument(
'--origin-minion-pool-id',
help='The ID of a pre-existing Coriolis minion pool associated '
'with the origin Coriolis endpoint to use for disk syncing. '
'The pool must contain Linux machines.')
if include_destination_pool_arg:
parser.add_argument(
'--destination-minion-pool-id',
help='The ID of a pre-existing Coriolis minion pool associated '
'with the target Coriolis endpoint to use for disk syncing. '
'The pool must contain Linux machines.')
if include_osmorphing_pool_mappings_arg:
# NOTE: arparse will just call whatever 'type=' was supplied on a value
# so we can pass in a single-arg function to have it modify the value:
def _split_pool_mapping_arg(arg):
instance_id, pool_id = arg.split('=')
return {
"instance_id": instance_id.strip('\'"'),
"pool_id": pool_id.strip('\'"')}
parser.add_argument(
'--osmorphing-minion-pool-mapping', action='append',
dest="instance_osmorphing_minion_pool_mappings",
type=_split_pool_mapping_arg,
help='Mapping between the identifier of an instance and a '
'pre-existing Coriolis minion pool to be used for its '
'OSMorphing. The minion pool must contain machines of the '
'same OS type and which are compatible with OSMorphing '
'the guest OS of each afferent instance. The mappings must '
'be of the form "INSTANCE_IDENTIFIER=MINION_POOL_ID".')
|