forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-upload
executable file
·118 lines (99 loc) · 4.18 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
#!/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/>.
import argparse
import getpass
import os
import socket
import subprocess
import sys
import urllib.parse
from machine.machine_core.constants import IMAGES_DIR
from machine.machine_core.directories import BOTS_DIR, get_images_data_dir
from task import api, PUBLIC_STORES, REDHAT_STORES
def upload(store, source):
ca = os.path.join(BOTS_DIR, "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
# Pass credentials, if present
if api.token:
user = url.username or getpass.getuser()
cmd += ["--user", user + ":" + api.token]
# 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 = 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(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:
# determine possible stores, unless explicitly given
stores = args.store
if not stores:
# these images are not freely redistributable, keep them Red Hat internal
b = os.path.basename(source)
if b.startswith("rhel") or image.startswith("windows"):
stores = REDHAT_STORES
else:
stores = PUBLIC_STORES
for store in stores:
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())