#!/usr/bin/env python3 # This file is part of Cockpit. # # Copyright (C) 2013 Red Hat, Inc. # # Cockpit is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # Cockpit 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with Cockpit; If not, see . # The default settings here should match one of the default download stores DEFAULT_UPLOAD = [ "https://images-cockpit.apps.ci.centos.org/", "https://209.132.184.41:8493/", ] TOKEN = "~/.config/github-token" import argparse import getpass import errno import os import socket import subprocess import sys import urllib.parse from machine import testvm BOTS = os.path.dirname(__file__) def upload(store, source): ca = os.path.join(BOTS, "images", "files", "ca.pem") url = urllib.parse.urlparse(store) # Start building the command cmd = ["curl", "--progress-bar", "--cacert", ca, "--fail", "--upload-file", source ] def try_curl(cmd): print("Uploading to", cmd[-1]) # Passing through a non terminal stdout is necessary to make progress work curl = subprocess.Popen(cmd, stdout=subprocess.PIPE) cat = subprocess.Popen(["cat"], stdin=curl.stdout) curl.stdout.close() ret = curl.wait() cat.wait() if ret != 0: sys.stderr.write("image-upload: unable to upload image: {0}\n".format(cmd[-1])) return ret # Parse the user name and token, if present user = url.username or getpass.getuser() try: with open(os.path.expanduser(TOKEN), "r") as gt: token = gt.read().strip() cmd += [ "--user", user + ":" + token ] except IOError as exc: if exc.errno == errno.ENOENT: pass # First try to use the original store URL, for stores with valid SSL cert on an OpenShift proxy if try_curl(cmd + [store]) == 0: return 0 # Fall back for stores that use our self-signed cockpit certificate # Parse out the actual address to connect to and override certificate info defport = url.scheme == 'http' and 80 or 443 ai = socket.getaddrinfo(url.hostname, url.port or defport, socket.AF_INET, 0, socket.IPPROTO_TCP) for (family, socktype, proto, canonname, sockaddr) in ai: resolve = "cockpit-tests:{1}:{0}".format(*sockaddr) curl_url = "https://cockpit-tests:{0}{1}".format(url.port or defport, url.path) ret = try_curl(cmd + ["--resolve", resolve, curl_url]) if ret == 0: return 0 return 1 def main(): parser = argparse.ArgumentParser(description='Upload bot state or images') parser.add_argument("--store", action="append", default=[], help="Where to send state or images") parser.add_argument("--state", action="store_true", help="Images or state not recorded in git") parser.add_argument('image', nargs='*') args = parser.parse_args() data_dir = testvm.get_images_data_dir() sources = [] for image in args.image: if args.state: source = os.path.join(data_dir, image) else: link = os.path.join(testvm.IMAGES_DIR, image) if not os.path.islink(link): parser.error("image link does not exist: " + image) source = os.path.join(data_dir, os.readlink(link)) if not os.path.isfile(source): parser.error("image does not exist: " + image) sources.append(source) for source in sources: for store in (args.store or DEFAULT_UPLOAD): ret = upload(store, source) if ret == 0: return ret else: # all stores failed, so return last exit code return ret if __name__ == '__main__': sys.exit(main())