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

Load custom fields when creating the container #39

Merged
merged 1 commit into from Jan 25, 2018

Conversation

born4new
Copy link
Contributor

@born4new born4new commented Jan 24, 2018

What's new?

This is a general mechanism to launch python scripts for Netbox: All the scripts put inside the docker/startup_scripts folder will be executed.

for script in startup_scripts/*.py; do
./manage.py shell --plain < $script
done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This opens a lot of possibilities for many customizations.

Dockerfile Outdated
@@ -29,6 +29,7 @@ WORKDIR /opt/netbox
RUN pip install -r requirements.txt

COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py
COPY docker/startup_scripts /opt/netbox/netbox/startup_scripts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two ways to use this feature for our users, actually. But in order to be generally useful, having this in the Dockerfile seems to be the wrong way..

User can either base of the original image and build their own Docker container (see example Dockerfile below), or they can just mount such files into the container using docker-compose (or whatever mechanism; see the example further down).

So, something like the following should probably be added to the README, along with a short explanation of the feature.

FROM ninech/netbox
COPY docker/startup_scripts /opt/netbox/netbox/startup_scripts

And I think it would be nice to adjust the default docker-compose.yml file to include the following:

services:
    netbox:
      volumes:
      - ./startup_scripts:/opt/netbox/netbox/startup_scripts

This would also require a .gitkeep file in the folder ./startup_scripts/.

)

nine_urn.obj_type.add(device)
nine_urn.obj_type.add(device_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems to contain internal code, but it would suit as a good example as part of the README.md.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -39,6 +39,10 @@ if not User.objects.filter(username='${SUPERUSER_NAME}'):
Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}')
END

for script in startup_scripts/*.py; do
./manage.py shell --plain < $script
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The files are launched every time the container starts, right?

Copy link
Collaborator

@cimnine cimnine Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should put quotation marks around $script, in case a file is saved with a space in the name:

"${script}"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@born4new born4new force-pushed the seed_custom_fields branch 2 times, most recently from ed3c341 to 759a2d5 Compare January 25, 2018 08:48
README.md Outdated
description='My own custom field'
)

my_custom_field.obj_type.add(device)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What impact does it have if the same obj_type is added over and over again with every start?

Copy link
Contributor Author

@born4new born4new Jan 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to my out-of-this-world python skills and CustomField.objects.get_or_create, not much: it will first try to fetch it, and if not present, will create it.

Copy link
Collaborator

@cimnine cimnine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you a lot for this contribution!

@born4new born4new merged commit 1a137a0 into master Jan 25, 2018
@born4new born4new deleted the seed_custom_fields branch January 25, 2018 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants