Skip to content

Commit 8099a3a

Browse files
committed
Improvements and tweaks
1 parent 90a5b63 commit 8099a3a

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

contrib/server/operations/pbench-upload-results.py

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
import sys
3333
from typing import Optional
3434

35-
from dateutil import parser as date_parser
35+
import dateutil.parser
3636
import requests
37-
from requests import Response
3837

3938

4039
def get_md5(tarball: Path) -> str:
@@ -52,32 +51,27 @@ def get_md5(tarball: Path) -> str:
5251

5352
def upload(
5453
server: str, token: str, tarball: Path, metadata: Optional[list[str]] = None
55-
) -> Response:
56-
query_parameters = {}
57-
54+
) -> requests.Response:
5855
md5 = get_md5(tarball)
59-
query_parameters["access"] = "public"
60-
dataset = tarball.name
61-
satellite, _ = tarball.parent.name.split("::")
62-
meta = [
63-
f"global.server.legacy.migrated:{datetime.datetime.now(tz=datetime.timezone.utc):%Y-%m-%dT%H:%M}"
64-
]
65-
if satellite:
56+
uploaded = datetime.datetime.fromtimestamp(
57+
tarball.stat().st_mtime, tz=datetime.timezone.utc
58+
)
59+
meta = [f"global.server.legacy.migrated:{uploaded:%Y-%m-%dT%H:%M}"]
60+
if "::" in tarball.parent.name:
61+
satellite, _ = tarball.parent.name.split("::")
6662
meta.append(f"server.origin:{satellite}")
6763
if metadata:
6864
meta.extend(metadata)
69-
query_parameters["metadata"] = meta
70-
headers = {
71-
"Content-MD5": md5,
72-
"content-type": "application/octet-stream",
73-
"authorization": f"bearer {token}",
74-
}
7565

7666
with tarball.open("rb") as f:
7767
return requests.put(
78-
f"{server}/api/v1/upload/{dataset}",
79-
headers=headers,
80-
params=query_parameters,
68+
f"{server}/api/v1/upload/{tarball.name}",
69+
headers={
70+
"Content-MD5": md5,
71+
"content-type": "application/octet-stream",
72+
"authorization": f"bearer {token}",
73+
},
74+
params={"metadata": meta, "access": "public"},
8175
data=f,
8276
)
8377

@@ -127,6 +121,9 @@ def main() -> int:
127121
s = Path("/opt/pbench-server/SHA1")
128122
version = v.read_text().strip() if v.exists() else "unknown"
129123
sha1 = s.read_text().strip() if s.exists() else "unknown"
124+
125+
# The standard metadata keys here are modeled on those provided by the
126+
# 0.69-11 passthrough server.
130127
metadata = [
131128
f"global.server.legacy.hostname:{host}",
132129
f"global.server.legacy.sha1:{sha1}",
@@ -135,21 +132,29 @@ def main() -> int:
135132
if parsed.metadata:
136133
metadata.extend(parsed.metadata)
137134

135+
# Process date range filtering arguments
138136
since: Optional[datetime.datetime] = None
139137
before: Optional[datetime.datetime] = None
140138

141139
since_ts: Optional[float] = None
142140
before_ts: Optional[float] = None
143141

144142
if parsed.since:
145-
since = date_parser.parse(parsed.since)
143+
since = dateutil.parser.parse(parsed.since)
146144
since_ts = since.timestamp()
147145

148146
if parsed.before:
149-
before = date_parser.parse(parsed.before)
147+
before = dateutil.parser.parse(parsed.before)
150148
before_ts = before.timestamp()
151149

152150
if since and before:
151+
if before <= since:
152+
print(
153+
f"SINCE ({since}) must be earlier than BEFORE ({before})",
154+
file=sys.stderr,
155+
)
156+
return 1
157+
153158
when = f" (from {since:%Y-%m-%d %H:%M} to {before:%Y-%m-%d %H:%M})"
154159
elif since:
155160
when = f" (since {since:%Y-%m-%d %H:%M})"
@@ -165,21 +170,23 @@ def main() -> int:
165170

166171
print(f"{what}{when} -> SERVER {parsed.server}")
167172

173+
# If a checkpoint file is specified, and already exists, load the list of
174+
# files already uploaded.
168175
checkpoint: Optional[Path] = None
169176
processed: list[str] = []
170177
if parsed.checkpoint:
171178
checkpoint = parsed.checkpoint
172-
if parsed.verify:
173-
print(f"Processing checkpoint state from {checkpoint}...")
174179
if checkpoint.exists():
180+
if parsed.verify:
181+
print(f"Processing checkpoint state from {checkpoint}...")
175182
processed = checkpoint.read_text().splitlines()
176183
if parsed.verify:
177-
print(f"[CPT] done {len(processed)}")
178-
if parsed.verify:
179-
print(
180-
f"Finished processing checkpoint data: {len(processed)} checkpointed files"
181-
)
184+
print(
185+
f"Finished processing checkpoint data: {len(processed)} checkpointed files"
186+
)
182187

188+
# Using the specified filters and checkpoint data, determine the set of
189+
# tarballs we'll try to upload.
183190
if parsed.verify:
184191
print("Identifying target tarballs")
185192
skipped = 0
@@ -201,23 +208,29 @@ def main() -> int:
201208
if str(t) in processed:
202209
skipped += 1
203210
if parsed.verify:
204-
print(f"[CPT] skip {t}")
211+
print(f"[checkpoint] skip {t}")
205212
continue
206213
which.append(t)
207214
elif parsed.tarball.is_file():
208215
which = [parsed.tarball]
209216
else:
210-
print(f"Path {parsed.tarball} doesn't exist", file=sys.stderr)
217+
print(f"Path {parsed.tarball} isn't a directory or file", file=sys.stderr)
211218
return 1
212219
if parsed.verify:
213220
print(
214221
f"Identified {len(which)} target tarballs: skipped {skipped}, {early} too old, {late} too new"
215222
)
216223

224+
# We'll append successful uploads to the checkpoint file.
217225
checkwriter: Optional[TextIOWrapper] = None
218226
if checkpoint:
219227
checkwriter = checkpoint.open(mode="a")
220228

229+
# Now start the upload, checkpointing each file after a successful upload
230+
#
231+
# We maintain the checkpoint file for --preview to assist in testing: if
232+
# you want to test with --preview and then do a real upload, delete the
233+
# checkpoint file!
221234
if parsed.verify:
222235
print("Uploading target files...")
223236
success = 0

0 commit comments

Comments
 (0)