summary refs log tree commit diff
path: root/scripts/flat-manager-client
blob: 89a23a35685f967e384ea6d2f1a641c792c0258f (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
#!/usr/bin/python3

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import asyncio
import base64
import binascii
import errno
import fnmatch
import gzip
import json
import logging
import os
import sys
import time
import traceback
from argparse import ArgumentParser
from functools import reduce
from urllib.parse import urljoin, urlparse, urlsplit, urlunparse, urlunsplit

import aiohttp
import aiohttp.client_exceptions
from tenacity import retry, retry_if_exception_type, stop_after_delay, wait_random_exponential

import gi
gi.require_version('OSTree', '1.0')
from gi.repository import Gio, GLib, OSTree

UPLOAD_CHUNK_LIMIT = 4 * 1024 * 1024
DEFAULT_LIMIT = 2 ** 16

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

class UsageException(Exception):
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return self.msg

class ApiError(Exception):
    def __init__(self, response, body):
        self.url = str(response.url)
        self.status = response.status

        try:
            self.body = json.loads(body)
        except:
            self.body = {"status": self.status, "error-type": "no-error", "message": "No json error details from server"}

    def repr(self):
        return {
            "type": "api",
            "url": self.url,
            "status_code": self.status,
            "details": self.body
        }

    def __str__(self):
        return "Api call to %s failed with status %d, details: %s" % (self.url, self.status, self.body)

class ServerApiError(ApiError):
    def __init__(self, response, body):
        super().__init__(response, body)


class FailedJobError(Exception):
    def __init__(self, job):
        self.job = job

    def repr(self):
        return {
            "type": "job",
            "job": self.job
        }

    def __str__(self):
        return "Job failed: %s" % (self.job)


TENACITY_RETRY_EXCEPTIONS = (retry_if_exception_type(aiohttp.client_exceptions.ServerDisconnectedError) | retry_if_exception_type(ServerApiError) | retry_if_exception_type(aiohttp.client_exceptions.ServerConnectionError) | retry_if_exception_type(aiohttp.client_exceptions.ClientOSError))
TENACITY_STOP_AFTER = stop_after_delay(300)
TENACITY_WAIT_BETWEEN = wait_random_exponential(multiplier=1, max=60)

# This is similar to the regular payload, but opens the file lazily
class AsyncNamedFilePart(aiohttp.payload.Payload):
    def __init__(self,
                 value,
                 disposition='attachment',
                 *args,
                 **kwargs):
        self._file = None
        if 'filename' not in kwargs:
            kwargs['filename'] = os.path.basename(value)

        super().__init__(value, *args, **kwargs)

        if self._filename is not None and disposition is not None:
            self.set_content_disposition(disposition, filename=self._filename, quote_fields=False)

        self._size = os.stat(value).st_size

    async def write(self, writer):
        if self._file is None or self._file.closed:
            self._file = open(self._value, 'rb')
        try:
            chunk = self._file.read(DEFAULT_LIMIT)
            while chunk:
                await writer.write(chunk)
                chunk = self._file.read(DEFAULT_LIMIT)
        finally:
            self._file.close()

    @property
    def size(self):
        return self._size

def ostree_object_path(repo, obj):
    repodir = repo.get_path().get_path()
    return os.path.join(repodir, 'objects', obj[0:2], obj[2:])

def ostree_get_dir_files(repo, objects, dirtree):
    if dirtree.endswith(".dirtree"):
        dirtree = dirtree[:-8]
    dirtreev = repo.load_variant(OSTree.ObjectType.DIR_TREE, dirtree)[1]
    iter = OSTree.RepoCommitTraverseIter()
    iter.init_dirtree(repo, dirtreev, 0)
    while True:
        type = iter.next()
        if type == OSTree.RepoCommitIterResult.END:
            break
        if type == OSTree.RepoCommitIterResult.ERROR:
            break
        if type == OSTree.RepoCommitIterResult.FILE:
            d = iter.get_file()
            objects.add(d.out_checksum + ".filez")
        if type == OSTree.RepoCommitIterResult.DIR:
            pass

def local_needed_files(repo, metadata_objects):
    objects = set()
    for c in metadata_objects:
        if c.endswith(".dirtree"):
            ostree_get_dir_files(repo, objects, c)
    return objects

def local_needed_metadata_dirtree(repo, objects, dirtree_content, dirtree_meta):
    objects.add(dirtree_meta + ".dirmeta")
    dirtree_content_name = dirtree_content + ".dirtree"
    if dirtree_content_name in objects:
        return
    objects.add(dirtree_content_name)

    dirtreev = repo.load_variant(OSTree.ObjectType.DIR_TREE, dirtree_content)[1]
    iter = OSTree.RepoCommitTraverseIter()
    iter.init_dirtree(repo, dirtreev, 0)
    while True:
        type = iter.next()
        if type == OSTree.RepoCommitIterResult.END:
            break
        if type == OSTree.RepoCommitIterResult.ERROR:
            break
        if type == OSTree.RepoCommitIterResult.FILE:
            pass
        if type == OSTree.RepoCommitIterResult.DIR:
            d = iter.get_dir()
            local_needed_metadata_dirtree(repo, objects, d.out_content_checksum, d.out_meta_checksum)

def local_needed_metadata(repo, commits):
    objects = set()
    for rev in commits:
        objects.add(rev + ".commit")
        commitv = repo.load_variant(OSTree.ObjectType.COMMIT, rev)[1]
        iter = OSTree.RepoCommitTraverseIter()
        iter.init_commit(repo, commitv, 0)
        while True:
            type = iter.next()
            if type == OSTree.RepoCommitIterResult.END:
                break
            if type == OSTree.RepoCommitIterResult.ERROR:
                break
            if type == OSTree.RepoCommitIterResult.FILE:
                pass
            if type == OSTree.RepoCommitIterResult.DIR:
                d = iter.get_dir()
                local_needed_metadata_dirtree(repo, objects, d.out_content_checksum, d.out_meta_checksum)
    return objects


def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def missing_objects(session, build_url, token, wanted):
    missing=[]
    for chunk in chunks(wanted, 2000):
        wanted_json=json.dumps({'wanted': chunk}).encode('utf-8')
        data=gzip.compress(wanted_json)
        headers = {
            'Authorization': 'Bearer ' + token,
            'Content-Encoding': 'gzip',
            'Content-Type': 'application/json'
        }
        resp = await session.get(build_url + "/missing_objects", data=data, headers=headers)
        async with resp:
            if resp.status != 200:
                raise ApiError(resp, await resp.text())
            data = await resp.json()
            missing.extend(data["missing"])
    return missing

@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def upload_files(session, build_url, token, files):
    if len(files) == 0:
        return
    print("Uploading %d files (%d bytes)" % (len(files), reduce(lambda x, y: x + y, map(lambda f: f.size, files))))
    with aiohttp.MultipartWriter() as writer:
        for f in files:
            writer.append(f)
    writer.headers['Authorization'] = 'Bearer ' + token
    resp = await session.request("post", build_url + '/upload', data=writer, headers=writer.headers)
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())


