Added a check for cloud-init completion before final ssh client check.
[snaps.git] / snaps / file_utils.py
index 699d378..284ae15 100644 (file)
@@ -12,6 +12,8 @@
 # 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 ssl
+
 import os
 import logging
 
@@ -39,10 +41,11 @@ def file_exists(file_path):
     the path is a directory
     :return:
     """
-    if os.path.exists(file_path):
-        if os.path.isdir(file_path):
+    expanded_path = os.path.expanduser(file_path)
+    if os.path.exists(expanded_path):
+        if os.path.isdir(expanded_path):
             return False
-        return os.path.isfile(file_path)
+        return os.path.isfile(expanded_path)
     return False
 
 
@@ -167,7 +170,8 @@ def __get_url_response(url):
     proxy_handler = urllib.ProxyHandler({})
     opener = urllib.build_opener(proxy_handler)
     urllib.install_opener(opener)
-    return urllib.urlopen(url)
+    context = ssl._create_unverified_context()
+    return urllib.urlopen(url, context=context)
 
 
 def read_yaml(config_file_path):
@@ -179,7 +183,7 @@ def read_yaml(config_file_path):
     logger.debug('Attempting to load configuration file - ' + config_file_path)
     config_file = None
     try:
-        with open(config_file_path) as config_file:
+        with open(config_file_path, 'r') as config_file:
             config = yaml.safe_load(config_file)
             logger.info('Loaded configuration')
         return config
@@ -189,6 +193,20 @@ def read_yaml(config_file_path):
             config_file.close()
 
 
+def persist_dict_to_yaml(the_dict, file_name):
+    """
+    Creates a YAML file from a dict
+    :param the_dict: the dictionary to store
+    :param conf_dir: the directory used to store the config file
+    :return: the file object
+    """
+    logger.info('Persisting %s to [%s]', the_dict, file_name)
+    file_path = os.path.expanduser(file_name)
+    yaml_from_dict = yaml.dump(
+        the_dict, default_flow_style=False, default_style='')
+    return save_string_to_file(yaml_from_dict, file_path)
+
+
 def read_os_env_file(os_env_filename):
     """
     Reads the OS environment source file and returns a map of each key/value