forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-upload
executable file
·120 lines (101 loc) · 4.09 KB
/
image-upload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/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 <http://www.gnu.org/licenses/>.
# 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())