async def upload_deltas(session, repo_path, build_url, token, deltas, refs, ignore_delta):
    if not len(deltas):
        return

    req = []
    for ref, commit in refs.items():
        # Skip screenshots here
        parts = ref.split("/")
        if len(parts) == 4 and (parts[0] == "app" or parts[0] =="runtime") and not should_skip_delta(parts[1], ignore_delta):
            for delta in deltas:
                # Only upload from-scratch deltas, as these are the only reused ones
                if delta == commit:
                    print(" %s: %s" % (ref, delta))
                    delta_name = delta_name_encode (delta)
                    delta_dir = repo_path + "/deltas/" + delta_name[:2] + "/" + delta_name[2:]
                    parts = os.listdir(delta_dir)
                    for part in parts:
                        req.append(AsyncNamedFilePart(delta_dir + "/" + part, filename = delta_name + "." + part + ".delta"))

    if len(req):
        await upload_files(session, build_url, token, req)


async def upload_objects(session, repo_path, build_url, token, objects):
    req = []
    total_size = 0
    for file_obj in objects:
        named = get_object_multipart(repo_path, file_obj)
        file_size = named.size
        if total_size + file_size > UPLOAD_CHUNK_LIMIT: # The new object would bring us over the chunk limit
            if len(req) > 0: # We already have some objects, upload those first
                next_req = [named]
                total_size = file_size
            else:
                next_req  =  []
                req.append(named)
                total_size = 0
            await upload_files(session, build_url, token, req)
            req = next_req
        else:
            total_size = total_size + file_size
            req.append(named)

    # Upload any remainder
    await upload_files(session, build_url, token, req)


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def create_ref(session, build_url, token, ref, commit, build_log_url=None):
    print("Creating ref %s with commit %s" % (ref, commit))
    resp = await session.post(
        build_url + "/build_ref",
        headers={ 'Authorization': 'Bearer ' + token },
        json= { "ref": ref, "commit": commit, "build-log-url": build_log_url }
    )
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())

        data = await resp.json()
        return data


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def add_extra_ids(session, build_url, token, extra_ids):
    print("Adding extra ids %s" % (extra_ids))
    resp = await session.post(build_url + "/add_extra_ids", headers={'Authorization': 'Bearer ' + token}, json= { "ids": extra_ids} )
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())

        data = await resp.json()
        return data


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def get_build(session, build_url, token):
    resp = await session.get(build_url, headers={'Authorization': 'Bearer ' + token})
    if resp.status != 200:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())
    data = await resp.json()
    return data

