Skip to content

Commit a7545af

Browse files
committed
(Re)add support to work with S3 buckets
Signed-off-by: Tobias Wolf <[email protected]>
1 parent 6278da3 commit a7545af

File tree

9 files changed

+364
-4
lines changed

9 files changed

+364
-4
lines changed

.github/actions/features_parse/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ outputs:
1111
runs:
1212
using: composite
1313
steps:
14-
- uses: gardenlinux/python-gardenlinux-lib/.github/actions/setup@0.7.3
14+
- uses: gardenlinux/python-gardenlinux-lib/.github/actions/setup@0.8.0
1515
- id: result
1616
shell: bash
1717
run: |

.github/actions/flavors_parse/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ outputs:
1313
runs:
1414
using: composite
1515
steps:
16-
- uses: gardenlinux/python-gardenlinux-lib/.github/actions/setup@0.7.3
16+
- uses: gardenlinux/python-gardenlinux-lib/.github/actions/setup@0.8.0
1717
- id: matrix
1818
shell: bash
1919
run: |

.github/actions/setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Installs the given GardenLinux Python library
33
inputs:
44
version:
55
description: GardenLinux Python library version
6-
default: "0.7.3"
6+
default: "0.8.0"
77
runs:
88
using: composite
99
steps:

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "gardenlinux"
3-
version = "0.7.3"
3+
version = "0.8.0"
44
description = "Contains tools to work with the features directory of gardenlinux, for example deducting dependencies from feature sets or validating cnames"
55
authors = ["Garden Linux Maintainers <[email protected]>"]
66
license = "Apache-2.0"
@@ -34,6 +34,7 @@ gl-cname = "gardenlinux.features.cname_main:main"
3434
gl-features-parse = "gardenlinux.features.__main__:main"
3535
gl-flavors-parse = "gardenlinux.flavors.__main__:main"
3636
gl-oci = "gardenlinux.oci.__main__:main"
37+
gl-s3 = "gardenlinux.s3.__main__:main"
3738

3839
[tool.pytest.ini_options]
3940
pythonpath = ["src"]

src/gardenlinux/features/cname.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ def feature_set(self) -> str:
128128

129129
return Parser().filter_as_string(self.flavor)
130130

131+
@property
132+
def platform(self) -> str:
133+
"""
134+
Returns the platform for the cname parsed.
135+
136+
:return: (str) Flavor
137+
"""
138+
139+
return re.split("[_-]", self._flavor, 1)[0]
140+
131141
@property
132142
def version(self) -> Optional[str]:
133143
"""

src/gardenlinux/s3/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
S3 module
5+
"""
6+
7+
from .bucket import Bucket
8+
from .s3_artifacts import S3Artifacts
9+
10+
__all__ = ["Bucket", "S3Artifacts"]

src/gardenlinux/s3/__main__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
gl-s3 main entrypoint
6+
"""
7+
8+
import argparse
9+
import os
10+
import re
11+
import sys
12+
from functools import reduce
13+
from pathlib import Path
14+
from typing import Any, List, Set
15+
16+
from .s3_artifacts import S3Artifacts
17+
18+
19+
_ARGS_ACTION_ALLOWED = [
20+
"download-artifacts-from-bucket",
21+
"upload-artifacts-to-bucket",
22+
]
23+
24+
25+
def main() -> None:
26+
"""
27+
gl-s3 main()
28+
29+
:since: 0.8.0
30+
"""
31+
32+
parser = argparse.ArgumentParser()
33+
34+
parser.add_argument("--bucket", dest="bucket")
35+
parser.add_argument("--cname", required=False, dest="cname")
36+
parser.add_argument("--path", required=False, dest="path")
37+
38+
parser.add_argument("action", nargs="?", choices=_ARGS_ACTION_ALLOWED)
39+
40+
args = parser.parse_args()
41+
42+
if args.action == "download-artifacts-from-bucket":
43+
S3Artifacts(args.bucket).download_to_directory(args.cname, args.path)
44+
elif args.action == "upload-artifacts-to-bucket":
45+
S3Artifacts(args.bucket).upload_from_directory(args.cname, args.path)

src/gardenlinux/s3/bucket.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
S3 bucket
5+
"""
6+
7+
import boto3
8+
import logging
9+
from typing import Any, Optional
10+
11+
from ..logger import LoggerSetup
12+
13+
14+
class Bucket(object):
15+
"""
16+
S3 bucket class
17+
18+
:author: Garden Linux Maintainers
19+
:copyright: Copyright 2024 SAP SE
20+
:package: gardenlinux
21+
:subpackage: s3
22+
:since: 0.8.0
23+
:license: https://www.apache.org/licenses/LICENSE-2.0
24+
Apache License, Version 2.0
25+
"""
26+
27+
def __init__(
28+
self,
29+
bucket_name: str,
30+
endpoint_url: Optional[str] = None,
31+
s3_resource_config: Optional[dict[str, Any]] = None,
32+
logger: Optional[logging.Logger] = None,
33+
):
34+
"""
35+
Constructor __init__(Bucket)
36+
37+
:param bucket_name: S3 bucket name
38+
:param endpoint_url: S3 endpoint URL
39+
:param s3_resource_config: Additional boto3 S3 config values
40+
:param logger: Logger instance
41+
42+
:since: 0.8.0
43+
"""
44+
45+
if logger is None or not logger.hasHandlers():
46+
logger = LoggerSetup.get_logger("gardenlinux.s3")
47+
48+
if s3_resource_config is None:
49+
s3_resource_config = {}
50+
51+
if endpoint_url is not None:
52+
s3_resource_config["endpoint_url"] = endpoint_url
53+
54+
self._s3_resource = boto3.resource("s3", **s3_resource_config)
55+
56+
self._bucket = self._s3_resource.Bucket(bucket_name)
57+
self._logger = logger
58+
59+
@property
60+
def objects(self):
61+
"""
62+
Returns a list of all objects in a bucket.
63+
64+
:return: (list) S3 bucket objects
65+
:since: 0.8.0
66+
"""
67+
68+
return self._bucket.objects.all()
69+
70+
def __getattr__(self, name):
71+
"""
72+
python.org: Called when an attribute lookup has not found the attribute in
73+
the usual places (i.e. it is not an instance attribute nor is it found in the
74+
class tree for self).
75+
76+
:param name: Attribute name
77+
78+
:return: (mixed) Attribute
79+
:since: 0.8.0
80+
"""
81+
82+
return getattr(self._bucket, name)

0 commit comments

Comments
 (0)