From 1467c014162855cba1967fd02017736bf24a04f3 Mon Sep 17 00:00:00 2001
From: asmundh <aasmund.haugse@gmail.com>
Date: Tue, 2 Mar 2021 14:53:08 +0100
Subject: [PATCH] Add instructions for CI/CD with Heroku

---
 .gitlab-ci.yml                        |  25 ++++++
 README.md                             |  40 +++++++++
 backend/Procfile                      |   1 +
 backend/secfit/secfit/djangoHeroku.py | 117 ++++++++++++++++++++++++++
 backend/secfit/secfit/settings.py     |  21 +++--
 frontend/Procfile                     |   1 +
 package.json                          |  13 +++
 runtime.txt                           |   1 +
 8 files changed, 214 insertions(+), 5 deletions(-)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 backend/Procfile
 create mode 100644 backend/secfit/secfit/djangoHeroku.py
 create mode 100644 frontend/Procfile
 create mode 100644 package.json
 create mode 100644 runtime.txt

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..89bcf76
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,25 @@
+stages:
+    - test
+    - staging
+  
+test:
+  image: python:3.8
+  stage: test
+  script:
+  # this configures Django application to use attached postgres database that is run on `postgres` host
+    - cd backend/secfit
+    - apt-get update -qy
+    - pip install -r requirements.txt
+
+staging:
+  type: deploy
+  image: ruby
+  stage: staging
+  script:
+    - apt-get update -qy
+    - apt-get install -y ruby-dev
+    - gem install dpl
+    - dpl --provider=heroku --app=<Your-herokuproject-name> --api-key=$HEROKU_STAGING_API_KEY
+    - dpl --provider=heroku --app=<Your-herokuproject-name> --api-key=$HEROKU_STAGING_API_KEY
+  only:
+    - master
diff --git a/README.md b/README.md
index 8912dda..bf7729f 100644
--- a/README.md
+++ b/README.md
@@ -126,3 +126,43 @@ If you want to run this as a mobile application
 It's possible you will need to add the platforms you want to run and build.
 The following documentation can be used to run the application in an Android emulator: \
 https://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html