# For stupid reasons this is a string with json, lets expand it
def reparse_job_results(job):
    job["results"] = json.loads(job.get("results", "{}"))
    return job


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def get_job(session, job_url, token):
    resp = await session.get(job_url, headers={'Authorization': 'Bearer ' + token}, json={})
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())
        data = await resp.json()
        return data


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def wait_for_job(session, job_url, token):
    reported_delay = False
    old_job_status  = 0
    printed_len = 0
    iterations_since_change=0
    error_iterations = 0
    while True:
        try:
            resp = await session.get(job_url, headers={'Authorization': 'Bearer ' + token}, json={'log-offset': printed_len})
            async with resp:
                if resp.status == 200:
                    error_iterations = 0
                    job = await resp.json()
                    job_status = job['status']
                    if job_status == 0 and not reported_delay:
                        reported_delay = True
                        start_after_struct = job.get("start_after", None)
                        if start_after_struct:
                            start_after = start_after_struct.get("secs_since_epoch", None)
                            now = time.time()
                            if start_after and start_after > now:
                                print("Waiting %d seconds before starting job" % (int(start_after - now)))
                    if job_status > 0 and old_job_status == 0:
                        print("/ Job was started");
                    old_job_status = job_status
                    log = job['log']
                    if len(log) > 0:
                        iterations_since_change=0
                        for line in log.splitlines(True):
                            print("| %s" % line, end="")
                        printed_len = printed_len + len(log)
                    else:
                        iterations_since_change=iterations_since_change+1
                    if job_status > 1:
                        if job_status == 2:
                            print("\\ Job completed successfully")
                        else:
                            print("\\ Job failed")
                            raise FailedJobError(job)
                        return job
                else:
                    iterations_since_change=4 # Start at 4 so we ramp up the delay faster
                    error_iterations=error_iterations + 1
                    if error_iterations <= 5:
                        print("Unexpected response %s getting job log, ignoring" % resp.status)
                    else:
                        raise ApiError(resp, await resp.text())
        except OSError as e:
            if e.args[0] == errno.ECONNRESET:
                # Client disconnected, retry
                # Not sure exactly why, but i got a lot of ConnectionResetErrors here
                # in tests. I guess the server stops reusing a http2 session after a bit
                # Should be fine to retry with the backof
                pass
            else:
                raise
        # Some polling backoff to avoid loading the server
        if iterations_since_change <= 1:
            sleep_time=1
        elif iterations_since_change < 5:
            sleep_time=3
        elif iterations_since_change < 15:
            sleep_time=5
        elif iterations_since_change < 30:
            sleep_time=10
        else:
            sleep_time=60
        time.sleep(sleep_time)

