files.py 25.7 KB
Newer Older
David Runge's avatar
David Runge committed
1
2
import shutil
import tempfile
3
import time
David Runge's avatar
David Runge committed
4
5
import zipfile
from pathlib import Path
6
from typing import List, Optional, Tuple
David Runge's avatar
David Runge committed
7
8
9

import orjson
from prometheus_client.parser import text_fd_to_metric_families
10
from pydantic import BaseModel
David Runge's avatar
David Runge committed
11

12
13
from arch_release_promotion.config import ProjectConfig, Settings
from arch_release_promotion.gitlab import Upstream
14
15
16
17
18
19
from arch_release_promotion.release import (
    AmountMetric,
    Release,
    SizeMetric,
    VersionMetric,
)
David Runge's avatar
David Runge committed
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

TEMP_DIR_PREFIX = "arp-"


def files_in_dir(path: Path) -> List[str]:
    """Return the files in a directory as a list of strings

    Parameters
    ----------
    path: Path
        The path for which to list files

    Raises
    ------
    RuntimeError
        If the provided path is not a directory

    Returns
    -------
    List[str]
        A list of strings representing the files in a directory
    """

    if not path.is_dir():
        raise RuntimeError(f"The path is not a path: {path}")

    return [child.name for child in path.iterdir()]


def get_version_from_artifact_release_dir(path: Path) -> str:
    """Extrapolate the version of a given release path

    Parameters
    ----------
    path: Path
        The path to a release

    Raises
    ------
    RuntimeError
        If the provided path is not a directory

    Returns
    -------
    str
        The version string for the release
    """

    if not path.is_dir():
        raise RuntimeError(f"The path for the release is not a directory: {path}")

    version = path.name.split("-")[1]
    if len(version) < 1:
        raise RuntimeError(f"The extracted version string '{version}' from path '{path}' is not valid.")

    return version


def create_temp_dir() -> Path:
    """Create a temporary directory

    Returns
    -------
    Path
        The path to the directory
    """

    return Path(tempfile.mkdtemp(prefix=TEMP_DIR_PREFIX))


def remove_temp_dir(path: Path) -> None:
    """Remove a temporary directory recursively

    Parameters
    ----------
    path: Path
        The path to a directory to remove

    Raises
    ------
    RuntimeError
        If the to be removed path is not a directory or if its name does not contain TEMP_DIR_PREFIX
    """

    if not path.is_dir():
        raise RuntimeError(f"The path to remove is not a directory: {path}")

    if TEMP_DIR_PREFIX not in path.name:
        raise RuntimeError(f"Can not remove a temporary directory, that is not created by the same tool: {path}")

    shutil.rmtree(path=path)


def extract_zip_file_to_parent_dir(path: Path) -> None:
    """Extract the contents of a ZIP file to the parent directory of that file

    Parameters
    ----------
    path: Path
        The path to a ZIP file

    Raises
    ------
    RuntimeError
        If the path does not specify a valid zip file
    """

    if not zipfile.is_zipfile(path):
        raise RuntimeError(f"The file is not a ZIP file: {path}")

    with zipfile.ZipFile(file=path, mode="r") as zip_file:
        zip_file.extractall(path=path.parent)


def copy_signatures(source: Path, destination: Path) -> None:
    """Copy any signature files from a source directory to a destination directory

    Parameters
    ----------
    source: Path
        The source directory to copy signature files from
    destination: Path
        The destination directory to copy signature files to

    Raises
    ------
    RuntimeError
        If either source or destination is not a directory
    """

    for path in [source, destination]:
        if not path.is_dir():
            raise RuntimeError("The specified path is not a directory: {path}")

    for file in source.iterdir():
        if file.suffix in [".sig"]:
            shutil.copy(src=file, dst=destination)


def write_release_info_to_file(release: Release, path: Path) -> None:
    """Write a Release instance to a JSON file

    Parameters
    ----------
    release: Release
        A release instance that will be serialized to JSON
    path: Path
        The file to write the JSON string to

    Raises
    ------
    IsADirectoryError:
        If the path is a directory
    OSError:
        If the file path is not writable
    """

    with open(path, "wb") as file:
        file.write(
            orjson.dumps(release.dict(), option=orjson.OPT_INDENT_2 | orjson.OPT_APPEND_NEWLINE | orjson.OPT_SORT_KEYS)
        )


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def load_release_from_json_payload(path: Path) -> Release:
    """Read a JSON payload and return it as a Release instance

    Parameters
    ----------
    path: Path
        The path to a file containing a JSON payload

    Returns
    -------
    Release
        A Release instance reflecting the data from the JSON payload
    """

    with open(path, "r") as file:
        return Release(**orjson.loads(file.read()))


