Browse Source

Installation script, Other repo work

master
Apostolos Fanakis 6 years ago
parent
commit
d5f57afeea
  1. 7
      LICENSE.md
  2. 89
      UI/Database API/SETUP.md
  3. 6
      UI/Database API/create_roles.sql
  4. 326
      UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0001_initial.py
  5. 0
      UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/__init__.py
  6. 8
      UI/Database API/hyrieus/flavoursAPI/settings.py
  7. 85
      UI/Database API/install.sh
  8. 5
      UI/Database API/requirements.txt
  9. 22
      UI/Database API/schema.sql

7
LICENSE.md

@ -0,0 +1,7 @@
"THE BEER-WARE LICENSE"
Copyright (c) 2018 Apostolof, TheofilosG
As long as you retain this notice you can do whatever you want with this stuff.
If we meet some day, and you think this stuff is worth it, you can buy us a
beer in return.

89
UI/Database API/SETUP.md

@ -0,0 +1,89 @@
# Installation
Welcome to **Hyrieus** installation guide (the API for the application Flavours Without Borders).
**This installation guide is intended for computers using Debian based, Linux distributions!**
The software provided will work on other operating systems too, after loosely following this guide, but changes and decisions may have to be made along the way.
This guide will help you set up the database and Django API required to test the application. Although the guide is pretty detailed don't hesitate to open an issue or contact me directly for any questions.
## Prerequisites
To execute the commands bellow you should have a working MySQL server and support for python 3 and pip 3 on the machine you intend to use.
Other than that the only prerequisite is installation of `virtualenv`, `python3-dev` and `libmysqlclient-dev` packages, using the commands:
```bash
sudo apt install virtualenv
sudo apt-get install python3-dev libmysqlclient-dev
```
If you want the API to be available through the public IP of the computer, you should forward the port `8181`.
## Database and API setup
First you need to uninstall MySQL's password validation plugin. To do this, login to the mysql server as root:
```bash
mysql -h localhost -u root -p
```
and run the following sql command:
```mysql
UNINSTALL PLUGIN validate_password;
```
For the Android app to be able to reach the API, you need to provide a route either through your public IP or though your router using your LAN IP. In either case you need to tell the MySQL server *where to listen* by modifying the file `/etc/mysql/mysql.conf.d/mysqld.cnf`. Use a preferred editor to change the line starting with `bind-address` to `bind-address = YOUR_IP`.
**WARNING:** You may not be able to connect to the MySQL server again, until you revert this change!
When done, run the command bellow to restart MySQL server:
```bash
sudo systemctl restart mysql.service
```
You are now ready to setup the database and API application using the command bellow and following the instructions:
```bash
./install.sh
```
The script will prompt for an IP address to use, you should input the same IP as the one used earlier.
The script will create the database, create a python 3 virtual environment, install (locally) all necessary python modules, connect the API application to the database and create an API superuser. Lastly the API server will be started.
When the API server is started you can try using the Android app provided or head over to the admin panel or root API page using the commands:
```bash
sensible-browser YOUR_IP:8181/admin/
sensible-browser YOUR_IP:8181/api/
```
## Uninstalling
To revert all changes made to your system follow the steps bellow:
1. Remove Django files created.
```bash
rm -rf hyrieus/env
rm -rf hyrieus/flavoursAPI/hyrieus/migrations
```
2. Revert changes done to the settings.py file by the installation script:
```bash
git checkout -- settings.py
```
3. Revert changes to the file `/etc/mysql/mysql.conf.d/mysqld.cnf`. Use an editor to change the line starting with `bind-address` to `bind-address = 127.0.0.1`.
4. Reinstall MySQL's password validation plugin. Login to the mysql server as root:
```bash
mysql -h localhost -u root -p
```
and run the following SQL command:
```mysql
INSTALL PLUGIN validate_password SONAME 'validate_password.so';
```
5. Connect to MySQL server and drop the flavoursWithoutBorders schema:
```bash
mysql -h localhost -u root -p -e "DROP DATABASE IF EXISTS flavoursWithoutBorders;"
```
---
<sub><sub>Hyrieus was a Greek mythical figure known for his son, Orion. Gods gave Orion to Hyrieus because he was super lonely, like me when working on this assignment. 😔</sub></sub>

6
UI/Database API/create_roles.sql