@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def wait_for_checks(session, build_url, token):
    print("Waiting for checks, if any...")
    while True:
        resp = await session.get(build_url + "/extended", headers={'Authorization': 'Bearer ' + token})
        async with resp:
            if resp.status == 404:
                return
            if resp.status >= 500:
                raise ServerApiError(resp, await resp.text())
            elif resp.status != 200:
                raise ApiError(resp, await resp.text())

            build = await resp.json()

        # wait for the repo to be validated
        if build["build"]["repo_state"] == 1:
            time.sleep(2)
        else:
            break

    for check in build["checks"]:
        print("Waiting for check: %s" % (check["check_name"]))
        job_url = build_url + "/check/" + check["check_name"] + "/job"
        await wait_for_job(session, job_url, token)

    resp = await session.get(build_url + "/extended", headers={'Authorization': 'Bearer ' + token})
    async with resp:
        if resp.status == 404:
            return
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())

        build = await resp.json()
        for check in build["checks"]:
            if check["status"] == 3:
                print("\\ Check {} has failed".format(check["check_name"]))
                raise FailedJobError(check)

@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def commit_build(session, build_url, eol, eol_rebase, token_type, wait, token):
    print("Committing build %s" % (build_url))
    json = {
        "endoflife": eol,
        "endoflife_rebase": eol_rebase
    }
    if token_type != None:
        json['token_type'] = token_type
    resp = await session.post(build_url + "/commit", headers={'Authorization': 'Bearer ' + token}, json=json)
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())

        job = await resp.json()
        job_url = resp.headers['location'];

        if wait:
            pass

        print("Waiting for commit job")
        await wait_for_checks(session, build_url, token)
        job = await wait_for_job(session, job_url, token)

        reparse_job_results(job)
        job["location"] = job_url
        return job


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def publish_build(session, build_url, wait, token):
    print("Publishing build %s" % (build_url))
    resp = await session.post(build_url + "/publish", headers={'Authorization': 'Bearer ' + token}, json= { } )
    async with resp:
        if resp.status == 400:
            body = await resp.json()
            try:
                if isinstance(body, str):
                    body = json.loads(body)

                current_state = body.get("current-state", None)

                if current_state == "published":
                    print("the build has been already published")
                    return {}
                elif current_state == "failed":
                    print("the build has failed")
                    raise ApiError(resp, await resp.text())
                elif current_state == "validating":
                    print("the build is still being validated or held for review")
                    return {}
            except:
                pass

        if resp.status != 200:
            raise ApiError(resp, await resp.text())

        job = await resp.json()
        job_url = resp.headers['location'];

        if wait:
            print("Waiting for publish job")
            job = await wait_for_job(session, job_url, token);

        reparse_job_results(job)
        job["location"] = job_url
        return job


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def purge_build(session, build_url, token):
    print("Purging build %s" % (build_url))
    resp = await session.post(build_url + "/purge", headers={'Authorization': 'Bearer ' + token}, json= {} )
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())
        return await resp.json()


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def create_token(session, manager_url, token, name, subject, scope, duration):
    token_url = urljoin(manager_url, "api/v1/token_subset")
    resp = await session.post(token_url, headers={'Authorization': 'Bearer ' + token}, json = {
        "name": name,
        "sub": subject,
        "scope": scope,
        "duration": duration,
    })
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())
        return await resp.json()

def get_object_multipart(repo_path, object):
    return AsyncNamedFilePart(repo_path + "/objects/" + object[:2] + "/" + object[2:], filename=object)


@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def create_command(session, args):
    build_url = urljoin(args.manager_url, "api/v1/build")
    json = {
        "repo": args.repo
    }
    if args.app_id is not None:
        json["app-id"] = args.app_id
    if args.public_download is not None:
        json["public-download"] = args.public_download
    if args.build_log_url is not None:
        json["build-log-url"] = args.build_log_url
    resp = await session.post(build_url, headers={'Authorization': 'Bearer ' + args.token}, json=json)
    async with resp:
        if resp.status >= 500:
            raise ServerApiError(resp, await resp.text())
        elif resp.status != 200:
            raise ApiError(resp, await resp.text())
        data = await resp.json()
        data["location"] = resp.headers['location']
        if not args.print_output:
            print(resp.headers['location'])
        return data

