From 01c6852f9648699729d9cd348c30d280e965399f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 11:23:48 +1000 Subject: [PATCH 1/9] Add make target to install mysql requirements --- Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index dda44c9a03..1a59135924 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,11 @@ superuser: install: requirements secret migrate superuser +mysql: + apt-get install mysql-server + apt-get install libmysqlclient-dev + pip3 install mysqlclient + style: flake8 InvenTree @@ -47,4 +52,4 @@ backup: python3 InvenTree/manage.py dbbackup python3 InvenTree/manage.py mediabackup -.PHONY: clean migrate requirements secret superuser install style test coverage documentation backup \ No newline at end of file +.PHONY: clean migrate requirements secret superuser install mysql style test coverage documentation backup \ No newline at end of file From 0fa5cfdd668d2ede7b2af7ef2f77aaa0e9e5b71a Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 11:32:18 +1000 Subject: [PATCH 2/9] Add doc for mysql installation --- docs/config.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/config.rst b/docs/config.rst index fc768b8e87..a506ae4317 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -30,7 +30,17 @@ Database options are specified under the *database* heading in the configuration By default, InvenTree uses an sqlite database file : ``inventree_db.sqlite3``. This provides a simple, portable database file that is easy to use for debug and testing purposes. -**MYSQL:** MySQL database backend is supported with the native Django implemetation. +**MYSQL:** MySQL database backend is supported with the native Django implemetation. To run InvenTree with the MySQL backend, a number of extra packages need to be installed: + +* mysql-server - *MySQL backend server* +* libmysqlclient-dev - *Required for connecting to the MySQL database in Python* +* (pip) mysqlclient - *Python package for communication with MySQL database* + +These requirements can be installed from the base directory with the command ``make mysql``. + +It is up to the database adminstrator to create a new database to store inventree data, in addition to a username/password to access the data. + +The database options then need to be adjusted to communicate the MySQL backend. Refer to the `Django docs `_ for further information. **POSTGRESQL:** PostgreSQL database backend is supported with the native Django implementation. Note that to use this backend, the ``psycopg2`` Python library must first be installed. From b7391231aa079968b8eddab7f66ec078b7d28c51 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 11:41:20 +1000 Subject: [PATCH 3/9] Fixes for default config file --- InvenTree/config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/config.yaml b/InvenTree/config.yaml index 510951842a..ac0bd82d4f 100644 --- a/InvenTree/config.yaml +++ b/InvenTree/config.yaml @@ -36,7 +36,7 @@ cors: # MEDIA_ROOT is the local filesystem location for storing uploaded files # By default, it is stored in a directory named 'media' local to the InvenTree directory # This should be changed for a production installation -media_root: './media' +media_root: 'media' # STATIC_ROOT is the local filesystem location for storing static files # By default it is stored in a directory named 'static' local to the InvenTree directory @@ -47,4 +47,4 @@ log_queries: False # Backup options # Set the backup_dir parameter to store backup files in a specific location -# backup_dir = "/home/me/inventree-backup/" \ No newline at end of file +backup_dir: '/home/me/inventree-backup/' \ No newline at end of file From 905c02d86f3e84c43a90fa4152a5d549651a4a5f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 12:05:47 +1000 Subject: [PATCH 4/9] Copy config template to config file if it does not exist --- .../{config.yaml => config_template.yaml} | 17 +++-- InvenTree/keygen.py | 56 --------------- InvenTree/setup.py | 68 +++++++++++++++++++ Makefile | 8 +-- docs/config.rst | 2 +- 5 files changed, 84 insertions(+), 67 deletions(-) rename InvenTree/{config.yaml => config_template.yaml} (85%) delete mode 100644 InvenTree/keygen.py create mode 100644 InvenTree/setup.py diff --git a/InvenTree/config.yaml b/InvenTree/config_template.yaml similarity index 85% rename from InvenTree/config.yaml rename to InvenTree/config_template.yaml index ac0bd82d4f..b08efdb8be 100644 --- a/InvenTree/config.yaml +++ b/InvenTree/config_template.yaml @@ -3,15 +3,20 @@ # Ref: https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-DATABASES # Specify database parameters below as they appear in the Django docs database: + # Example configuration - sqlite (default) ENGINE: django.db.backends.sqlite3 - NAME: inventree_db.sqlite3 + NAME: inventree_db.sqlite3 # For more complex database installations, further parameters are required # Refer to the django documentation for full list of options - # USER: db_username - # PASSWORD: db_password - # HOST: db_hostname - # PORT: db_port + + # Example Configuration - MySQL + #ENGINE: django.db.backends.mysql + #NAME: inventree + #USER: inventree + #PASSWORD: password + #HOST: '' + #PORT: '' # Set debug to False to run in production mode debug: True @@ -47,4 +52,4 @@ log_queries: False # Backup options # Set the backup_dir parameter to store backup files in a specific location -backup_dir: '/home/me/inventree-backup/' \ No newline at end of file +backup_dir: '/mnt/c/Users/Oliver/inventree-backup/' \ No newline at end of file diff --git a/InvenTree/keygen.py b/InvenTree/keygen.py deleted file mode 100644 index c96df22794..0000000000 --- a/InvenTree/keygen.py +++ /dev/null @@ -1,56 +0,0 @@ -""" -Generates a Django SECRET_KEY file to be used by manage.py -""" - -import random -import string -import os -import sys -import argparse - -KEY_FN = 'secret_key.txt' -KEY_DIR = os.path.dirname(os.path.realpath(__file__)) - - -def generate_key(length=50): - """ Generate a random string - - Args: - length: Number of characters in returned string (default = 50) - - Returns: - Randomized secret key string - """ - - options = string.digits + string.ascii_letters + string.punctuation - key = ''.join([random.choice(options) for i in range(length)]) - return key - - -if __name__ == '__main__': - - parser = argparse.ArgumentParser(description='Generate Django SECRET_KEY file') - parser.add_argument('--output', help='Specify key file path', default=None) - parser.add_argument('--force', '-f', help='Override key file (if it exists)', action='store_true') - parser.add_argument('--dummy', '-d', help='Dummy run (display key only', action='store_true') - - args = parser.parse_args() - - if args.output: - key_filename = args.output - else: - key_filename = os.path.join(KEY_DIR, KEY_FN) - - key_data = generate_key() - - if args.dummy: - print('SECRET_KEY: {k}'.format(k=key_data)) - sys.exit(0) - - if not args.force and os.path.exists(key_filename): - print("Key file already exists - '{f}'".format(f=key_filename)) - sys.exit(0) - - with open(key_filename, 'w') as key_file: - print("Generating SECRET_KEY file - '{f}'".format(f=key_filename)) - key_file.write(key_data) diff --git a/InvenTree/setup.py b/InvenTree/setup.py new file mode 100644 index 0000000000..b74cfe7358 --- /dev/null +++ b/InvenTree/setup.py @@ -0,0 +1,68 @@ +""" +Performs initial setup functions. + +- Generates a Django SECRET_KEY file to be used by manage.py +- Copies config template file (if a config file does not already exist) +""" + +import random +import string +import os +import sys +import argparse +from shutil import copyfile + +OUTPUT_DIR = os.path.dirname(os.path.realpath(__file__)) + +KEY_FN = 'secret_key.txt' +CONFIG_FN = 'config.yaml' +CONFIG_TEMPLATE_FN = 'config_template.yaml' + + +def generate_key(length=50): + """ Generate a random string + + Args: + length: Number of characters in returned string (default = 50) + + Returns: + Randomized secret key string + """ + + options = string.digits + string.ascii_letters + string.punctuation + key = ''.join([random.choice(options) for i in range(length)]) + return key + + +if __name__ == '__main__': + + parser = argparse.ArgumentParser(description='Generate Django SECRET_KEY file') + parser.add_argument('--force', '-f', help='Override existing files', action='store_true') + parser.add_argument('--dummy', '-d', help='Dummy run (do not create any files)', action='store_true') + + args = parser.parse_args() + + # Places to store files + key_filename = os.path.join(OUTPUT_DIR, KEY_FN) + conf_template = os.path.join(OUTPUT_DIR, CONFIG_TEMPLATE_FN) + conf_filename = os.path.join(OUTPUT_DIR, CONFIG_FN) + + # Generate secret key data + key_data = generate_key() + + if args.dummy: + print('SECRET_KEY: {k}'.format(k=key_data)) + sys.exit(0) + + if not args.force and os.path.exists(key_filename): + print("Key file already exists - '{f}'".format(f=key_filename)) + else: + with open(key_filename, 'w') as key_file: + print("Generating SECRET_KEY file - '{f}'".format(f=key_filename)) + key_file.write(key_data) + + if not args.force and os.path.exists(conf_filename): + print("Config file already exists (skipping)") + else: + print("Copying config template to 'config.yaml'") + copyfile(conf_template, conf_filename) diff --git a/Makefile b/Makefile index 1a59135924..900a67c7b2 100644 --- a/Makefile +++ b/Makefile @@ -19,13 +19,13 @@ migrate: requirements: pip3 install -U -r requirements.txt -secret: - python3 InvenTree/keygen.py +setup: + python3 InvenTree/setup.py superuser: python3 InvenTree/manage.py createsuperuser -install: requirements secret migrate superuser +install: requirements setup migrate superuser mysql: apt-get install mysql-server @@ -52,4 +52,4 @@ backup: python3 InvenTree/manage.py dbbackup python3 InvenTree/manage.py mediabackup -.PHONY: clean migrate requirements secret superuser install mysql style test coverage documentation backup \ No newline at end of file +.PHONY: clean migrate requirements setup superuser install mysql style test coverage documentation backup \ No newline at end of file diff --git a/docs/config.rst b/docs/config.rst index a506ae4317..e737caa46c 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -15,7 +15,7 @@ To support install specific settings, a simple configuration file ``config.yaml` The default configuration file launches a *DEBUG* configuration with a simple SQLITE database backend. This default configuration file is shown below: -.. literalinclude :: ../InvenTree/config.yaml +.. literalinclude :: ../InvenTree/config_template.yaml :language: yaml :linenos: From 7b4c8cdc4f469ab8453d3bbfbdcc7349afff37d5 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 12:06:51 +1000 Subject: [PATCH 5/9] Ignore local config file --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 8cf83e1908..3866c30c95 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,9 @@ docs/_build InvenTree/media InvenTree/static +# Local config file +config.yaml + # Key file secret_key.txt From ea669e756248399b4ae8a6b169c24ad84a94ea02 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 12:13:31 +1000 Subject: [PATCH 6/9] Print warnings to stderr not stdout --- InvenTree/InvenTree/settings.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index cd55fc1b25..5b6b308522 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -11,11 +11,16 @@ database setup in this file. """ +import sys import os import logging import tempfile import yaml +def eprint(*args, **kwargs): + """ Print a warning message to stderr """ + print(*args, file=sys.stderr, **kwargs) + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -23,7 +28,7 @@ cfg_filename = os.path.join(BASE_DIR, 'config.yaml') if not os.path.exists(cfg_filename): CONFIG = {} - print("Warning: config.yaml not found - using default settings") + eprint("Warning: config.yaml not found - using default settings") else: with open(cfg_filename, 'r') as cfg: CONFIG = yaml.safe_load(cfg) @@ -52,7 +57,7 @@ if cors_opt: CORS_ORIGIN_ALLOW_ALL = cors_opt.get('allow_all', False) if CORS_ORIGIN_ALLOW_ALL: - print("Warning: CORS requests are allowed for any domain!") + eprint("Warning: CORS requests are allowed for any domain!") else: CORS_ORIGIN_WHITELIST = cors_opt.get('whitelist', []) @@ -156,7 +161,7 @@ DATABASES = {} if 'database' in CONFIG: DATABASES['default'] = CONFIG['database'] else: - print("Warning: Database backend not specified - using default (sqlite)") + eprint("Warning: Database backend not specified - using default (sqlite)") DATABASES['default'] = { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'inventree_db.sqlite3'), From 475e1681de9d1d42c971d74f56bacf51a77dfb41 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 12:54:38 +1000 Subject: [PATCH 7/9] Doc fix --- docs/start.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/start.rst b/docs/start.rst index 9fe52868aa..6150c22bb0 100644 --- a/docs/start.rst +++ b/docs/start.rst @@ -52,7 +52,7 @@ Development and Testing Other shorthand functions are provided for the development and testing process: * ``make requirements`` - Install all required underlying packages using PIP -* ``make secret`` - Generate the SECRET_KEY file for session validation +* ``make setup`` - Perform one-time setup functions * ``make superuser`` - Create a superuser account * ``make backup`` - Backup database tables and media files * ``make test`` - Run all unit tests From a7bf031e4a4397bbc95e501799b1e98bb23e6a9e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 13:09:09 +1000 Subject: [PATCH 8/9] Fix travis script --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 58bdad303f..5df4432cef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ addons: before_install: - make requirements - - make secret + - make setup - make migrate script: From 0a97bc1b40c9949fa6881b6485ee1b9dbcea569e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 14 Aug 2019 13:15:36 +1000 Subject: [PATCH 9/9] PEP fixes --- InvenTree/InvenTree/settings.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 5b6b308522..66f22be9e4 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -17,10 +17,12 @@ import logging import tempfile import yaml + def eprint(*args, **kwargs): """ Print a warning message to stderr """ print(*args, file=sys.stderr, **kwargs) + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))