#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # Copyright (C) 2014 Canonical Ltd. # Author: Li-Hao Liao (Leon Liao) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # ciic: Collect Installed Information Client # This utility will be included into QA's test utilities. # This utility will download the cii.py from PXE server and launch it. from os.path import basename, join, isfile from urllib.request import urlopen, urlretrieve import urllib.error import subprocess import imp import sys import socket __version__ = '2.15.1.28' SERVERURL='http://10.101.47.201:8168' REQUESTS='python-requests_2.2.1-1ubuntu0.1_all.deb' URLLIB3='python-urllib3_1.7.1-1build1_all.deb' CII = 'cii' socket.setdefaulttimeout(5) domain = subprocess.check_output('route | awk \'/default/{print $2}\'', shell=True, universal_newlines=True).strip() if domain == '10.101.46.1': SERVERURL='http://10.101.46.229:8168' if isfile(".ciipath"): with open(".ciipath") as f: SERVERURL=f.read() def downloadAndLaunch(url, type='python'): # try: urllib.request.urlretrieve(url, basename(url)) #except urllib.error.HTTPError as httperror: # print('\033[91mFail: \033[92mCan not get the ' + basename(url) + ' from server.\033[0m') if type == 'python': cmd = 'python3 ./' + basename(url) elif type == 'deb': cmd = 'sudo dpkg -i ' + basename(url) ret = subprocess.call(cmd.split()) cmd = 'rm -irf ' + basename(url) subprocess.call(cmd.split()) return ret try: imp.find_module('requests') except ImportError: downloadAndLaunch(join(SERVERURL,URLLIB3), 'deb') downloadAndLaunch(join(SERVERURL,REQUESTS), 'deb') try: imp.find_module('requests') except ImportError: subprocess.call('sudo apt-get update'.split()) subprocess.call('sudo apt-get install -y python-requests'.split()) try: import requests ret = downloadAndLaunch(join(SERVERURL,CII)) except (requests.exceptions.ConnectionError, urllib.error.URLError) as e: print(e) print('\033[91mFail: \033[92mThere is a network problem to connect to {}. This process have been stopped.\033[0m'.format(SERVERURL)) sys.exit(1) except: print('\033[91mFail: \033[92mSome problems cause this process can not be finished.\033[0m') sys.exit(1) sys.exit(ret)