#!/usr/bin/env python # The default settings here should match one of the default # download stores. These are usually cockpit/images instances UPLOAD = "https://209.132.184.69:8493/" TOKEN = "~/.config/github-token" import argparse import getpass import os import socket import subprocess import sys import urlparse BOTS = os.path.dirname(__file__) IMAGES = os.path.join(BOTS, "images") DATA = os.path.join(os.environ.get("TEST_DATA", BOTS), "images") def upload(store, source): ca = os.path.join(BOTS, "images", "files", "ca.pem") url = urlparse.urlparse(store) # Parse out the user name if present user = url.username or getpass.getuser() # 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) message = "{scheme}://{0}:{1}{path}".format(*sockaddr, scheme=url.scheme, path=url.path) break # Start building the command cmd = ["curl", "--progress-bar", "--cacert", ca, "--resolve", resolve, "--fail", "--upload-file", source ] token = "" try: with open(os.path.expanduser(TOKEN), "r") as gt: token = gt.read().strip() except IOError as exc: if exc.errno == errno.ENOENT: pass if token: cmd += [ "--user", user + ":" + token ] cmd.append("https://cockpit-tests:{0}{1}".format(url.port or defport, url.path)) # 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(message)) return ret def main(): parser = argparse.ArgumentParser(description='Upload virtual machine images') parser.add_argument("--store", default=UPLOAD, help="Where to send images") parser.add_argument('image', nargs='*') args = parser.parse_args() sources = [] for image in args.image: link = os.path.join(IMAGES, image) if not os.path.islink(link): parser.error("image link does not exist: " + image) source = os.path.join(DATA, os.readlink(link)) if not os.path.isfile(source): parser.error("image does not exist: " + image) sources.append(source) for source in sources: ret = upload(args.store, source) if ret != 0: return ret if __name__ == '__main__': sys.exit(main())