@ -0,0 +1,6 @@
USE `flavoursWithoutBorders`;
LOCK TABLES `role` WRITE;
/*!40000 ALTER TABLE `role` DISABLE KEYS */;
INSERT INTO `role` VALUES (0,'user','Απλός χρήστης'),(1,'moderator','Διαχειριστής'),(2,'owner','Ιδιοκτήτης');
/*!40000 ALTER TABLE `role` ENABLE KEYS */;
UNLOCK TABLES;

326
UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/0001_initial.py

@ -1,326 +0,0 @@
# Generated by Django 2.1.4 on 2019-01-13 16:56
from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0009_alter_user_last_name_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('user_age', models.DateField(null=True)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
],
options={
'db_table': 'user',
'managed': True,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='Diet',
fields=[
('diet_id', models.AutoField(primary_key=True, serialize=False)),
('diet_name', models.CharField(max_length=500)),
('diet_description', models.CharField(blank=True, max_length=700, null=True)),
('diet_is_approved', models.BooleanField(default=False)),
],
options={
'db_table': 'diet',
'managed': True,
},
),
migrations.CreateModel(
name='DietProhibitsIngredient',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('diet', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Diet')),
],
options={
'db_table': 'diet_prohibits_ingredient',
'managed': True,
},
),
migrations.CreateModel(
name='Drink',
fields=[
('drink_id', models.AutoField(primary_key=True, serialize=False)),
('drink_name', models.CharField(max_length=500)),
('drink_description', models.CharField(blank=True, max_length=700, null=True)),
('drink_is_approved', models.BooleanField(default=False)),
],
options={
'db_table': 'drink',
'managed': True,
},
),
migrations.CreateModel(
name='DrinkHasIngredient',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('drink', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Drink')),
],
options={
'db_table': 'drink_has_ingredient',
'managed': True,
},
),
migrations.CreateModel(
name='Food',
fields=[
('food_id', models.AutoField(primary_key=True, serialize=False)),
('food_name', models.CharField(max_length=500)),
('food_description', models.CharField(blank=True, max_length=700, null=True)),
('food_calories', models.IntegerField(blank=True, null=True)),
('food_is_approved', models.BooleanField(default=False)),
],
options={
'db_table': 'food',
'managed': True,
},
),
migrations.CreateModel(
name='FoodHasIngredient',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('food', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Food')),
],
options={
'db_table': 'food_has_ingredient',
'managed': True,
},
),
migrations.CreateModel(
name='Ingredient',
fields=[
('ingredient_name', models.CharField(max_length=500, primary_key=True, serialize=False)),
('ingredient_has_alcohol', models.BooleanField(blank=True, default=False, null=True)),
],
options={
'db_table': 'ingredient',
'managed': True,
},
),
migrations.CreateModel(
name='Permission',
fields=[
('permission_id', models.AutoField(primary_key=True, serialize=False)),
('permission_description', models.CharField(max_length=700)),
],
options={
'db_table': 'permission',
'managed': True,
},
),
migrations.CreateModel(
name='Restaurant',
fields=[
('restaurant_id', models.AutoField(primary_key=True, serialize=False)),
('restaurant_name', models.CharField(max_length=500)),
('restaurant_category', models.CharField(max_length=10)),
('restaurant_longitude', models.FloatField()),
('restaurant_latitude', models.FloatField()),
('restaurant_opening', models.TimeField(blank=True, null=True)),
('restaurant_closing', models.TimeField(blank=True, null=True)),
('restaurant_is_approved', models.BooleanField(default=False)),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'restaurant',
'managed': True,
},
),
migrations.CreateModel(
name='Role',
fields=[
('role_id', models.IntegerField(primary_key=True, serialize=False)),
('role_name', models.CharField(max_length=500)),
('role_description', models.CharField(max_length=700)),
],
options={
'db_table': 'role',
'managed': True,
},
),
migrations.CreateModel(
name='RoleHasPermission',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('permission', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Permission')),
('role', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Role')),
],
options={
'db_table': 'role_has_permission',
'managed': True,
},
),
migrations.CreateModel(
name='UserFollowsDiet',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('diet', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Diet')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'user_follows_diet',
'managed': True,
},
),
migrations.CreateModel(
name='UserProhibitsIngredient',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ingredient_name', models.ForeignKey(db_column='ingredient_name', on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Ingredient')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'user_prohibits_ingredient',
'managed': True,
},
),
migrations.CreateModel(
name='UserRatesDrink',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating_grade', models.IntegerField()),
('rating_date', models.DateField(auto_now=True)),
('rating_text', models.CharField(blank=True, max_length=700, null=True)),
('rating_pοrtion_size', models.CharField(blank=True, max_length=6, null=True)),
('drink', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Drink')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'user_rates_drink',
'managed': True,
},
),
migrations.CreateModel(
name='UserRatesFood',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating_grade', models.IntegerField()),
('rating_date', models.DateField(auto_now=True)),
('rating_text', models.CharField(blank=True, max_length=700, null=True)),
('rating_pοrtion_size', models.CharField(blank=True, max_length=6, null=True)),
('food', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Food')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'user_rates_food',
'managed': True,
},
),
migrations.CreateModel(
name='UserRatesRestaurant',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating_grade', models.IntegerField()),
('rating_date', models.DateField(auto_now=True)),
('rating_text', models.CharField(blank=True, max_length=700, null=True)),
('rating_accessibility', models.CharField(blank=True, max_length=8, null=True)),
('diet', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Diet')),
('restaurant', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Restaurant')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'user_rates_restaurant',
'managed': True,
},
),
migrations.AddField(
model_name='foodhasingredient',
name='ingredient_name',
field=models.ForeignKey(db_column='ingredient_name', on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Ingredient'),
),
migrations.AddField(
model_name='food',
name='restaurant',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Restaurant'),
),
migrations.AddField(
model_name='drinkhasingredient',
name='ingredient_name',
field=models.ForeignKey(db_column='ingredient_name', on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Ingredient'),
),
migrations.AddField(
model_name='drink',
name='restaurant',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Restaurant'),
),
migrations.AddField(
model_name='dietprohibitsingredient',
name='ingredient_name',
field=models.ForeignKey(db_column='ingredient_name', on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Ingredient'),
),
migrations.AddField(
model_name='user',
name='role',
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.DO_NOTHING, to='hyrieus.Role'),
),
migrations.AddField(
model_name='user',
name='user_permissions',
field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions'),
),
migrations.AlterUniqueTogether(
name='userratesrestaurant',
unique_together={('user', 'restaurant')},
),
migrations.AlterUniqueTogether(
name='userratesfood',
unique_together={('user', 'food')},
),
migrations.AlterUniqueTogether(
name='userratesdrink',
unique_together={('user', 'drink')},
),
migrations.AlterUniqueTogether(
name='userprohibitsingredient',
unique_together={('user', 'ingredient_name')},
),
migrations.AlterUniqueTogether(
name='userfollowsdiet',
unique_together={('user', 'diet')},
),
migrations.AlterUniqueTogether(
name='rolehaspermission',
unique_together={('role', 'permission')},
),
migrations.AlterUniqueTogether(
name='foodhasingredient',
unique_together={('food', 'ingredient_name')},
),
migrations.AlterUniqueTogether(
name='drinkhasingredient',
unique_together={('drink', 'ingredient_name')},
),
migrations.AlterUniqueTogether(
name='dietprohibitsingredient',
unique_together={('diet', 'ingredient_name')},
),
]

0
UI/Database API/hyrieus/flavoursAPI/hyrieus/migrations/__init__.py

8
UI/Database API/hyrieus/flavoursAPI/settings.py

@ -25,7 +25,7 @@ SECRET_KEY = 'pz6p6wc1%%0c&dc8lutf)k5sba*4sxnv3ooynci0mcv-l-p_j8'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['83.212.109.171']
ALLOWED_HOSTS = ['server_ip_goes_here']
# Application definition
@ -86,10 +86,10 @@ WSGI_APPLICATION = 'flavoursAPI.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'flavours_without_borders',
'NAME': 'flavoursWithoutBorders',
'USER': 'flavoursUser',
'PASSWORD': 'flavours13',
'HOST': '83.212.109.171',
'PASSWORD': 'database_password_goes_here',
'HOST': 'server_ip_goes_here',
'PORT': '',
},
}