David Runge's avatar
David Runge committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def write_zip_file_to_parent_dir(path: Path, name: str = "promotion", format: str = "zip") -> None:
    """Create ZIP file of all contents in a directory and write it to the directory's parent

    Parameters
    ----------
    path: Path
        The path to add to the ZIP file
    name: str
        The name to use for the compressed file (defaults to "promotion")
    format: str
        The compressed file format to use (defaults to "zip")
    """

    known_formats = ["zip", "tar", "gztar", "bztar", "xztar"]
    if len(name) < 1:
        raise RuntimeError("The file name has to be at least one char long, but empty string was provided.")
    if format not in known_formats:
        raise RuntimeError(f"The format must be one of {known_formats}, but {format} is provided.")

    shutil.make_archive(base_name=str(path.parent / Path(name)), format=format, root_dir=path)


223
224
225
226
227
228
229
def read_metrics_file(
    path: Path,
    version_metrics_names: Optional[List[str]],
    size_metrics_names: Optional[List[str]],
    amount_metrics_names: Optional[List[str]],
) -> Tuple[List[AmountMetric], List[SizeMetric], List[VersionMetric]]:
    """Read a metrics file that contains openmetrics based metrics and return those that match the respective keywords
David Runge's avatar
David Runge committed
230
231
232
233
234

    Parameters
    ----------
    path: Path
        The path of the file to read
235
236
237
238
239
240
    version_metrics_names: Optional[List[str]]
        A list of metric names to search for in the labels of metric samples of type "info"
    size_metrics_names: Optional[List[str]],
        A list of metric names to search for in the labels of metric samples of type "gauge"
    amount_metrics_names: Optional[List[str]],
        A list of metric names to search for in the labels of metric samples of type "summary"
David Runge's avatar
David Runge committed
241
242
243

    Returns
    -------
244
245
    Tuple[List[AmountMetric], List[SizeMetric], List[VersionMetric]]:
        A Tuple with lists of AmountMetric, SizeMetric and VersionMetric instances derived from the input file
David Runge's avatar
David Runge committed
246
247
    """

248
249
250
    amount_metrics: List[AmountMetric] = []
    size_metrics: List[SizeMetric] = []
    version_metrics: List[VersionMetric] = []
David Runge's avatar
David Runge committed
251

252
253
    if path.exists():
        with open(path, "r") as file:
254
            for metric in text_fd_to_metric_families(file):  # type: ignore[no-untyped-call]
David Runge's avatar
David Runge committed
255
256
                for sample in metric.samples:
                    if (
257
258
259
260
                        version_metrics_names
                        and metric.type == "info"
                        and metric.name == "version_info"
                        and sample.labels.get("name") in version_metrics_names
David Runge's avatar
David Runge committed
261
262
263
                        and sample.labels.get("description")
                        and sample.labels.get("version")
                    ):
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
                        version_metrics += [
                            VersionMetric(
                                name=sample.labels.get("name"),
                                description=sample.labels.get("description"),
                                version=sample.labels.get("version"),
                            )
                        ]
                    if (
                        size_metrics_names
                        and metric.type == "gauge"
                        and metric.name == "artifact_bytes"
                        and sample.labels.get("name") in size_metrics_names
                        and sample.labels.get("description")
                        and sample.value
                    ):
                        size_metrics += [
                            SizeMetric(
                                name=sample.labels.get("name"),
                                description=sample.labels.get("description"),
                                size=sample.value,
                            )
                        ]
                    if (
                        amount_metrics_names
                        and metric.type == "summary"
                        and metric.name == "data_count"
                        and sample.labels.get("name") in amount_metrics_names
                        and sample.labels.get("description")
                        and sample.value
                    ):
                        amount_metrics += [
                            AmountMetric(
                                name=sample.labels.get("name"),
                                description=sample.labels.get("description"),
                                amount=sample.value,
                            )
                        ]
301

302
    return (amount_metrics, size_metrics, version_metrics)
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


