Skip to content

Commit 937d65e

Browse files
authored
Merge pull request #71 from kartoza/fixes
[skip-release] improve bucket creation
2 parents c44137d + 58f0dbe commit 937d65e

File tree

8 files changed

+105
-46
lines changed

8 files changed

+105
-46
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ updates:
33
- package-ecosystem: "github-actions"
44
directory: "/"
55
schedule:
6-
interval: "monthly"
6+
interval: "weekly"

docker-compose-s3.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ services:
2121
entrypoint: /bin/bash
2222
command: -c 'minio server /data --console-address ":9001"'
2323
volumes:
24-
- minio_data:/mapproxy
24+
- minio_data:/data
2525
ports:
2626
- "9000:9000"
2727
- "9001:9001"

mapproxy_configuration/mapproxy-s3.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ services:
3939
contact:
4040
person: Kartoza
4141
position: GIS Manager
42-
organization:
43-
address:
44-
city:
45-
postcode:
42+
organization: Kartoza
43+
address: Kartoza
44+
city: Cape Town
45+
postcode: 7700
4646
country: south Africa
47-
phone:
48-
email:
47+
phone: '276426345'
48+
email: info@kartoza.com
4949
access_constraints: Insert license and copyright information for this service.
5050
fees: "None"
5151
tms:

mapproxy_configuration/mapproxy.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ services:
3939
contact:
4040
person: Kartoza
4141
position: GIS Manager
42-
organization:
43-
address:
44-
city:
45-
postcode:
42+
organization: Kartoza
43+
address: Kartoza
44+
city: Cape Town
45+
postcode: 7700
4646
country: south Africa
47-
phone:
48-
email:
47+
phone: '276426345'
48+
email: info@kartoza.com
4949
access_constraints:
5050
Insert license and copyright information for this service.
5151
fees: 'None'

scenario_tests/s3/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ services:
1919
entrypoint: /bin/bash
2020
command: -c 'minio server /data --console-address ":9001"'
2121
volumes:
22-
- minio_data:/mapproxy
22+
- minio_data:/data
2323
ports:
2424
- "9000:9000"
2525
- "9001:9001"

scenario_tests/s3/mapproxy_configuration/mapproxy.yaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ services:
3939
contact:
4040
person: Kartoza
4141
position: GIS Manager
42-
organization:
43-
address:
44-
city:
45-
postcode:
42+
organization: Kartoza
43+
address: Kartoza
44+
city: Cape Town
45+
postcode: 7700
4646
country: south Africa
47-
phone:
48-
email:
47+
phone: '276426345'
48+
email: info@kartoza.com
4949
access_constraints: Insert license and copyright information for this service.
5050
fees: "None"
5151
tms:
@@ -70,7 +70,6 @@ caches:
7070
grids: [osm_grid]
7171
meta_size: [5, 5]
7272
meta_buffer: 20
73-
concurrent_tile_creators: 2
7473
cache:
7574
type: s3
7675
directory: /hillshade/

scripts/create_default_buckets.py

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,100 @@
1-
from os import environ as env
1+
import os
22
import re
3-
import boto3, botocore
3+
import logging
4+
from typing import List
5+
import boto3
6+
from botocore.exceptions import ClientError
47