def delta_name_part_encode(commit):
    return base64.b64encode(binascii.unhexlify(commit), b"+_")[:-1].decode("utf-8")

def delta_name_encode (delta):
    return "-".join(map(delta_name_part_encode, delta.split("-")))

def should_skip_delta(id, globs):
    if globs:
        for glob in globs:
            if fnmatch.fnmatch(id, glob):
                return True
    return False

def build_url_to_api(build_url):
    parts = urlparse(build_url)
    path = os.path.dirname(os.path.dirname(parts.path))
    return urlunparse((parts.scheme, parts.netloc, path, None, None, None))

@retry(
    stop=TENACITY_STOP_AFTER,
    wait=TENACITY_WAIT_BETWEEN,
    retry=TENACITY_RETRY_EXCEPTIONS,
    reraise=True,
)
async def push_command(session, args):
    local_repo = OSTree.Repo.new(Gio.File.new_for_path(args.repo_path))
    try:
        local_repo.open(None)
    except GLib.Error as err:
        raise UsageException("Can't open repo %s: %s" % (args.repo_path, err.message)) from err

    refs = {}
    if len(args.branches) == 0:
        _, all_refs = local_repo.list_refs(None, None)
        for ref in all_refs:
            if ref.startswith("app/") or ref.startswith("runtime/") or ref.startswith("screenshots/"):
                refs[ref] = all_refs[ref]
    else:
        for branch in args.branches:
            _, rev = local_repo.resolve_rev(branch, False)
            refs[branch] = rev

    if (args.minimal_token):
        id = os.path.basename(urlparse(args.build_url).path)
        token = create_token(args.build_url, args.token, "minimal-upload", "build/%s" % (id), ["upload"], 60*60)["token"]
    else:
        token = args.token

    print("Uploading refs to %s: %s"% (args.build_url, list(refs)))

    metadata_objects = local_needed_metadata(local_repo, refs.values())

    print("Refs contain %d metadata objects" % (len(metadata_objects)))

    missing_metadata_objects = await missing_objects(session, args.build_url, token, list(metadata_objects))

    print("Remote missing %d of those" % (len(missing_metadata_objects)))

    file_objects = local_needed_files(local_repo, missing_metadata_objects)
    print("Has %d file objects for those" % (len(file_objects)))

    missing_file_objects = await missing_objects(session, args.build_url, token, list(file_objects))
    print("Remote missing %d of those" % (len(missing_file_objects)))

    # First upload all missing file objects
    print("Uploading file objects")
    await upload_objects(session, args.repo_path, args.build_url, token, missing_file_objects)

    # Then all the metadata
    print("Uploading metadata objects")
    await upload_objects(session, args.repo_path, args.build_url, token, missing_metadata_objects)

    _, deltas = local_repo.list_static_delta_names()
    print("Uploading deltas")
    await upload_deltas(session, args.repo_path, args.build_url, token, deltas, refs, args.ignore_delta)

    # Then the refs
    for ref, commit in refs.items():
        await create_ref(session, args.build_url, token, ref, commit, build_log_url=args.build_log_url)

    # Then any extra ids
    if args.extra_id:
        await add_extra_ids(session, args.build_url, token, args.extra_id)

    commit_job = None
    publish_job = None
    update_job = None

    # Note, this always uses the full token, as the minimal one only has upload permissions
    if args.commit or args.publish:
        commit_job = await commit_build(session, args.build_url, args.end_of_life, args.end_of_life_rebase, args.token_type, args.publish or args.wait, args.token)

    if args.publish:
        publish_job = await publish_build(session, args.build_url, args.wait or args.wait_update, args.token)
        update_job_id = publish_job.get("results", {}).get("update-repo-job", None)
        if update_job_id:
            print("Queued repo update job %d" %(update_job_id))
            update_job_url = build_url_to_api(args.build_url) + "/job/" + str(update_job_id)
            if args.wait_update:
                print("Waiting for repo update job")
                update_job = await wait_for_job (session, update_job_url, token);
            else:
                update_job = await get_job(session, update_job_url, token)
            reparse_job_results(update_job)
            update_job["location"] = update_job_url

    data = await get_build(session, args.build_url, args.token)
    if commit_job:
        data["commit_job"] = commit_job
    if publish_job:
        data["publish_job"] = publish_job
    if update_job:
        data["update_job"] = update_job
    return data