+
+## Continuous integration
+WARNING: Do not perform penetration testing on Heroku applications
+
+Continuous integration will build the code pushed to master and push it to your heroku app so you get a live version of your latest code by just pushing your code to GitLab.
+
+1. Create a heroku account and an app for both the frontend and the backend.
+2. Select buildpacks for the two apps. The backend uses Python while the frontend uses node.js.
+   * Settings > Buildpacks > Add buildpack
+   * Both applications need the buildpack https://github.com/heroku/heroku-buildpack-multi-procfile.git too.
+3. Set the project in the .gitlab-cs.yml file by replacing `<Your-herokuproject-name>` with the name of the Heroku app you created
+`- dpl --provider=heroku --app=<Your-herokuproject-name> --api-key=$HEROKU_STAGING_API_KEY`
+5. Set varibles at GitLab
+   * settings > ci > Environment Variables
+   * `HEROKU_STAGING_API_KEY` = heroku > Account Settings > API Key
+6. Add heroku database for the backend
+   * Resources > Add ons > search for postgres > add "Heroku Postgres"
+7. Set variables for the backend on Heroku. Settings > Config vars > Reveal vars
+   * `DATABASE_URL` = Should be set by default. If not here is where you can find it: Resources > postgress > settings > view credentials > URI
+   * `IS_HEROKU` = `IS_HEROKU`
+   * `PROCFILE` = `backend/secfit/Procfile`
+8. Set variables for the frontend on heroku. Settings > Config vars > Reveal vars. Insert the URL for your backend app.
+   * `BACKEND_HOST` = `https://<SECFIT_BACKEND>.herokuapp.com`
+   * `PROCFILE` = `frontend/Procfile`
+9. Push the repository to both of the heroku applications https://devcenter.heroku.com/articles/git 
+   * git push `<backend-repository>` HEAD:master
+   * git push `<frontend-repository>` HEAD:master
+10. On GitLab go to CI / CD in the repository menu and select `Run Pipeline` if it has not already started. When both stages complete the app should be available on heroku. Staging will fail from timeout as Heroku does not give the propper response to end the job. But the log should state that the app was deployed.
+11. Setup the applications database.
+   * Install heroku CLI by following: https://devcenter.heroku.com/articles/heroku-cli
+   * Log in to the Heroku CLI by entering `heroku login`. This opens a webbrowser and you accept the login request.
+   * Migrate database by entering
+   `heroku run python backend/secfit/manage.py migrate -a <heroku-app-name>`. `Heroku run` will run the folowing command on your heroku instance. Remember to replace `<heroku-app-name>` with your app name
+   * and create an admin account by running
+   `heroku run python backend/secfit/manage.py createsuperuser -a <heroku-app-name>`.
+   * seed database `heroku run python backend/secfit/manage.py loaddata seed.json -a <heroku-app-name>`
+12. On the frontend app, add a config variable for `BACKEND_HOST` = `BACKEND_HOST`
+
+You will also need the heroku multi-procfile buildpack: https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-multi-procfile.
+In general, for this application to work (beyond locally), you will need to set the BACKEND_HOST to the URL of the REST API backend.
diff --git a/backend/Procfile b/backend/Procfile
new file mode 100644
index 0000000..3791efd
--- /dev/null
+++ b/backend/Procfile
@@ -0,0 +1 @@
+web: gunicorn --pythonpath 'backend/secfit' secfit.wsgi --log-file -
\ No newline at end of file
diff --git a/backend/secfit/secfit/djangoHeroku.py b/backend/secfit/secfit/djangoHeroku.py
new file mode 100644
index 0000000..df2f150
--- /dev/null
+++ b/backend/secfit/secfit/djangoHeroku.py
@@ -0,0 +1,117 @@
+#import logging
+import os
+
+import dj_database_url
+from django.test.runner import DiscoverRunner
+from .djangoHeroku import settings
+
+
+MAX_CONN_AGE = 600
+
+def settings(config, *, db_colors=False, databases=True, test_runner=True, staticfiles=True, allowed_hosts=True, logging=True, secret_key=True):
+
+    # Database configuration.
+    # TODO: support other database (e.g. TEAL, AMBER, etc, automatically.)
+    if databases:
+        # Integrity check.
+        if 'DATABASES' not in config:
+            config['DATABASES'] = {'default': None}
+
+        conn_max_age = config.get('CONN_MAX_AGE', MAX_CONN_AGE)
+            
+        if db_colors:
+            # Support all Heroku databases.
+            # TODO: This appears to break TestRunner.
+            for (env, url) in os.environ.items():
+                if env.startswith('HEROKU_POSTGRESQL'):
+                    db_color = env[len('HEROKU_POSTGRESQL_'):].split('_')[0]
+
+                    #logger.info('Adding ${} to DATABASES Django setting ({}).'.format(env, db_color))
+
+                    config['DATABASES'][db_color] = dj_database_url.parse(url, conn_max_age=conn_max_age, ssl_require=True)
+
+        if 'DATABASE_URL' in os.environ:
+            #logger.info('Adding $DATABASE_URL to default DATABASE Django setting.')
+
+            # Configure Django for DATABASE_URL environment variable.
+            config['DATABASES']['default'] = dj_database_url.config(conn_max_age=conn_max_age, ssl_require=True)
+
+            #logger.info('Adding $DATABASE_URL to TEST default DATABASE Django setting.')
+
+            # Enable test database if found in CI environment.
+            if 'CI' in os.environ:
+                config['DATABASES']['default']['TEST'] = config['DATABASES']['default']
+
+        #else:
+            #logger.info('$DATABASE_URL not found, falling back to previous settings!')
+
+    if test_runner:
+        # Enable test runner if found in CI environment.
+        if 'CI' in os.environ:
+            config['TEST_RUNNER'] = 'django_heroku.HerokuDiscoverRunner'
+
+    # Staticfiles configuration.
+    if staticfiles:
+        #logger.info('Applying Heroku Staticfiles configuration to Django settings.')
+
+        config['STATIC_ROOT'] = os.path.join(config['BASE_DIR'], 'staticfiles')
+        config['STATIC_URL'] = '/static/'
+
+        # Ensure STATIC_ROOT exists.
+        os.makedirs(config['STATIC_ROOT'], exist_ok=True)
+
+        # Insert Whitenoise Middleware.
+        try:
+            config['MIDDLEWARE_CLASSES'] = tuple(['whitenoise.middleware.WhiteNoiseMiddleware'] + list(config['MIDDLEWARE_CLASSES']))
+        except KeyError:
+            config['MIDDLEWARE'] = tuple(['whitenoise.middleware.WhiteNoiseMiddleware'] + list(config['MIDDLEWARE']))
+
+        # Enable GZip.
+        config['STATICFILES_STORAGE'] = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
+
+    if allowed_hosts:
+        #logger.info('Applying Heroku ALLOWED_HOSTS configuration to Django settings.')
+        config['ALLOWED_HOSTS'] = ['*']
+    """
+    if logging:
+        logger.info('Applying Heroku logging configuration to Django settings.')
+
+        config['LOGGING'] = {
+            'version': 1,
+            'disable_existing_loggers': False,
+            'formatters': {
+                'verbose': {
+                    'format': ('%(asctime)s [%(process)d] [%(levelname)s] ' +
+                               'pathname=%(pathname)s lineno=%(lineno)s ' +
+                               'funcname=%(funcName)s %(message)s'),
+                    'datefmt': '%Y-%m-%d %H:%M:%S'
+                },
+                'simple': {
+                    'format': '%(levelname)s %(message)s'
+                }
+            },
+            'handlers': {
+                'null': {
+                    'level': 'DEBUG',
+                    'class': 'logging.NullHandler',
+                },
+                'console': {
+                    'level': 'DEBUG',
+                    'class': 'logging.StreamHandler',
+                    'formatter': 'verbose'
+                }
+            },
+            'loggers': {
+                'testlogger': {
+                    'handlers': ['console'],
+                    'level': 'INFO',
+                }
+            }
+        }
+    """
+    # SECRET_KEY configuration.
+    if secret_key:
+        if 'SECRET_KEY' in os.environ:
+            #logger.info('Adding $SECRET_KEY to SECRET_KEY Django setting.')
+            # Set the Django setting from the environment variable.
+            config['SECRET_KEY'] = os.environ['SECRET_KEY']
diff --git a/backend/secfit/secfit/settings.py b/backend/secfit/secfit/settings.py
index 9233653..f4f4f07 100644
--- a/backend/secfit/secfit/settings.py
+++ b/backend/secfit/secfit/settings.py
@@ -43,6 +43,7 @@ ALLOWED_HOSTS = [
     "10." + groupid + ".0.4",
     "molde.idi.ntnu.no",
     "10.0.2.2",
+    # ADD HEROKU URL HERE
 ]
 
 # Application definition
