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

Changed bucket check to correct logic and and fixed printout #1176

Merged
merged 2 commits into from
May 17, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ Please add a _short_ line describing the PR you make, if the PR implements a spe
- Patch: Fix the warning in web for too soon TOTP login (within 90 seconds) ([#1173](https://github.com/ScilifelabDataCentre/dds_web/pull/1173))
- Bug: Do not remove the bucket when emptying the project ([#1172](https://github.com/ScilifelabDataCentre/dds_web/pull/1172))
- New `add-missing-buckets` argument option to the `lost-files` flask command ([#1174](https://github.com/ScilifelabDataCentre/dds_web/pull/1174))
- Bug: Corrected `lost-files` logic and message ([#1176](https://github.com/ScilifelabDataCentre/dds_web/pull/1176))
27 changes: 21 additions & 6 deletions dds_web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,32 @@ def lost_files_s3_db(action_type: str):
import boto3
from dds_web.utils import bucket_is_valid

# Interate through the units
for unit in models.Unit.query:
session = boto3.session.Session()

# Connect to S3
resource = session.resource(
service_name="s3",
endpoint_url=unit.safespring_endpoint,
aws_access_key_id=unit.safespring_access,
aws_secret_access_key=unit.safespring_secret,
)

db_count = 0
s3_count = 0
# Variables
db_count = 0 # Files not found in s3
s3_count = 0 # Files not found in db

# Iterate through unit projects
for project in unit.projects:
# Check for objects in bucket
try:
s3_filenames = set(
entry.key for entry in resource.Bucket(project.bucket).objects.all()
)
except resource.meta.client.exceptions.NoSuchBucket:
flask.current_app.logger.warning("Missing bucket %s", project.bucket)
# Create a missing bucket if argument chosen
if action_type == "add-missing-buckets":
valid, message = bucket_is_valid(bucket_name=project.bucket)
if not valid:
Expand All @@ -526,13 +533,18 @@ def lost_files_s3_db(action_type: str):
flask.current_app.logger.info(f"Bucket '{project.bucket}' created.")
continue

# Get objects in project
try:
db_filenames = set(entry.name_in_bucket for entry in project.files)
except sqlalchemy.exc.OperationalError:
flask.current_app.logger.critical("Unable to connect to db")

diff_db = db_filenames.difference(s3_filenames)
diff_s3 = s3_filenames.difference(db_filenames)
# Differences
diff_db = db_filenames.difference(s3_filenames) # In db but not in S3
diff_s3 = s3_filenames.difference(db_filenames) # In S3 but not in db

# List all files which are missing in either db of s3
# or delete the files from the s3 if missing in db, or db if missing in s3
if action_type == "list":
for file_entry in diff_db:
flask.current_app.logger.info(
Expand Down Expand Up @@ -578,16 +590,19 @@ def lost_files_s3_db(action_type: str):
s3_count += len(diff_s3)
db_count += len(diff_db)

# Print out information about actions performed in cronjob
if s3_count or db_count:
action_word = "Found" if action_type in ("find", "list") else "Deleted"
action_word = (
"Found" if action_type in ("find", "list", "add-missing-buckets") else "Deleted"
)
flask.current_app.logger.info(
"%s %d entries for lost files (%d in db, %d in s3)",
action_word,
s3_count + db_count,
db_count,
s3_count,
)
if action_type in ("find", "list"):
if action_type in ("find", "list", "add-missing-buckets"):
sys.exit(1)

else:
Expand Down
2 changes: 1 addition & 1 deletion dds_web/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def bucket_is_valid(bucket_name):
message = f"The bucket name has the incorrect length {len(bucket_name)}"
elif re.findall(r"[^a-zA-Z0-9.-]", bucket_name):
message = "The bucket name contains invalid characters."
elif bucket_name[0].isalnum():
elif not bucket_name[0].isalnum():
message = "The bucket name must begin with a letter or number."
elif bucket_name.count(".") > 2:
message = "The bucket name cannot contain more than two dots."
Expand Down