async def commit_command(session, args):
    job = await commit_build(session, args.build_url, args.end_of_life, args.end_of_life_rebase, args.token_type, args.wait, args.token)
    return job

async def publish_command(session, args):
    job = await publish_build(session, args.build_url, args.wait or args.wait_update, args.token)
    update_job_id = job.get("results", {}).get("update-repo-job", None)
    if update_job_id:
        print("Queued repo update job %d" %(update_job_id))
        update_job_url = build_url_to_api(args.build_url) + "/job/" + str(update_job_id)
        if args.wait_update:
            print("Waiting for repo update job")
            update_job = await wait_for_job(session, update_job_url, args.token);
        else:
            update_job = await get_job(session, update_job_url, args.token)
        reparse_job_results(update_job)
        update_job["location"] = update_job_url
    return job

async def purge_command(session, args):
    job = await purge_build(session, args.build_url, args.token)
    return job

async def create_token_command(session, args):
    data = await create_token(session, args.manager_url, args.token, args.name, args.subject, args.scope, args.duration)
    if not args.print_output:
        print(data['token'])
    return data

async def follow_job_command(session, args):
    job = await wait_for_job(session, args.job_url, args.token)
    return job

async def run_with_session(args):
    timeout = aiohttp.ClientTimeout(total=90*60)
    async with aiohttp.ClientSession(timeout=timeout) as session:
        result = await args.func(session, args)
    return result