85
UI/Database API/install.sh

@ -0,0 +1,85 @@
#!/bin/bash
# If /root/.my.cnf exists then it won't ask for root password
if [ -f /root/.my.cnf ]; then
echo "Creating new Flavours database..."
# mysql -e "CREATE DATABASE IF NOT EXISTS flavoursWithoutBorders /*!40100 DEFAULT CHARACTER SET utf8 */;"
mysql < schema.sql
echo "Database successfully created!"
echo ""
echo "Please enter a password for the new Flavours database user!"
read userpass
echo ""
echo "Creating new user..."
mysql -e "DROP USER IF EXISTS 'flavoursUser'@'localhost';"
mysql -e "DROP USER IF EXISTS 'flavoursUser'@'%';"
mysql -e "CREATE USER 'flavoursUser'@'localhost' IDENTIFIED BY '${userpass}';"
mysql -e "CREATE USER 'flavoursUser'@'%' IDENTIFIED BY '${userpass}';"
echo "User successfully created!"
echo ""
echo "Granting ALL privileges on flavoursWithoutBorders to flavoursUser!"
mysql -e "GRANT ALL ON flavoursWithoutBorders.* TO 'flavoursUser'@'localhost';"
mysql -e "GRANT ALL ON flavoursWithoutBorders.* TO 'flavoursUser'@'%';"
mysql -e "FLUSH PRIVILEGES;"
# If /root/.my.cnf doesn't exist then it'll ask for root password
else
echo "Please enter root user MySQL password!"
read rootpasswd
echo "Creating new Flavours database..."
# mysql -uroot -p${rootpasswd} -e "CREATE DATABASE IF NOT EXISTS flavoursWithoutBorders /*!40100 DEFAULT CHARACTER SET utf8 */;"
mysql -uroot -p${rootpasswd} < schema.sql
echo "Database successfully created!"
echo ""
echo "Please enter a password for the new Flavours database user!"
read userpass
echo ""
echo "Creating new user..."
mysql -uroot -p${rootpasswd} -e "DROP USER IF EXISTS 'flavoursUser'@'localhost';"
mysql -uroot -p${rootpasswd} -e "DROP USER IF EXISTS 'flavoursUser'@'%';"
mysql -uroot -p${rootpasswd} -e "CREATE USER 'flavoursUser'@'localhost' IDENTIFIED BY '${userpass}';"
mysql -uroot -p${rootpasswd} -e "CREATE USER 'flavoursUser'@'%' IDENTIFIED BY '${userpass}';"
echo "User successfully created!"
echo ""
echo "Granting ALL privileges on flavoursWithoutBorders to flavoursUser!"
mysql -uroot -p${rootpasswd} -e "GRANT ALL ON flavoursWithoutBorders.* TO 'flavoursUser'@'localhost';"
mysql -uroot -p${rootpasswd} -e "GRANT ALL ON flavoursWithoutBorders.* TO 'flavoursUser'@'%';"
mysql -uroot -p${rootpasswd} -e "FLUSH PRIVILEGES;"
fi
echo ""
cd hyrieus/
echo "Creating virtual environment..."
virtualenv -p python3 env
source ./env/bin/activate
echo "Environment successfully created!"
echo ""
echo "Installing required modules..."
[ -r "../requirements.txt" ] && pip3 install -r "../requirements.txt"
echo "Modules installed!"
echo ""
echo Input the server IP to use:
read ip_to_use
sed -i "s/server_ip_goes_here/$ip_to_use/g" flavoursAPI/settings.py
sed -i "s/database_password_goes_here/$userpass/g" flavoursAPI/settings.py
echo ""
echo "Making migrations..."
python3 manage.py makemigrations hyrieus
python3 manage.py migrate
mysql -u flavoursUser -p${userpass} < ../create_roles.sql
echo "Done!"
echo ""
echo "Creating API superuser..."
python3 manage.py createsuperuser
echo ""
echo "Starting server..."
python3 manage.py runserver "$ip_to_use:8181"
echo ""
exit

5
UI/Database API/requirements.txt

@ -0,0 +1,5 @@
django
djangorestframework
mysqlclient
django-allauth
django-rest-auth

22
UI/Database API/schema.sql

@ -0,0 +1,22 @@
DROP DATABASE IF EXISTS `flavoursWithoutBorders`;
CREATE DATABASE IF NOT EXISTS `flavoursWithoutBorders` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `flavoursWithoutBorders`;
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
Loading…
Cancel
Save