5-
def check_bucket(s3, bucket_name):
8+
# Configure logging
9+
logging.basicConfig(level=logging.INFO)
10+
logger = logging.getLogger(__name__)
11+
12+
13+
def get_environment_variable(name: str, required: bool = True) -> str:
14+
"""
15+
Retrieves an environment variable or raises an error if it's missing.
16+
"""
17+
value = os.getenv(name)
18+
if required and not value:
19+
raise ValueError(f"Missing required environment variable: {name}")
20+
return value
21+
22+
23+
def parse_bucket_list(bucket_list: str) -> List[str]:
24+
"""
25+
Parses a string of bucket names separated by commas, spaces, or semicolons into a list.
26+
"""
27+
return re.split(r'[ ,;]+', bucket_list.strip())
28+
29+
30+
def check_bucket(s3_client, bucket_name: str) -> bool:
31+
"""
32+
Checks if a bucket exists and is accessible.
33+
34+
Returns True if the bucket exists or is private, False otherwise.
35+
"""
636
try:
7-
s3.head_bucket(Bucket=bucket_name)
8-
print(f"{bucket_name} available")
37+
s3_client.head_bucket(Bucket=bucket_name)
38+
logger.info(f"Bucket '{bucket_name}' is available.")
939
return True
10-
except botocore.exceptions.ClientError as e:
40+
except ClientError as e:
1141
error_code = int(e.response['Error']['Code'])
1242
if error_code == 403:
13-
print(f"{bucket_name} is Private. Access denied.")
43+
logger.warning(f"Bucket '{bucket_name}' is private. Access denied.")
1444
return True
1545
elif error_code == 404:
16-
print(f"{bucket_name} does not exist")
46+
logger.info(f"Bucket '{bucket_name}' does not exist.")
1747
return False
48+
else:
49+
logger.error(f"Error checking bucket '{bucket_name}': {e}")
50+
raise
51+
52+
53+
def create_bucket(s3_client, bucket_name: str):
54+
"""
55+
Creates a bucket if it does not exist.
56+
"""
57+
try:
58+
logger.info(f"Creating bucket '{bucket_name}'.")
59+
s3_client.create_bucket(Bucket=bucket_name)
60+
logger.info(f"Bucket '{bucket_name}' created successfully.")
61+
except ClientError as e:
62+
logger.error(f"Failed to create bucket '{bucket_name}': {e}")
63+
raise
64+
1865

1966
def main():
20-
buckets = env['S3_BUCKET_LIST']
21-
buckets = re.split(r',| |;', buckets)
22-
end_point = env['S3_BUCKET_ENDPOINT']
67+
"""
68+
Main function to check and create S3 buckets as needed.
69+
"""
70+
bucket_list_str = get_environment_variable('S3_BUCKET_LIST')
71+
buckets = parse_bucket_list(bucket_list_str)
72+
endpoint = get_environment_variable('S3_BUCKET_ENDPOINT')
2373

24-
session = boto3.session.Session()
74+
aws_access_key_id = get_environment_variable('AWS_ACCESS_KEY_ID')
75+
aws_secret_access_key = get_environment_variable('AWS_SECRET_ACCESS_KEY')
2576

77+
session = boto3.session.Session()
2678
s3 = session.client(
2779
service_name='s3',
28-
aws_access_key_id=env['AWS_ACCESS_KEY_ID'],
29-
aws_secret_access_key=env['AWS_SECRET_ACCESS_KEY'],
30-
endpoint_url=end_point,
80+
aws_access_key_id=aws_access_key_id,
81+
aws_secret_access_key=aws_secret_access_key,
82+
endpoint_url=endpoint,
3183
)
3284

33-
for i in buckets:
34-
if not check_bucket(s3, i):
35-
print(f"Creating {i}")
36-
s3.create_bucket(Bucket=i)
85+
for bucket_name in buckets:
86+
if not check_bucket(s3, bucket_name):
87+
create_bucket(s3, bucket_name)
88+
3789

38-
if __name__=="__main__":
39-
create_buckets = env['CREATE_DEFAULT_S3_BUCKETS']
40-
if create_buckets.lower() == 'true':
41-
print("Creating default buckets")
42-
main()
90+
if __name__ == "__main__":
91+
try:
92+
create_buckets = get_environment_variable('CREATE_DEFAULT_S3_BUCKETS', required=False)
93+
if create_buckets and create_buckets.lower() == 'true':
94+
logger.info("Starting bucket creation process.")
95+
main()
96+
else:
97+
logger.info("CREATE_DEFAULT_S3_BUCKETS is not set to 'true'. Skipping bucket creation.")
98+
except Exception as e:
99+
logger.error(f"An error occurred: {e}")
100+
exit(1)

scripts/start.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function entry_point_script {
3333
for f in /docker-entrypoint-mapproxy.d/*; do
3434
case "$f" in
3535
*.sh) echo "$0: running $f"; . "$f" || true;;
36+
*.py) echo "$0: running Python script $f"; python3 "$f" || true;;
3637
*) echo "$0: ignoring $f" ;;
3738
esac
3839
echo
@@ -50,6 +51,7 @@ function cleanup_files(){
5051
if [[ $proxy_count != 0 ]];then
5152
for X in ${PARAM}_*.${EXT}; do
5253
if [ "$X" != "${PARAM}_${HOSTNAME}.${EXT}" ]; then
54+
echo "Removing $X"
5355
rm -rf "$X"
5456
fi
5557
done

0 commit comments

Comments
 (0)