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

Using S3 django storages backend requires USE_TZ=False #363

Closed
nniehoff opened this issue Apr 22, 2021 · 5 comments · Fixed by #673
Closed

Using S3 django storages backend requires USE_TZ=False #363

nniehoff opened this issue Apr 22, 2021 · 5 comments · Fixed by #673
Labels
type: bug Something isn't working as expected

Comments

@nniehoff
Copy link
Contributor

Environment

  • Python version: 3.6
  • Nautobot version: 1.0b4

When configuring django-storages to use s3:

STORAGE_BACKEND = "storages.backends.s3boto3.S3Boto3Storage"
STORAGE_CONFIG = {
    "AWS_STORAGE_BUCKET_NAME": os.environ.get("AWS_STORAGE_BUCKET_NAME", ""),
    "AWS_S3_REGION_NAME": os.environ.get("AWS_S3_REGION_NAME", ""),
    "AWS_DEFAULT_ACL": os.environ.get("AWS_DEFAULT_ACL", "public-read"),
    "AWS_QUERYSTRING_AUTH": is_truthy(os.environ.get("AWS_QUERYSTRING_AUTH", False)),
    "AWS_LOCATION": os.environ.get("AWS_LOCATION", ""),
}
STATICFILES_STORAGE = STORAGE_BACKEND

nautobot-server collectstatic fails with the following stack trace:

Traceback (most recent call last):
  File "/usr/local/bin/nautobot-server", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/nautobot/core/cli.py", line 62, in main
    initializer=_configure_settings,  # Called after defaults
  File "/usr/local/lib/python3.6/site-packages/nautobot/core/runner/runner.py", line 268, in run_app
    management.execute_from_command_line([runner_name, command] + command_args)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 194, in handle
    collected = self.collect()
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 118, in collect
    handler(path, prefixed_path, storage)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 345, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 286, in delete_file
    source_last_modified.replace(microsecond=0)
TypeError: can't compare offset-naive and offset-aware datetimes

Setting USE_TZ=False resolves the issue but comes with its own set of issues. It looks like the files are correctly getting published to S3. I am unsure if this happens on the first collectstatic or only subsequent collectstatic runs.

Steps to Reproduce

  1. See above

Expected Behavior

Collect Static publishes the files to S3 and exits successfully.

Observed Behavior

Files are published but the command fails.

@jathanism
Copy link
Contributor

Setting USE_TZ=False resolves the issue but comes with its own set of issues. It looks like the files are correctly getting published to S3. I am unsure if this happens on the first collectstatic or only subsequent collectstatic runs.

Yeah, so what I'm wondering is if you push the files to S3 for the first time with USE_TZ=True, will this work as expected? Let's see if we can confirm that, before we try to come up with a solution otherwise.

I'd like to rule that out, since this isn't isn't unique to Nautobot since the functionality is coming from django-storages. I would like to identify the root cause here and at least provide some troubleshooting docs.

@nniehoff
Copy link
Contributor Author

agreed, I'll work on getting a slightly less complex setup to reproduce this and maybe we can dig deeper there

@nniehoff
Copy link
Contributor Author

I have a clean install on an AWS instance and was able to reproduce the issue, so none of the other overhead I was dealing with before. In this case I am testing 1.0.0b4 with Python 3.8.5 on Ubuntu 20.04. I can confirm if USE_TZ=True the first collectstatic runs fine, subsequent collectstatic operations fail with the above error.

@jathanism jathanism added type: bug Something isn't working as expected status: current labels Apr 28, 2021
@jathanism jathanism added this to the v1.1.0 milestone Apr 28, 2021
@jathanism jathanism added this to To do in Release v1.1.0 via automation Apr 28, 2021
@jedelman8 jedelman8 removed this from the v1.1.0 milestone May 28, 2021
@jedelman8 jedelman8 removed this from To do in Release v1.1.0 May 28, 2021
@nniehoff
Copy link
Contributor Author

@FragmentedPacket found the solution! For AWS we need to add USE_TZ: True to the STORAGES_CONFIG we will work on getting this documented and/or an example implemented.

@FragmentedPacket
Copy link
Contributor

I can fix the code as well. Waiting on Jathan to give thumbs on code fix, but glad we found the issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants