awesto / cookiecutter-django-shop

Cookiecutter django-SHOP is a blueprint for an e-commerce site based on django-CMS.
126 stars 58 forks source link

Docker Cookiecutter first migration fails #44

Closed ay0000 closed 3 years ago

ay0000 commented 3 years ago

What happened?

django-shop docker container fails to start on fresh install. It seems to try to write a migration to a folder than it doesn't actually have access to. I tried to change the user in the docker file from django to root but that just creates different problems.

See below

webapp_1         | Operations to perform:
webapp_1         |   Apply all migrations: admin, auth, authtoken, cms, cmsplugin_cascade, contenttypes, djangocms_text_ckeditor, easy_thumbnails, email_auth, filer, menus, post_office, sessions, shop, shop_sendcloud, sites
webapp_1         | Running migrations:
webapp_1         |   No migrations to apply.
webapp_1         |   Your models have changes that are not yet reflected in a migration, and so won't be applied.
webapp_1         |   Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
webapp_1         | Initializing project paintbypattern
webapp_1         | Migrations for 'cms':
webapp_1         |   /usr/local/lib/python3.6/site-packages/cms/migrations/0023_auto_20210219_0125.py
webapp_1         |     - Alter field depth on cmsplugin
webapp_1         |     - Alter field depth on treenode
webapp_1         | Traceback (most recent call last):
webapp_1         |   File "/web/manage.py", line 10, in <module>
webapp_1         |     execute_from_command_line(sys.argv)
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
webapp_1         |     utility.execute()
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
webapp_1         |     self.fetch_command(subcommand).run_from_argv(self.argv)
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
webapp_1         |     self.execute(*args, **cmd_options)
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
webapp_1         |     output = self.handle(*args, **options)
webapp_1         |   File "/web/paintbypattern/management/commands/initialize_shop_demo.py", line 33, in handle
webapp_1         |     call_command('makemigrations', 'paintbypattern')
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 168, in call_command
webapp_1         |     return command.execute(*args, **defaults)
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
webapp_1         |     output = self.handle(*args, **options)
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
webapp_1         |     res = handle_func(*args, **kwargs)
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 182, in handle
webapp_1         |     self.write_migration_files(changes)
webapp_1         |   File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 220, in write_migration_files
webapp_1         |     with open(writer.path, "w", encoding='utf-8') as fh:
webapp_1         | PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/site-packages/cms/migrations/0023_auto_20210219_0125.py'

What should've happened instead?

Docker container should have started.

Steps to reproduce

sudo apt update && sudo apt upgrade -y

sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

curl https://pyenv.run | bash
**ADD TO PATH**
exec $SHELL

pyenv update
pyenv install -v 3.7.9
**ADD TO PATH**
pyenv global 3.7.9

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

   sudo apt-get update

   sudo apt-get install docker-ce docker-ce-cli containerd.io

   sudo groupadd docker
   sudo usermod -aG docker $USER
   newgrp docker

   sudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
   sudo chmod +x /usr/local/bin/docker-compose

pip install pipenv cookiecutter autopep8

cookiecutter https://github.com/awesto/cookiecutter-django-shop
...
docker-compose up --build

Docker-compose version 1.28.2, build 67630359 Docker version 20.10.3, build 48d30b5 Python 3.7.9 Ubuntu 20.04 running on AWS

ay0000 commented 3 years ago

So I changed the dockerfile to set the user django as the owner of the /usr/local/lib/python3.6/site-packages/cms/migrations/ folder. This takes care of the permission denied problem but I'm now getting a duplicate key error.

Even after running manage.py flush and trying to initialize the demo manually, please see the output below

django@5c3aad691978:~$ ./manage.py initialize_shop_demo
Operations to perform:
  Apply all migrations: admin, auth, authtoken, cms, cmsplugin_cascade, contenttypes, djangocms_text_ckeditor, easy_thumbnails, email_auth, filer, menus, paintbypattern, post_office, sessions, shop, shop_sendcloud, sites
Running migrations:
  No migrations to apply.
Initializing project paintbypattern
No changes detected in app 'paintbypattern'
Operations to perform:
  Apply all migrations: admin, auth, authtoken, cms, cmsplugin_cascade, contenttypes, djangocms_text_ckeditor, easy_thumbnails, email_auth, filer, menus, paintbypattern, post_office, sessions, shop, shop_sendcloud, sites
Running migrations:
  No migrations to apply.
Installed 33 object(s) from 1 fixture(s)
The following CMS pages must be adjusted:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.IntegrityError: duplicate key value violates unique constraint "cms_cmsplugin_path_4917bb44_uniq"
DETAIL:  Key (path)=() already exists.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  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 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/web/paintbypattern/management/commands/initialize_shop_demo.py", line 37, in handle
    call_command('shop', 'check-pages', add_recommended=True)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 168, in call_command
    return command.execute(*args, **defaults)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/shop/management/commands/shop.py", line 83, in handle
    for k, msg in enumerate(self.create_recommended_pages(), 1):
  File "/usr/local/lib/python3.6/site-packages/shop/management/commands/shop.py", line 148, in create_recommended_pages
    self.publish_in_all_languages(page)
  File "/usr/local/lib/python3.6/site-packages/shop/management/commands/shop.py", line 455, in publish_in_all_languages
    copy_plugins_to_language(page, languages[0], language)
  File "/usr/local/lib/python3.6/site-packages/cms/api.py", line 509, in copy_plugins_to_language
    copied_plugins = copy_plugins.copy_plugins_to(plugins, placeholder, target_language)
  File "/usr/local/lib/python3.6/site-packages/cms/utils/copy_plugins.py", line 20, in copy_plugins_to
    old_parent_cache, no_signals))
  File "/usr/local/lib/python3.6/site-packages/cms/models/pluginmodel.py", line 333, in copy_plugin
    new_plugin.save()
  File "/usr/local/lib/python3.6/site-packages/cms/models/pluginmodel.py", line 267, in save
    super().save(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 749, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 787, in save_base
    force_update, using, update_fields,
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 890, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 929, in _do_insert
    using=using, raw=raw,
  File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1204, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1394, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "cms_cmsplugin_path_4917bb44_uniq"
DETAIL:  Key (path)=() already exists.
ay0000 commented 3 years ago

Found that this was related to django-treebeard. v.4.5 breaks things apparently.

Added django-treebeard==4.4 to requirements.txt and that fixed it

see here: https://github.com/django-cms/django-cms/issues/6980