def create_dir(path: Path) -> Path:
    """Create a directory

    Parameters
    ----------
    path: Path
        The path for which to create a directory

    Raises
    ------
    RuntimeError
        If the path exists but is not a directory

    Returns
    -------
    Path
        The path representing the existing, absolute directory
    """

    path = path.resolve(strict=False)

    if path.exists() and not path.is_dir():
        raise RuntimeError(f"The provided path is not a directory: {path}")
    else:
        path.mkdir(parents=True, exist_ok=True)

    return path


class ProjectFiles(BaseModel):
    """A pydantic model to operate on a project's files and releases

    Attributes
    ----------
    project_config: ProjectConfig
        A ProjectConfig instance describing the project
    upstream: Upstream
        An Upstream instance used for queries to releases of a project
    promoted_releases: List[str]
        A list of version strings
    """

    project_config: ProjectConfig
    upstream: Upstream
    promoted_releases: List[str]

    class Config:
        arbitrary_types_allowed = True

    def __init__(self, project_config: ProjectConfig, settings: Settings) -> None:
        """A custom constructor to initialize an instance of ProjectFiles

        The names of the configured maximum number of promoted releases is retrieved using an Upstream instance.
        If the project's sync_dir does not exist, it will be created.

        Parameters
        ----------
        project_config: ProjectConfig
            A ProjectConfig instance describing the project
        settings: Settings
            A Settings instance used to initialize an Upstream instance
        """

        upstream = Upstream(
            url=settings.GITLAB_URL,
            private_token=None,
            name=project_config.name,
        )
        promoted_releases = upstream.get_releases(
            max_releases=project_config.sync_config.backlog,  # type: ignore
            promoted=True,
        )
        print(f"Synchronizing release versions for {project_config.name}: " f"{', '.join(promoted_releases)}")

        create_dir(path=project_config.sync_config.directory)  # type: ignore

        super().__init__(
            project_config=project_config,
            upstream=upstream,
            promoted_releases=promoted_releases,
        )

    @classmethod
    def sync(self, project_config: ProjectConfig, settings: Settings) -> None:
        """A factory method to initialize an instance of ProjectFiles and synchronize all of its promoted_releases

        Parameters
        ----------
        project_config: ProjectConfig
            A ProjectConfig instance describing the project
        settings: Settings
            A Settings instance used to initialize an Upstream instance
        """

        change_state: List[bool] = []

        project_files = self(
            project_config=project_config,
            settings=settings,
        )

        for child in project_files.project_config.sync_config.directory.iterdir():  # type: ignore
            if child.name.startswith(".tmp-"):
                print(f"Removing pre-existing temporary directory: {child}")
                shutil.rmtree(child)

        with tempfile.TemporaryDirectory(
            prefix=".tmp-",
            dir=project_files.project_config.sync_config.directory  # type: ignore
            if project_files.project_config.sync_config.temp_in_sync_dir  # type: ignore
            else None,
        ) as temp_dir_base_name:
            for promoted_release in project_files.promoted_releases:
                change_state += [
                    project_files._sync_version(temp_dir_base=Path(temp_dir_base_name), version=promoted_release)
                ]

        change_state += [project_files._set_latest_version_symlink()]
        change_state += [project_files._remove_obsolete_releases()]

        if any(change_state):
            project_files._set_last_update_file_timestamp()

    def _set_last_update_file_timestamp(self) -> None:
        """Write the current seconds since the epoch to a "last update file" if it is configured"""

        if self.project_config.sync_config.last_updated_file:  # type: ignore
            print(f"Updating timestamp in {self.project_config.sync_config.last_updated_file}...")  # type: ignore
            with open(self.project_config.sync_config.last_updated_file, "w") as file:  # type: ignore
                file.write(f"{int(time.time())}")

            print("Done!")

    def _remove_obsolete_releases(self) -> bool:
        """Remove obsolete releases of a project from its sync_dir"""

        state: List[bool] = []
        expected_dirs: List[Path] = []
        expected_files: List[Path] = []

        for release_type in self.project_config.releases:
            release_type_dir = self.project_config.sync_config.directory / Path(release_type.name)  # type: ignore
            print(f"Removing obsolete release files from '{release_type_dir}'...")

            expected_dirs += [release_type_dir / Path("latest")]
            expected_dirs += [
                release_type_dir / Path(f"{release_type.name}-{version}") for version in self.promoted_releases
            ]
            expected_files += [
                release_type_dir / Path(f"{release_type.name}-{version}.json") for version in self.promoted_releases
            ]
            expected_files += [
                release_type_dir / Path(f"{release_type.name}-{version}.torrent") for version in self.promoted_releases
            ]

            for file in release_type_dir.iterdir():
                if file.is_dir() and file not in expected_dirs:
                    print(f"Removing directory '{file}'")
                    shutil.rmtree(path=file)
                    state += [True]
                if file.is_file() and file not in expected_files:
                    print(f"Removing file '{file}'")
                    file.unlink()
                    state += [True]

            print("Done!")

        return any(state)

    def _set_latest_version_symlink(self) -> bool:
        """Set the symlink to the latest version in a project's sync_dir"""

        state: List[bool] = []

        if self.promoted_releases:
            latest_version = sorted(self.promoted_releases)[-1]

            for release_type in self.project_config.releases:
                latest_link = (
                    self.project_config.sync_config.directory / Path(release_type.name) / Path("latest")  # type: ignore
                )
                release_dir = Path(f"{release_type.name}-{latest_version}")
                print(f"Establishing '{latest_version}' as latest release version for '{release_type.name}'...")

                if latest_link.exists():
                    if (latest_link.is_symlink() and latest_link.readlink() != release_dir) or latest_link.is_file():
                        latest_link.unlink()
                    if not latest_link.is_symlink() and latest_link.is_dir():
                        shutil.rmtree(path=latest_link)
                if not latest_link.exists():
                    latest_link.symlink_to(release_dir)
                    state += [True]

            print("Done!")

        return any(state)

    def _sync_version(self, temp_dir_base: Optional[Path], version: str) -> bool:
        """Synchronize a project's (release) version

        Download a project release's promotion artifact and use it to establish whether the release version has been
        synchronized fully to the project's sync_dir.
        Download the project release's build artifact, if the release version is not yet (fully) synchronized and move
        the combined artifacts to the project's sync_dir.

        Parameters
        ----------
        temp_dir_base: Optional[Path]
            The directory to use as base for creating temporary directories in when downloading and moving files
        version: str
            The project's release version

        Returns
        -------
        bool
            True if the synchronization introduced changes in the synchronization directory, False otherwise
        """

        with tempfile.TemporaryDirectory(prefix=TEMP_DIR_PREFIX, dir=temp_dir_base) as promotion_temp_dir_name:
            promotion_temp_dir = Path(promotion_temp_dir_name)
            extract_zip_file_to_parent_dir(
                path=self.upstream.download_promotion_artifact(
                    tag_name=version,
                    temp_dir=promotion_temp_dir,
                )
            )
            if self._project_version_requires_sync(version=version):
                with tempfile.TemporaryDirectory(prefix=TEMP_DIR_PREFIX, dir=temp_dir_base) as build_temp_dir_name:
                    build_temp_dir = Path(build_temp_dir_name)
                    build_artifact = self.upstream.download_release(
                        tag_name=version,
                        temp_dir=build_temp_dir,
                        job_name=self.project_config.job_name,
                    )
                    extract_zip_file_to_parent_dir(path=build_artifact)

                    for release_type in [
                        load_release_from_json_payload(path=json_payload)
                        for json_payload in promotion_temp_dir.glob("*/*.json")
                    ]:
                        self.copy_release_type_promotion_artifacts_to_build_dir(
                            release_type=release_type,
                            source_base=promotion_temp_dir,
                            destination_base=build_temp_dir / self.project_config.output_dir,
                        )

                        self.validate_release_type_files(
                            release_type=release_type,
                            path=build_temp_dir / self.project_config.output_dir,
                        )
                        self.move_release_type_to_sync_dir(
                            release=release_type,
                            source_base=build_temp_dir / self.project_config.output_dir,
                            sync_dir=self.project_config.sync_config.directory,  # type: ignore
                        )

                    return True

            return False

    def _is_release_type_synced(self, name: str, version: str) -> bool:
        """Check whether a release type of a project's release version is synchronized fully already

        Parameters
        ----------
        name: str
            The release name
        version: str
            The release version

        Returns
        -------
        bool
            True if the project's release type in the specified version is fully synchronized, False otherwise
        """

        release_base = self.project_config.sync_config.directory / Path(name)  # type: ignore
        release_json = release_base / Path(f"{name}-{version}.json")
        if not release_json.exists():
            return False

        release = load_release_from_json_payload(path=release_json)

        if release.torrent_file and not (release_base / Path(release.torrent_file)).exists():
            return False

        release_dir = release_base / Path(f"{name}-{version}")
        for file in release.files:
            if not (release_dir / Path(file)).exists():
                return False

        return True

    def _project_version_requires_sync(
        self,
        version: str,
    ) -> bool:
        """Evaluate whether a project's release requires syncing

        Parameters
        ----------
        version: str
            The version of the project's release

        Returns
        -------
        bool
            True if any of the release types of the project are not yet fully synchronized, False otherwise
        """

        release_type_states: List[bool] = []
        for project_release_type in self.project_config.releases:
            if not self._is_release_type_synced(
                name=project_release_type.name,
                version=version,
            ):
                release_type_states += [True]
            else:
                release_type_states += [False]

        return any(release_type_states)

    @classmethod
    def copy_release_type_promotion_artifacts_to_build_dir(
        self,
        release_type: Release,
        source_base: Path,
        destination_base: Path,
    ) -> None:
        """Copy promotion artifacts of a release type to its respective build artifact directory

        Parameters
        ----------
        release_type: Release
            The Release instance describing the files to copy
        source_base: Path
            The base directory in which the promotion artifacts of the release type are located
        destination_base: Path
            The base directory in which the build artifacts of the release type are located
        """

        copy_signatures(
            source=source_base / Path(f"{release_type.name}/{release_type.name}-{release_type.version}"),
            destination=destination_base / Path(f"{release_type.name}/{release_type.name}-{release_type.version}"),
        )
        # move torrent file if it exists
        if release_type.torrent_file:
            (source_base / Path(f"{release_type.name}/{release_type.torrent_file}")).rename(
                destination_base / Path(f"{release_type.name}/{release_type.torrent_file}")
            )
        # move JSON payload
        (source_base / Path(f"{release_type.name}/{release_type.name}-{release_type.version}.json")).rename(
            (destination_base / Path(f"{release_type.name}/{release_type.name}-{release_type.version}.json"))
        )

    @classmethod
    def validate_release_type_files(self, release_type: Release, path: Path) -> None:
        """Validate the files of a release type

        Parameters
        ----------
        release_type: Release
            The Release instance describing the release type
        path: Path
            The directory in which the files for the release type are located

        Raises
        ------
        RuntimeError
            If one of the files is missing
        """

        print(f"Validating release type '{release_type.name}' version '{release_type.version}'...")

        json_payload = Path(f"{release_type.name}/{release_type.name}-{release_type.version}.json")
        if not (path / json_payload).exists():
            raise RuntimeError(f"The file '{json_payload}' does not exist.")

        torrent_file = Path(f"{release_type.name}/{release_type.name}-{release_type.version}.torrent")
        if release_type.torrent_file and not (path / torrent_file).exists():
            raise RuntimeError(f"The file '{torrent_file}' does not exist.")

        for file in release_type.files:
            file_path = Path(f"{release_type.name}/{release_type.name}-{release_type.version}/{file}")
            if not (path / file_path).exists():
                raise RuntimeError(f"The file '{file_path}' does not exist.")

        print("Done!")

    @classmethod
    def move_release_type_to_sync_dir(self, release: Release, source_base: Path, sync_dir: Path) -> None:
        """Move a validated version of a release type to a sync directory

        Parameters
        ----------
        release_type: Release
            The Release instance describing the release type
        source_base: Path
            The directory in which the files for the release type are located
        sync_dir: Path
            The directory to which the release type files are moved
        """

        print(f"Moving release type '{release.name}' version '{release.version}' to '{sync_dir}'...")

        release_type_base = sync_dir / Path(f"{release.name}")
        create_dir(release_type_base)
        destination_release_dir = release_type_base / Path(f"{release.name}-{release.version}")

        # if the destination exists already, remove it first
        if destination_release_dir.exists():
            if destination_release_dir.is_dir():
                shutil.rmtree(path=destination_release_dir)
            else:
                destination_release_dir.unlink()

        destination_release_dir.mkdir(parents=True)

        for file in release.files:
            shutil.move(
                src=source_base / Path(f"{release.name}/{release.name}-{release.version}/{file}"),
                dst=destination_release_dir / Path(f"{file}"),
            )

        if release.torrent_file:
            shutil.move(
                src=source_base / Path(f"{release.name}/{release.name}-{release.version}.torrent"),
                dst=release_type_base / Path(f"{release.name}-{release.version}.torrent"),
            )

        shutil.move(
            src=source_base / Path(f"{release.name}/{release.name}-{release.version}.json"),
            dst=release_type_base / Path(f"{release.name}-{release.version}.json"),
        )

        print("Done!")