3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
24 # ----------------------------------------------------------
28 # -----------------------------------------------------------
29 def check_internet_connectivity(url='http://www.opnfv.org/'):
31 Check if there is access to the internet
34 urllib2.urlopen(url, timeout=5)
36 except urllib2.URLError:
40 def download_url(url, dest_path):
42 Download a file to a destination path given a URL
44 name = url.rsplit('/')[-1]
45 dest = dest_path + "/" + name
47 response = urllib2.urlopen(url)
48 except (urllib2.HTTPError, urllib2.URLError):
51 with open(dest, 'wb') as f:
52 shutil.copyfileobj(response, f)
56 # ----------------------------------------------------------
60 # -----------------------------------------------------------
61 def get_git_branch(repo_path):
65 repo = Repo(repo_path)
66 branch = repo.active_branch
70 def get_installer_type(logger=None):
72 Get installer type (fuel, apex, joid, compass)
75 installer = os.environ['INSTALLER_TYPE']
78 logger.error("Impossible to retrieve the installer type")
79 installer = "Unknown_installer"
84 def get_scenario(logger=None):
89 scenario = os.environ['DEPLOY_SCENARIO']
92 logger.error("Impossible to retrieve the scenario")
93 scenario = "Unknown_scenario"
98 def get_version(logger=None):
102 # Use the build tag to retrieve the version
103 # By default version is unknown
104 # if launched through CI the build tag has the following format
105 # jenkins-<project>-<installer>-<pod>-<job>-<branch>-<id>
106 # e.g. jenkins-functest-fuel-opnfv-jump-2-daily-master-190
107 # use regex to match branch info
108 rule = "daily-(.+?)-[0-9]*"
109 build_tag = get_build_tag(logger)
110 m = re.search(rule, build_tag)
117 def get_pod_name(logger=None):
119 Get PoD Name from env variable NODE_NAME
122 return os.environ['NODE_NAME']
126 "Unable to retrieve the POD name from environment. " +
127 "Using pod name 'unknown-pod'")
131 def get_build_tag(logger=None):
133 Get build tag of jenkins jobs
136 build_tag = os.environ['BUILD_TAG']
139 logger.error("Impossible to retrieve the build tag")
140 build_tag = "unknown_build_tag"
145 def push_results_to_db(db_url, project, case_name, logger, pod_name,
146 version, scenario, criteria, build_tag, payload):
148 POST results to the Result target DB
150 url = db_url + "/results"
151 installer = get_installer_type(logger)
152 params = {"project_name": project, "case_name": case_name,
153 "pod_name": pod_name, "installer": installer,
154 "version": version, "scenario": scenario, "criteria": criteria,
155 "build_tag": build_tag, "details": payload}
157 headers = {'Content-Type': 'application/json'}
159 r = requests.post(url, data=json.dumps(params), headers=headers)
164 print ("Error [push_results_to_db('%s', '%s', '%s', " +
165 "'%s', '%s', '%s', '%s', '%s', '%s')]:" %
166 (db_url, project, case_name, pod_name, version,
167 scenario, criteria, build_tag, payload), e)
171 def get_resolvconf_ns():
173 Get nameservers from current resolv.conf
176 rconf = open("/etc/resolv.conf", "r")
177 line = rconf.readline()
179 ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
180 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
182 result = sock.connect_ex((ip.group(), 53))
184 nameservers.append(ip.group())
185 line = rconf.readline()
189 def get_ci_envvars():
191 Get the CI env variables
194 "installer": os.environ.get('INSTALLER_TYPE'),
195 "scenario": os.environ.get('DEPLOY_SCENARIO')}
199 def execute_command(cmd, logger=None,
205 error_msg = ("The command '%s' failed." % cmd)
206 msg_exec = ("Executing command: '%s'" % cmd)
210 logger.info(msg_exec)
212 logger.debug(msg_exec)
215 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
216 while p.poll() is None:
217 line = p.stdout.readline().rstrip()
226 if p.returncode != 0:
229 logger.error(error_msg)