if __name__ == '__main__':
    progname = os.path.basename(sys.argv[0])

    parser = ArgumentParser(prog=progname)
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='enable verbose output')
    parser.add_argument('--debug', action='store_true',
                        help='enable debugging output')
    parser.add_argument('--output', help='Write output json to file')
    parser.add_argument('--print-output', action='store_true', help='Print output json')
    parser.add_argument('--token', help='use this token')
    parser.add_argument('--token-file', help='use token from file')
    subparsers = parser.add_subparsers(title='subcommands',
                                       dest='subparser_name',
                                       description='valid subcommands',
                                       help='additional help')

    create_parser = subparsers.add_parser('create', help='Create new build')
    create_parser.add_argument('manager_url', help='remote repo manager url')
    create_parser.add_argument('repo', help='repo name')
    create_parser.add_argument('app_id', nargs='?', help='app ID')
    create_parser.add_argument('--public_download', action='store_true', default=None, help='allow public read access to the build repo')
    create_parser.add_argument('--no_public_download', action='store_false', dest='public_download', default=None, help='allow public read access to the build repo')
    create_parser.add_argument('--build-log-url', help='Set URL of the build log for the whole build')
    create_parser.set_defaults(func=create_command)

    push_parser = subparsers.add_parser('push', help='Push to repo manager')
    push_parser.add_argument('build_url', help='remote build url')
    push_parser.add_argument('repo_path', help='local repository')
    push_parser.add_argument('branches', nargs='*', help='branches to push')
    push_parser.add_argument('--commit', action='store_true',
                             help='commit build after pushing')
    push_parser.add_argument('--publish', action='store_true',
                             help='publish build after committing')
    push_parser.add_argument('--extra-id', action='append', help='add extra collection-id')
    push_parser.add_argument('--ignore-delta', action='append', help='don\'t upload deltas matching this glob')
    push_parser.add_argument('--wait', action='store_true',
                             help='wait for commit/publish to finish')
    push_parser.add_argument('--wait-update', action='store_true',
                             help='wait for update-repo to finish')
    push_parser.add_argument('--minimal-token', action='store_true',
                             help='Create minimal token for the upload')
    push_parser.add_argument('--end-of-life', help='Set end of life')
    push_parser.add_argument('--end-of-life-rebase', help='Set new ID which will supercede the current one')
    push_parser.add_argument('--token-type', help='Set token type', type=int)
    push_parser.add_argument('--build-log-url', help='Set URL of the build log for each uploaded ref')
    push_parser.set_defaults(func=push_command)

    commit_parser = subparsers.add_parser('commit', help='Commit build')
    commit_parser.add_argument('--wait', action='store_true',
                             help='wait for commit to finish')
    commit_parser.add_argument('--end-of-life', help='Set end of life')
    commit_parser.add_argument('--end-of-life-rebase', help='Set new ID which will supercede the current one')
    commit_parser.add_argument('--token-type', help='Set token type', type=int)
    commit_parser.add_argument('build_url', help='remote build url')
    commit_parser.set_defaults(func=commit_command)

    publish_parser = subparsers.add_parser('publish', help='Publish build')
    publish_parser.add_argument('--wait', action='store_true',
                             help='wait for publish to finish')
    publish_parser.add_argument('--wait-update', action='store_true',
                             help='wait for update-repo to finish')
    publish_parser.add_argument('build_url', help='remote build url')
    publish_parser.set_defaults(func=publish_command)

    purge_parser = subparsers.add_parser('purge', help='Purge build')
    purge_parser.add_argument('build_url', help='remote build url')
    purge_parser.set_defaults(func=purge_command)

    create_token_parser = subparsers.add_parser('create-token', help='Create subset token')
    create_token_parser.add_argument('manager_url', help='remote repo manager url')
    create_token_parser.add_argument('name', help='Name')
    create_token_parser.add_argument('subject', help='Subject')
    create_token_parser.add_argument('scope', nargs='*', help='Scope')
    create_token_parser.add_argument('--duration', help='Duration until expires, in seconds',
                                     default=60*60*24, # Default duration is one day
                                     type=int)
    create_token_parser.set_defaults(func=create_token_command)

    follow_job_parser = subparsers.add_parser('follow-job', help='Follow existing job log')
    follow_job_parser.add_argument('job_url', help='url of job')
    follow_job_parser.set_defaults(func=follow_job_command)

    args = parser.parse_args()

    loglevel = logging.WARNING
    if args.verbose:
        loglevel = logging.INFO
    if args.debug:
        loglevel = logging.DEBUG

    logging.basicConfig(format='%(module)s: %(levelname)s: %(message)s',
                        level=loglevel, stream=sys.stderr)

    if not args.subparser_name:
        print("No subcommand specified, see --help for usage")
        exit(1)

    if not args.token:
        if args.token_file:
            file = open(args.token_file, 'rb')
            args.token = file.read().splitlines()[0].decode("utf-8").strip()
        elif "REPO_TOKEN" in os.environ:
            args.token = os.environ["REPO_TOKEN"]
        else:
            print("No token available, pass with --token, --token-file or $REPO_TOKEN")
            exit(1)


    res = 1
    output = None
    try:
        result = asyncio.run(run_with_session(args))

        output = {
            "command": args.subparser_name,
            "result": result,
        }
        res = 0
    except SystemExit:
        # Something called sys.exit(), lets just exit
        res = 1
        raise # Pass on regular exit callse
    except (ApiError, FailedJobError) as e:
        eprint(str(e))
        output = {
            "command": args.subparser_name,
            "error": e.repr(),
        }
    except UsageException as e:
        eprint(str(e))
        output = {
            "error": {
                "type": "usage",
                "details": {
                    "message": str(e),
                }
            }
        }
    except:
        ei = sys.exc_info()
        eprint("Unexpected %s exception in %s: %s" % (ei[0].__name__, args.subparser_name, ei[1]))
        eprint(traceback.format_exc())
        output = {
            "command": args.subparser_name,
            "error": {
                "type": "exception",
                "details": {
                    "error-type": ei[0].__name__,
                    "message": str(ei[1]),
                }
            }
        }
        res = 1

    if output:
        if args.print_output:
            print(json.dumps(output, indent=4))
        if args.output:
            f = open(args.output,"w+")
            f.write(json.dumps(output, indent=4))
            f.write("\n")
            f.close()
    exit(res)