forked from jamorham/nightscout-vps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jamorham#27 from jamorham/vps-1
Vps 1
- Loading branch information
Showing
19 changed files
with
745 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python | ||
"""Django's command-line utility for administrative tasks.""" | ||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) from exc | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/bash | ||
if [ "`id -u`" != "0" ] | ||
then | ||
echo "Script needs root - pleass run sudo ./run_server_linux.sh" | ||
echo "Cannot continue.." | ||
exit 5 | ||
fi | ||
|
||
|
||
export SECRET_KEY=$(uuidgen) | ||
export ENV_DEBUG=False | ||
export ENV_TOKEN=$(uuidgen) | ||
export NS_CONFIG_FILE=/etc/nsconfig | ||
|
||
sudo apt-get -y install python3-pip | ||
pip install Django | ||
pip install django-extensions Werkzeug | ||
pip install qrcode | ||
|
||
. /etc/free-dns.sh | ||
python3 manage.py migrate >> /tmp/variables_log 2>&1 | ||
|
||
#make sure to put this after the migrate, as the migrate might fail. | ||
export KILL_AFTER_IDLE_TIME=900 | ||
|
||
CERT_LOCATION="/etc/letsencrypt/live/"$HOSTNAME | ||
|
||
echo | ||
echo PLEASE CONNECT TO https://$HOSTNAME:3389/variables?token=$ENV_TOKEN | ||
echo "The server will run for 15 minutes, and after that will stop (if not used). Press ctrl C to stop it before that." | ||
echo | ||
|
||
#python3 manage.py runserver 0.0.0.0:3389 >> /tmp/variables_log 2>&1 | ||
python3 manage.py runserver_plus 0.0.0.0:3389 --cert-file $CERT_LOCATION/cert.pem --key-file $CERT_LOCATION/privkey.pem >> /tmp/variables_log 2>&1 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
ASGI config for server project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings') | ||
|
||
application = get_asgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
""" | ||
Django settings for server project. | ||
Generated by 'django-admin startproject' using Django 3.2.16. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/3.2/ref/settings/ | ||
""" | ||
|
||
from pathlib import Path | ||
import os | ||
|
||
# Build paths inside the project like this: BASE_DIR / 'subdir'. | ||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
|
||
|
||
SECRET_KEY = os.environ.get('SECRET_KEY') | ||
if not SECRET_KEY: | ||
print("system must load with SECRET_KEY. use \"export SECRET_KEY=security token\" before starting") | ||
os._exit(1) | ||
|
||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = os.environ.get('ENV_DEBUG') | ||
if not DEBUG: | ||
print("system must load with DEBUG use \"export ENV_DEBUG=False\" before starting") | ||
os._exit(1) | ||
DEBUG=DEBUG=='True' | ||
|
||
|
||
ALLOWED_HOSTS = ['*'] | ||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = [ | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
'variables.apps.MembersConfig', | ||
'django_extensions' | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
] | ||
|
||
ROOT_URLCONF = 'server.urls' | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
WSGI_APPLICATION = 'server.wsgi.application' | ||
|
||
|
||
# Database | ||
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': BASE_DIR / 'db.sqlite3', | ||
} | ||
} | ||
|
||
|
||
# Password validation | ||
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators | ||
|
||
AUTH_PASSWORD_VALIDATORS = [ | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | ||
}, | ||
] | ||
|
||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/3.2/topics/i18n/ | ||
|
||
LANGUAGE_CODE = 'en-us' | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
# https://docs.djangoproject.com/en/3.2/howto/static-files/ | ||
|
||
STATIC_URL = '/static/' | ||
|
||
# Default primary key field type | ||
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field | ||
|
||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | ||
|
||
# Keep the session key for 15 minutes only. | ||
SESSION_COOKIE_AGE = 900 | ||
SESSION_SAVE_EVERY_REQUEST = True | ||
|
||
#verify this works | ||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""server URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/3.2/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.contrib import admin | ||
from django.urls import include, path | ||
|
||
urlpatterns = [ | ||
path('variables/', include('variables.urls')), | ||
path('admin/', admin.site.urls), | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
WSGI config for server project. | ||
It exposes the WSGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.wsgi import get_wsgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings') | ||
|
||
application = get_wsgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import os | ||
import sys | ||
import pathlib | ||
import re | ||
|
||
|
||
class Object(object): | ||
def __init__(self, key, value): | ||
self.key = key | ||
self.value = value | ||
|
||
|
||
class DB: | ||
def __init__(self, path): | ||
self.path = path | ||
|
||
def parse_line(line): | ||
match = re.match("export *(.*)=[\"'](.*)[\"']", line) | ||
if match: | ||
return Object(match.group(1), match.group(2)) | ||
else: | ||
match = re.match("export *(.*)=(.*)", line) | ||
if match: | ||
return Object(match.group(1), match.group(2)) | ||
return None | ||
|
||
|
||
def get_items(self): | ||
items = [] | ||
if not os.path.exists(self.path): | ||
return items | ||
file = open(self.path, "r") | ||
lines = file.readlines() | ||
|
||
for line in lines: | ||
line = line.strip() | ||
item = DB.parse_line(line) | ||
if item: | ||
items.append(item) | ||
file.close() | ||
return items; | ||
|
||
def convert_item(self, item): | ||
return 'export ' + item.key + '="' + item.value +'"' | ||
|
||
|
||
def append_item(self, item): | ||
file = open(self.path, "a") | ||
newline = "\n" + self.convert_item(item) | ||
file.write(newline) | ||
file.close() | ||
|
||
|
||
def change_item(self, item): | ||
items = self.get_items() | ||
open(self.path, "w").close() #delete all file content. | ||
file_lines = [] | ||
for it in items: | ||
if it.key == item.key: | ||
it.value = item.value | ||
file_lines.append(self.convert_item(it) + "\n") | ||
file = open(self.path, "w") | ||
file.writelines(file_lines) | ||
file.close() | ||
|
||
def remove_item(self, key): | ||
items = self.get_items() | ||
file_lines = [] | ||
file = open(self.path, "w").close() #delete all file content. | ||
for it in items: | ||
if it.key == key: | ||
continue; | ||
file_lines.append(self.convert_item(it)+ "\n") | ||
file = open(self.path, "w") | ||
file.writelines(file_lines) | ||
file.close() | ||
|
||
def test_line(line, expected_key, expected_val): | ||
out = DB.parse_line(line) | ||
if out.key != expected_key or out.value != expected_val: | ||
print("test failed for line", line, "out.key=", out.key, "out.value=", out.value) | ||
|
||
def test_re(): | ||
print("testint") | ||
test_line('export xxx="1"', "xxx", "1") | ||
test_line('export xxx=1' , "xxx", "1") | ||
test_line('export xxx="1 2"', "xxx", "1 2") | ||
test_line('export xxx=1 2', "xxx", "1 2") | ||
test_line('export xxx="1 \"2"', "xxx", "1 \"2") | ||
test_line('export xxx="1 \'*\'2"', "xxx", "1 '*'2") | ||
# does not work test_line('export xxx="1 "*"2"', "xxx", "1") | ||
test_line("export xxx='1'", "xxx", "1") | ||
test_line("export xxx='1 \"2'", "xxx", "1 \"2") | ||
|
||
if __name__ == "__main__": | ||
test_re() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
Oops, something went wrong.