1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 Common usuful functions
28 __author__
="Alfonso Tierno, Pablo Montes"
29 __date__
="$10-jul-2014 12:07:15$"
34 from definitionsClass
import definitionsClass
35 from definitionsClass
import Units
37 from jsonschema
import validate
as js_v
, exceptions
as js_e
39 def check_and_convert_units(value
, value_type
):
40 """TODO: Update description
41 This method receives a text with 2 fields using a blank as separator and a list of valid units. The first field must represent a number
42 and the second one units.
43 In case the second field is not one of valid_units (False, <error description>) is returned.
44 In case the second field is a valid unit the first number is converted in the following way:
45 Gbps, Mbps, kbps -> Mbps
46 GB,MB,KB,B,GiB,MiB,KiB -> B
48 If conversion is done successfully (True, <converted value>) is returned"""
50 if value_type
== Units
.no_units
:
51 if not isinstance(value
,int) and not isinstance(value
,float):
52 return (False, 'When no units are used only an integer or float must be used')
53 elif value_type
== Units
.name
:
54 if not isinstance(value
,str):
55 return (False, 'For names str must be used')
56 elif value_type
== Units
.boolean
:
57 if not isinstance(value
,bool):
58 return (False, 'A boolean or Yes/No mut be used')
60 splitted
= value
.split(' ')
61 if len(splitted
) != 2:
62 return (False, 'Expected format: <value> <units>')
63 (value
, units
) = splitted
64 if ',' in value
or '.' in value
:
65 return (False, 'Use integers to represent numeric values')
69 # if not isinstance(value_type, Units):
70 # return (False, 'Not valid value_type')
72 valid_units
= definitionsClass
.units
[value_type
]
74 #Convert everything to upper in order to make comparations easier
76 for i
in range(0, len(valid_units
)):
77 valid_units
[i
] = valid_units
[i
].upper()
79 #Check the used units are valid ones
80 if units
not in valid_units
:
81 return (False, 'Valid units are: '+', '.join(valid_units
))
83 if units
.startswith('GI'):
84 value
= value
*1024*1024*1024
85 elif units
.startswith('MI'):
86 value
= value
*1024*1024
87 elif units
.startswith('KI'):
89 elif units
.startswith('G'):
90 value
= value
*1000000000
91 elif units
.startswith('M'):
92 value
= value
*1000000
93 elif units
.startswith('K'):
96 return (False, 'Unexpected error in auxiliary_functions.py - check_and_convert_units:\n'+str(e
))
100 def get_ssh_connection(machine
, user
=None, password
=None):
101 """Stablishes an ssh connection to the remote server. Returns (True, paramiko_ssh) in case of success or (False, <error message>) in case of error"""
103 s
= paramiko
.SSHClient()
104 s
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
105 s
.load_system_host_keys()
106 s
.connect(machine
, 22, user
, password
, timeout
=10)
108 return (False, 'It was not possible to connect to '+machine
+str(e
))
112 def run_in_remote_server(s
,command
):
113 """Runs in the remote server the specified command. Returns (True, stdout) in case of success or (False, <error message>) in case of error"""
115 (_
, stdout
, stderr
) = s
.exec_command(command
)
116 error_msg
= stderr
.read()
117 if len(error_msg
) > 0:
118 return (False, error_msg
)
120 return (False, str(e
))
122 return (True, stdout
)
124 def read_file(file_
):
125 """Reads a file specified by 'file' and returns (True,<its content as a string>) in case of success or (False, <error message>) in case of failure"""
131 return (False, str(e
))
133 return (True, read_data
)
135 def check_contains(element
, keywords
):
136 """Auxiliary function used to check if a yaml structure contains or not
137 an specific field. Returns a bool"""
139 if not key
in element
:
143 def check_contains_(element
, keywords
):
144 """Auxiliary function used to check if a yaml structure contains or not
145 an specific field. Returns a bool,missing_variables"""
147 if not key
in element
:
151 def write_file(file_
, content
):
152 """Generates a file specified by 'file' and fills it using 'content'"""
157 def nice_print(yaml_element
):
158 """Print a yaml structure. Used mainly for debugging"""
159 print(yaml
.dump(yaml_element
, default_flow_style
=False))
161 def new_random_mac():
162 mac
= (0xE2, random
.randint(0x00, 0xff), random
.randint(0x00, 0xff), random
.randint(0x00, 0xff), random
.randint(0x00, 0xff), random
.randint(0x00, 0xff) )
163 return ':'.join(map(lambda x
: "%02X" % x
, mac
))
165 def parse_dict(var
, template
):
166 if type(var
) is not dict: return -1, 'not a dictionary'
167 for _
,tv
in template
.items():
171 def delete_nulls(var
):
172 if type(var
) is dict:
174 if var
[k
] is None: del var
[k
]
175 elif type(var
[k
]) is dict or type(var
[k
]) is list or type(var
[k
]) is tuple:
176 if delete_nulls(var
[k
]): del var
[k
]
177 if len(var
) == 0: return True
178 elif type(var
) is list or type(var
) is tuple:
180 if type(k
) is dict: delete_nulls(k
)
181 if len(var
) == 0: return True
184 def get_next_2pow(var
):
190 def check_valid_uuid(uuid
):
191 id_schema
= {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
193 js_v(uuid
, id_schema
)
195 except js_e
.ValidationError
:
199 '''Removes recursively empty dictionaries or lists
200 return True if var is an empty dict or list '''
201 if type(var
) is dict:
203 if var
[k
] is None: del var
[k
]
204 elif type(var
[k
]) is dict or type(var
[k
]) is list or type(var
[k
]) is tuple:
205 if DeleteNone(var
[k
]): del var
[k
]
206 if len(var
) == 0: return True
207 elif type(var
) is list or type(var
) is tuple:
209 if type(k
) is dict: DeleteNone(k
)
210 if len(var
) == 0: return True
213 def gen_random_mac():
214 '''generates a random mac address. Avoid multicast, broadcast, etc
218 #2 + 4*random.randint(0x00, 0x3f), #4 multiple, unicast local mac address
220 random
.randint(0x00, 0xff),
221 random
.randint(0x00, 0xff),
222 random
.randint(0x00, 0xff),
223 random
.randint(0x00, 0xff),
224 random
.randint(0x00, 0xff)
226 return ':'.join(map(lambda x
: "%02x" % x
, mac
))