Fixes stale undercloud delorean repos
[apex.git] / apex / common / utils.py
index 13250a4..b727b11 100644 (file)
@@ -8,10 +8,12 @@
 ##############################################################################
 
 import datetime
+import distro
 import json
 import logging
 import os
 import pprint
+import socket
 import subprocess
 import tarfile
 import time
@@ -192,3 +194,29 @@ def fetch_upstream_and_unpack(dest, url, targets):
             tar = tarfile.open(target_dest)
             tar.extractall(path=dest)
             tar.close()
+
+
+def install_ansible():
+    # we only install for CentOS/Fedora for now
+    dist = distro.id()
+    if 'centos' in dist:
+        pkg_mgr = 'yum'
+    elif 'fedora' in dist:
+        pkg_mgr = 'dnf'
+    else:
+        return
+
+    # yum python module only exists for 2.x, so use subprocess
+    try:
+        subprocess.check_call([pkg_mgr, '-y', 'install', 'ansible'])
+    except subprocess.CalledProcessError:
+        logging.warning('Unable to install Ansible')
+
+
+def internet_connectivity():
+    try:
+        urllib.request.urlopen('http://opnfv.org', timeout=3)
+        return True
+    except (urllib.request.URLError, socket.timeout):
+        logging.debug('No internet connectivity detected')
+        return False