@@ -95,12 +96,22 @@ WSGI_APPLICATION = "secfit.wsgi.application"
 # Database
 # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
 
-DATABASES = {
-    "default": {
-        "ENGINE": "django.db.backends.sqlite3",
-        "NAME": BASE_DIR / "db.sqlite3",
+is_prod = os.environ.get("IS_HEROKU", None)
+
+if is_prod:
+    settings(locals())
+
+if 'DATABASE_URL' in os.environ:
+    import dj_database_url
+    print("\n\n\n\n\nHEI\n\n\n\n\n\n")
+    DATABASES = {'default': dj_database_url.config()}
+else:
+    DATABASES = {
+        "default": {
+            "ENGINE": "django.db.backends.sqlite3",
+            "NAME": BASE_DIR / "db.sqlite3",
+        }
     }
-}
 
 # CORS Policy
 CORS_ORIGIN_ALLOW_ALL = (
diff --git a/frontend/Procfile b/frontend/Procfile
new file mode 100644
index 0000000..71b1c61
--- /dev/null
+++ b/frontend/Procfile
@@ -0,0 +1 @@
+web: cd frontend && cordova run browser --release --port=$PORT
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..3f61d7a
--- /dev/null
+++ b/package.json
@@ -0,0 +1,13 @@
+{
+  "name": "secfit",
+  "description": "Secure Fitness",
+  "version": "0.0.1",
+  "engines": {
+    "node": "12.x"
+  },
+  "dependencies": {
+      "cordova": "10.0.0",
+      "cordova-browser": "6.0.0",
+      "cordova-plugin-whitelist": "^1.3.4"
+  }
+}
diff --git a/runtime.txt b/runtime.txt
new file mode 100644
index 0000000..6f98a22
--- /dev/null
+++ b/runtime.txt
@@ -0,0 +1 @@
+python-3.8.6
\ No newline at end of file
-- 
GitLab