Netbox: Docker: superuser already exists after upgrade to v2.0-beta2

Created on 7 Apr 2017  路  7Comments  路  Source: netbox-community/netbox

Issue type: Bugreport

Python version: 2.7 (Docker)
NetBox version: digitalocean/netbox:v2.0-beta2 (Docker)

We set up a netbox instance through docker with netbox:v2.0-beta1, started using it, then upgraded tonetbox:v2.0-beta2` two days ago, and are now stuck with the following traceback.

The superuser with that name was already created in beta1 and cannot be recreated, which breaks the upgrade as netbox will not start anymore.

root@netbox:/home/hexa# docker logs netbox -f
Operations to perform:
  Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, tenancy, users
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "/opt/netbox/netbox/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 101, in handle
    exec(sys.stdin.read())
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 170, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 153, in _create_user
    user.save(using=self._db)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 922, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 961, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1060, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_user_username_key"
DETAIL:  Key (username)=(ffdaadmin) already exists.
bug

All 7 comments

FYI the Docker build components are being removed from the primary NetBox code base in v2.0 (see #1008). Leaving this open for now in case anyone has a quick fix for you.

Hey @mweinelt here's the hacky way I went around it. You can change the entrypoint.sh to this.

diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh
index 53e52ef..e9fc039 100755
--- a/docker/docker-entrypoint.sh
+++ b/docker/docker-entrypoint.sh
@@ -13,7 +13,12 @@ if [[ -z ${SUPERUSER_NAME} || -z ${SUPERUSER_EMAIL} || -z ${SUPERUSER_PASSWORD}
         SUPERUSER_PASSWORD='admin'
         echo "Using defaults: Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}, Password: ${SUPERUSER_PASSWORD}"
 fi
-echo "from django.contrib.auth.models import User; User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}')" | python /opt/netbox/netbox/manage.py shell
+
+python netbox/manage.py shell --plain << END
+from django.contrib.auth.models import User
+if not User.objects.filter(username='${SUPERUSER_NAME}'):
+    User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}')
+END

 # copy static files
 /opt/netbox/netbox/manage.py collectstatic --no-input

and it should work. Not sure the value of correcting it in a PR seeing as it's going to be removed soon anyways, but happy to make one if it makes sense.

That works, the path to manage.py however needs to be absolute! :+1:

This is what I ended up throwing together to make it work: https://github.com/freifunk-darmstadt/docker-netbox

It uses python3.6 and the latest gunicorn version that is on pip.

@mweinelt Awesome! I'd like to have some other people review your repo and if everyone's in agreement we can make it the official unofficial Docker package for NetBox.

I'm new to both docker and netbox, so I would rather not have this repository be anything close to official. I'd be happy to submit the changes somewhere, if they make sense, though.

Thanks @zachmoody for the tip above! Even though this will all be going away I think we should keep the current Docker implementation usable.

I encountered this moving from v1.9.5 to v2.0-beta2...

Was this page helpful?
0 / 5 - 0 ratings