Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[skip-release] improve bucket creation #71

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
interval: "weekly"
2 changes: 1 addition & 1 deletion docker-compose-s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
entrypoint: /bin/bash
command: -c 'minio server /data --console-address ":9001"'
volumes:
- minio_data:/mapproxy
- minio_data:/data
ports:
- "9000:9000"
- "9001:9001"
Expand Down
12 changes: 6 additions & 6 deletions mapproxy_configuration/mapproxy-s3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ services:
contact:
person: Kartoza
position: GIS Manager
organization:
address:
city:
postcode:
organization: Kartoza
address: Kartoza
city: Cape Town
postcode: 7700
country: south Africa
phone:
email:
phone: '276426345'
email: [email protected]
access_constraints: Insert license and copyright information for this service.
fees: "None"
tms:
Expand Down
12 changes: 6 additions & 6 deletions mapproxy_configuration/mapproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ services:
contact:
person: Kartoza
position: GIS Manager
organization:
address:
city:
postcode:
organization: Kartoza
address: Kartoza
city: Cape Town
postcode: 7700
country: south Africa
phone:
email:
phone: '276426345'
email: [email protected]
access_constraints:
Insert license and copyright information for this service.
fees: 'None'
Expand Down
2 changes: 1 addition & 1 deletion scenario_tests/s3/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ services:
entrypoint: /bin/bash
command: -c 'minio server /data --console-address ":9001"'
volumes:
- minio_data:/mapproxy
- minio_data:/data
ports:
- "9000:9000"
- "9001:9001"
Expand Down
13 changes: 6 additions & 7 deletions scenario_tests/s3/mapproxy_configuration/mapproxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ services:
contact:
person: Kartoza
position: GIS Manager
organization:
address:
city:
postcode:
organization: Kartoza
address: Kartoza
city: Cape Town
postcode: 7700
country: south Africa
phone:
email:
phone: '276426345'
email: [email protected]
access_constraints: Insert license and copyright information for this service.
fees: "None"
tms:
Expand All @@ -70,7 +70,6 @@ caches:
grids: [osm_grid]
meta_size: [5, 5]
meta_buffer: 20
concurrent_tile_creators: 2
cache:
type: s3
directory: /hillshade/
Expand Down
106 changes: 82 additions & 24 deletions scripts/create_default_buckets.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,100 @@
from os import environ as env
import os
import re
import boto3, botocore
import logging
from typing import List
import boto3
from botocore.exceptions import ClientError

def check_bucket(s3, bucket_name):
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def get_environment_variable(name: str, required: bool = True) -> str:
"""
Retrieves an environment variable or raises an error if it's missing.
"""
value = os.getenv(name)
if required and not value:
raise ValueError(f"Missing required environment variable: {name}")
return value


def parse_bucket_list(bucket_list: str) -> List[str]:
"""
Parses a string of bucket names separated by commas, spaces, or semicolons into a list.
"""
return re.split(r'[ ,;]+', bucket_list.strip())


def check_bucket(s3_client, bucket_name: str) -> bool:
"""
Checks if a bucket exists and is accessible.

Returns True if the bucket exists or is private, False otherwise.
"""
try:
s3.head_bucket(Bucket=bucket_name)
print(f"{bucket_name} available")
s3_client.head_bucket(Bucket=bucket_name)
logger.info(f"Bucket '{bucket_name}' is available.")
return True
except botocore.exceptions.ClientError as e:
except ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 403:
print(f"{bucket_name} is Private. Access denied.")
logger.warning(f"Bucket '{bucket_name}' is private. Access denied.")
return True
elif error_code == 404:
print(f"{bucket_name} does not exist")
logger.info(f"Bucket '{bucket_name}' does not exist.")
return False
else:
logger.error(f"Error checking bucket '{bucket_name}': {e}")
raise


def create_bucket(s3_client, bucket_name: str):
"""
Creates a bucket if it does not exist.
"""
try:
logger.info(f"Creating bucket '{bucket_name}'.")
s3_client.create_bucket(Bucket=bucket_name)
logger.info(f"Bucket '{bucket_name}' created successfully.")
except ClientError as e:
logger.error(f"Failed to create bucket '{bucket_name}': {e}")
raise


def main():
buckets = env['S3_BUCKET_LIST']
buckets = re.split(r',| |;', buckets)
end_point = env['S3_BUCKET_ENDPOINT']
"""
Main function to check and create S3 buckets as needed.
"""
bucket_list_str = get_environment_variable('S3_BUCKET_LIST')
buckets = parse_bucket_list(bucket_list_str)
endpoint = get_environment_variable('S3_BUCKET_ENDPOINT')

session = boto3.session.Session()
aws_access_key_id = get_environment_variable('AWS_ACCESS_KEY_ID')
aws_secret_access_key = get_environment_variable('AWS_SECRET_ACCESS_KEY')

session = boto3.session.Session()
s3 = session.client(
service_name='s3',
aws_access_key_id=env['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=env['AWS_SECRET_ACCESS_KEY'],
endpoint_url=end_point,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
endpoint_url=endpoint,
)

for i in buckets:
if not check_bucket(s3, i):
print(f"Creating {i}")
s3.create_bucket(Bucket=i)
for bucket_name in buckets:
if not check_bucket(s3, bucket_name):
create_bucket(s3, bucket_name)


if __name__=="__main__":
create_buckets = env['CREATE_DEFAULT_S3_BUCKETS']
if create_buckets.lower() == 'true':
print("Creating default buckets")
main()
if __name__ == "__main__":
try:
create_buckets = get_environment_variable('CREATE_DEFAULT_S3_BUCKETS', required=False)
if create_buckets and create_buckets.lower() == 'true':
logger.info("Starting bucket creation process.")
main()
else:
logger.info("CREATE_DEFAULT_S3_BUCKETS is not set to 'true'. Skipping bucket creation.")
except Exception as e:
logger.error(f"An error occurred: {e}")
exit(1)
2 changes: 2 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function entry_point_script {
for f in /docker-entrypoint-mapproxy.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" || true;;
*.py) echo "$0: running Python script $f"; python3 "$f" || true;;
*) echo "$0: ignoring $f" ;;
esac
echo
Expand All @@ -50,6 +51,7 @@ function cleanup_files(){
if [[ $proxy_count != 0 ]];then
for X in ${PARAM}_*.${EXT}; do
if [ "$X" != "${PARAM}_${HOSTNAME}.${EXT}" ]; then
echo "Removing $X"
rm -rf "$X"
fi
done
Expand Down