From 78c7aa42117c20a74358777ddb977eba969f0c14 Mon Sep 17 00:00:00 2001 From: tancou Date: Thu, 19 Mar 2020 21:11:20 +0100 Subject: [PATCH 01/17] Use of Python 3 --- .python-version | 1 + requirements.apt | 2 +- requirements.txt | 13 +++++++------ tsuru.yaml | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 .python-version diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..0833a98 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.7.4 diff --git a/requirements.apt b/requirements.apt index d4588d5..c211429 100644 --- a/requirements.apt +++ b/requirements.apt @@ -1,3 +1,3 @@ python-dev -libmysqlclient-dev +default-libmysqlclient-dev libevent-dev diff --git a/requirements.txt b/requirements.txt index 66fefb6..55da116 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ -Django==1.6 -MySQL-python==1.2.3 -boto==2.7.0 -crane-ec2==0.2.1 -gunicorn==0.14.6 -gevent==0.13.8 +Django==1.9 +mysqlclient +#MySQL-python==1.2.5 +boto +crane-ec2 +gunicorn +gevent diff --git a/tsuru.yaml b/tsuru.yaml index a196a7d..8ed3db5 100644 --- a/tsuru.yaml +++ b/tsuru.yaml @@ -1,4 +1,4 @@ hooks: build: - - python manage.py syncdb --noinput + - python manage.py migrate --noinput From fda6c3c31b19ec16e94a349808b93a55d094c2de Mon Sep 17 00:00:00 2001 From: tancou Date: Thu, 19 Mar 2020 21:15:05 +0100 Subject: [PATCH 02/17] FIX code for python 3 --- mysqlapi/api/creator.py | 4 ++-- mysqlapi/api/views.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mysqlapi/api/creator.py b/mysqlapi/api/creator.py index 0ccebf0..edbb6b0 100644 --- a/mysqlapi/api/creator.py +++ b/mysqlapi/api/creator.py @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -import Queue +import queue import threading model_class = None @@ -11,7 +11,7 @@ class InstanceQueue(object): def __init__(self): - self._queue = Queue.Queue() + self._queue = queue.Queue() self._closed = False self._sem = threading.Semaphore() diff --git a/mysqlapi/api/views.py b/mysqlapi/api/views.py index 7a1f604..f47f0b0 100644 --- a/mysqlapi/api/views.py +++ b/mysqlapi/api/views.py @@ -33,7 +33,7 @@ def post(self, request, name, *args, **kwargs): db = instance.db_manager() try: username, password = db.create_user(name, None) - except Exception, e: + except Exception as e: return HttpResponse(e.args[-1], status=500) config = { "MYSQL_HOST": db.public_host, @@ -53,7 +53,7 @@ def delete(self, request, name, *args, **kwargs): db = instance.db_manager() try: db.drop_user(name, None) - except Exception, e: + except Exception as e: return HttpResponse(e.args[-1], status=500) return HttpResponse("", status=200) @@ -123,7 +123,7 @@ def export(request, name): try: db = DatabaseManager(name, host) return HttpResponse(db.export()) - except subprocess.CalledProcessError, e: + except subprocess.CalledProcessError as e: return HttpResponse(e.output.split(":")[-1].strip(), status=500) From c76620c5b40394928c20c0a6ad93ce931a49c93f Mon Sep 17 00:00:00 2001 From: tancou Date: Thu, 19 Mar 2020 21:15:34 +0100 Subject: [PATCH 03/17] FIX Django database foreignKey --- mysqlapi/api/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysqlapi/api/models.py b/mysqlapi/api/models.py index 904b507..76de9c5 100644 --- a/mysqlapi/api/models.py +++ b/mysqlapi/api/models.py @@ -164,7 +164,7 @@ def db_manager(self): class ProvisionedInstance(models.Model): - instance = models.ForeignKey(Instance, null=True, blank=True, unique=True) + instance = models.ForeignKey(Instance, null=True, blank=True, unique=True, on_delete = models.CASCADE) host = models.CharField(max_length=500) port = models.IntegerField(default=3306) admin_user = models.CharField(max_length=255, default="root") From bc06683397062c5a20935857b7a74a9d1d12b306 Mon Sep 17 00:00:00 2001 From: tancou Date: Thu, 19 Mar 2020 21:22:58 +0100 Subject: [PATCH 04/17] Update Readme with new Django command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6018195..e758059 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The first step is to install the dependencies. Let's use pip to do it: Now we need to run syncdb: - $ python manage.py syncdb + $ python manage.py migrate Exporting enviroment variable to set the settings location: From 6f12b80d95fa72243b75cc3659c3a389315a0752 Mon Sep 17 00:00:00 2001 From: tancou Date: Fri, 20 Mar 2020 01:56:15 +0100 Subject: [PATCH 05/17] FIX code for python 3 --- mysqlapi/api/creator.py | 2 +- mysqlapi/settings.py | 4 ++++ requirements.txt | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mysqlapi/api/creator.py b/mysqlapi/api/creator.py index edbb6b0..a20e7cd 100644 --- a/mysqlapi/api/creator.py +++ b/mysqlapi/api/creator.py @@ -55,7 +55,7 @@ def run(self): while not _instance_queue.closed: try: instance = _instance_queue.get(timeout=2) - except Queue.Empty: + except queue.Empty: continue if not self.ec2_client.get(instance): _instance_queue.put(instance) diff --git a/mysqlapi/settings.py b/mysqlapi/settings.py index df74b84..4f063cb 100644 --- a/mysqlapi/settings.py +++ b/mysqlapi/settings.py @@ -4,6 +4,8 @@ import os +import django + ROOT = os.path.abspath(os.path.dirname(__file__)) DEBUG = int(os.environ.get("MYSQLAPI_DEBUG", 1)) != 0 TEMPLATE_DEBUG = DEBUG @@ -170,3 +172,5 @@ ALLOWED_HOSTS = [ os.environ.get("MYSQLAPI_ALLOWED_HOST", "localhost"), ] + +django.setup() diff --git a/requirements.txt b/requirements.txt index 55da116..c0fed76 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,6 @@ Django==1.9 mysqlclient #MySQL-python==1.2.5 boto -crane-ec2 +crane-ec2==0.2.1 gunicorn gevent From fe57efb43da853180182a564c7e93f3a415c7452 Mon Sep 17 00:00:00 2001 From: tancou Date: Fri, 20 Mar 2020 01:56:42 +0100 Subject: [PATCH 06/17] Update config --- service.yaml | 6 +++--- tsuru.yaml | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/service.yaml b/service.yaml index 9de675e..7e8e82c 100644 --- a/service.yaml +++ b/service.yaml @@ -1,6 +1,6 @@ id: mysqlapi -password: mysql123 +username: mysqlapi +password: password team: admin endpoint: - production: mysqlapi.com - test: localhost:8000 + production: production-endpoint.com \ No newline at end of file diff --git a/tsuru.yaml b/tsuru.yaml index 8ed3db5..6b59cae 100644 --- a/tsuru.yaml +++ b/tsuru.yaml @@ -1,4 +1,5 @@ hooks: build: + - python manage.py makemigrations --noinput - python manage.py migrate --noinput From 4e200038659de19c9246b0b6de5c0ba3caa20c28 Mon Sep 17 00:00:00 2001 From: tancou Date: Fri, 20 Mar 2020 09:53:26 +0100 Subject: [PATCH 07/17] Add Mysql 8 support --- README.md | 37 +++++++++++++++++++++++++++++-------- mysqlapi/api/models.py | 21 ++++++++++++++++----- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e758059..0a664f2 100644 --- a/README.md +++ b/README.md @@ -5,16 +5,18 @@ This is a service API for MySQL, used for [tsuru](https://github.com/tsuru/tsuru). -Installation +Installation on dedicated host ------------ In order to have mysql API ready to receive requests, we need some bootstrap stuff. +Requirements : `Python 3.7` + The first step is to install the dependencies. Let's use pip to do it: $ pip install -r requirements.txt -Now we need to run syncdb: +Now we need to run a migration before serving: $ python manage.py migrate @@ -61,6 +63,8 @@ accessible endpoint to be used by the apps that are using the service: Running the api --------------- +Mysql must be installed on the same machine, otherwise, set environment variables as suggested in the next section. + $ gunicorn wsgi -b 0.0.0.0:8888 @@ -69,7 +73,7 @@ Try your configuration You can try if the previous configuration worked using curl: - $> curl -d 'name=myapp' http://youmysqlapi.com/resources + $> curl -d 'name=myapp' http://youmysqlapi.com This call is the same as to ``tsuru service-add `` and will return 201 if everything goes ok. @@ -85,10 +89,16 @@ You can deploy `mysqlapi` as tsuru appplication. ### Install MySQL server (Debian/Ubuntu) -First you should have a MySQL server. In Debian/Ubuntu, use `apt-get` to install it. +First you should have a MySQL server. In Debian/Ubuntu, use `apt` to install it. +*Mysql 8 (Newer version)* ```bash -$ sudo apt-get install mysql-server-5.6 +$ sudo apt install mysql +``` + +*Old Mysql 5.6 version* +```bash +$ sudo apt install mysql-server-5.6 ``` During install the installation script will aks you the password for `root` user. @@ -107,8 +117,19 @@ Here is an example: ```sql CREATE DATABASE mysqlapi CHARACTER SET utf8 COLLATE utf8_bin; +``` + +*Mysql 8 (Newer version)* +```bash +CREATE USER 'mysqlapi'@'%' IDENTIFIED BY 'mysqlpass'; +GRANT ALL PRIVILEGES ON mysqlapi.* TO 'mysqlapi'@'%'; +``` + +*Old Mysql 5.6 version* +```sql GRANT ALL PRIVILEGES ON mysqlapi.* TO 'mysqlapi'@'%' IDENTIFIED BY 'mysqlpass'; ``` + Configure mysql to accept external connection, create a file `/etc/mysql/conf.d/bind.cnf` with the following content: ``` @@ -150,6 +171,8 @@ tsuru env-set -a mysqlapi DJANGO_SETTINGS_MODULE=mysqlapi.settings Export these variables to specify the shared cluster: +In that configuration, *`root` user has to be accessible by the app*. If needed, create a new root user on Mysql with a secure ip range. + ```bash # these settings can be different with mysqlapi's database $ tsuru env-set -a mysqlapi MYSQLAPI_SHARED_SERVER=db.192.168.50.4.nip.io @@ -176,13 +199,11 @@ Configure the service template and point it to your application: $ tsuru app-info -a mysqlapi | grep Address # set production address $ editor service.yaml -$ crane create service.yaml +$ tsuru service-create create service.yaml ``` To list your services: ```bash -$ crane list -# OR $ tsuru service-list ``` diff --git a/mysqlapi/api/models.py b/mysqlapi/api/models.py index 76de9c5..259be43 100644 --- a/mysqlapi/api/models.py +++ b/mysqlapi/api/models.py @@ -68,8 +68,12 @@ def public_host(self): def create_database(self): self.conn.open() cursor = self.conn.cursor() - sql = "CREATE DATABASE %s default character set utf8 " + \ - "default collate utf8_general_ci" + if settings.MSQL_5_VERSION_ENABLED: + sql = "CREATE DATABASE %s default character set utf8 " + \ + "default collate utf8_general_ci" + else: + sql = "CREATE DATABASE %s default character set utf8mb4 " + \ + "default collate utf8mb4_unicode_ci" cursor.execute(sql % self.name) self.conn.close() @@ -84,9 +88,16 @@ def create_user(self, username, host): cursor = self.conn.cursor() username = generate_user(username) password = generate_password(username) - sql = ("grant all privileges on {0}.* to '{1}'@'%'" - " identified by '{2}'") - cursor.execute(sql.format(self.name, username, password)) + + if settings.MSQL_5_VERSION_ENABLED: + sql = ("grant all privileges on {0}.* to '{1}'@'%'" + " identified by '{2}'") + cursor.execute(sql.format(self.name, username, password)) + else: + sql = ("CREATE USER '{0}'@'%' identified by '{1}'") + cursor.execute(sql.format(username, password)) + sql = ("grant all privileges on {0}.* to '{1}'@'%'") + cursor.execute(sql.format(self.name, username)) self.conn.close() return username, password From d5f8ea7312bc75bbae6943df0756f9102ced92f7 Mon Sep 17 00:00:00 2001 From: tancou Date: Fri, 20 Mar 2020 14:13:45 +0100 Subject: [PATCH 08/17] FIX password generation --- README.md | 7 ++++--- mysqlapi/api/models.py | 12 ++++++------ mysqlapi/api/tests/test_models.py | 6 ++---- mysqlapi/settings.py | 5 +++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0a664f2..5ffd3a0 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,10 @@ $ sudo apt install mysql-server-5.6 During install the installation script will aks you the password for `root` user. +By default, mysqlapi use Mysql 8. Set the environment variable `MSQL_5_VERSION_ENABLED=True` to enable Mysql 5.6 version. +``` +tsuru env-set -a mysqlapi MSQL_5_VERSION_ENABLED=True +``` #### Create database for mysqlapi @@ -162,9 +166,6 @@ $ tsuru env-set -a mysqlapi MYSQLAPI_DB_USER=mysqlapi $ tsuru env-set -a mysqlapi MYSQLAPI_DB_PASSWORD=mysqlpass $ tsuru env-set -a mysqlapi MYSQLAPI_DB_HOST=db.192.168.50.4.nip.io -# salt used to hash the username/password -$ tsuru env-set -a mysqlapi MYSQLAPI_SALT=****** - # Exporting enviroment variable to set the settings location tsuru env-set -a mysqlapi DJANGO_SETTINGS_MODULE=mysqlapi.settings ``` diff --git a/mysqlapi/api/models.py b/mysqlapi/api/models.py index 259be43..81c043d 100644 --- a/mysqlapi/api/models.py +++ b/mysqlapi/api/models.py @@ -32,13 +32,13 @@ class DatabaseCreationError(Exception): pass -def generate_password(string): - return hashlib.sha1(string + settings.SALT).hexdigest() +def generate_password(): + return hashlib.sha1(str(os.urandom(256)).encode('utf-8')).hexdigest() def generate_user(username): if len(username) > 16: - _username = username[:12] + generate_password(username)[:4] + _username = username[:12] + generate_password()[:4] else: _username = username return _username @@ -87,16 +87,16 @@ def create_user(self, username, host): self.conn.open() cursor = self.conn.cursor() username = generate_user(username) - password = generate_password(username) + password = generate_password() if settings.MSQL_5_VERSION_ENABLED: sql = ("grant all privileges on {0}.* to '{1}'@'%'" " identified by '{2}'") cursor.execute(sql.format(self.name, username, password)) else: - sql = ("CREATE USER '{0}'@'%' identified by '{1}'") + sql = ("CREATE USER '{0}'@'%' IDENTIFIED BY '{1}';") cursor.execute(sql.format(username, password)) - sql = ("grant all privileges on {0}.* to '{1}'@'%'") + sql = ("GRANT ALL PRIVILEGES ON {0}.* TO '{1}'@'%';") cursor.execute(sql.format(self.name, username)) self.conn.close() return username, password diff --git a/mysqlapi/api/tests/test_models.py b/mysqlapi/api/tests/test_models.py index bf2376b..f81d0be 100644 --- a/mysqlapi/api/tests/test_models.py +++ b/mysqlapi/api/tests/test_models.py @@ -378,8 +378,6 @@ def test_canonicalize_db_name_do_nothing_when_called_twice(self): class GeneratePasswordTestCase(TestCase): - @override_settings(SALT="salt") def test_generate_password(self): - expected = hashlib.sha1("bla" + settings.SALT).hexdigest() - result = models.generate_password("bla") - self.assertEqual(expected, result) + result = models.generate_password() + self.assertIsNotNone(result) diff --git a/mysqlapi/settings.py b/mysqlapi/settings.py index 4f063cb..1bd1412 100644 --- a/mysqlapi/settings.py +++ b/mysqlapi/settings.py @@ -167,10 +167,11 @@ S3_SECRET_KEY = os.environ.get("TSURU_S3_SECRET_KEY") S3_BUCKET = os.environ.get("TSURU_S3_BUCKET") -SALT = os.environ.get("MYSQLAPI_SALT", "") - ALLOWED_HOSTS = [ os.environ.get("MYSQLAPI_ALLOWED_HOST", "localhost"), ] +MSQL_5_VERSION_ENABLED = os.environ.get("MSQL_5_VERSION_ENABLED", 'False') in \ + ("True", "true", "1") + django.setup() From 86121a4cf46aeb9919e28da1198cb64495153b79 Mon Sep 17 00:00:00 2001 From: tancou Date: Fri, 20 Mar 2020 16:33:14 +0100 Subject: [PATCH 09/17] Typo Readme --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5ffd3a0..4f13a32 100644 --- a/README.md +++ b/README.md @@ -104,8 +104,8 @@ $ sudo apt install mysql-server-5.6 During install the installation script will aks you the password for `root` user. By default, mysqlapi use Mysql 8. Set the environment variable `MSQL_5_VERSION_ENABLED=True` to enable Mysql 5.6 version. -``` -tsuru env-set -a mysqlapi MSQL_5_VERSION_ENABLED=True +```bash +$ tsuru env-set -a mysqlapi MSQL_5_VERSION_ENABLED=True ``` #### Create database for mysqlapi @@ -113,7 +113,7 @@ tsuru env-set -a mysqlapi MSQL_5_VERSION_ENABLED=True After install MySQL, you need to create a user and a database for `mysqlapi`, that is needed to store informations about created instances. -``` +```bash mysql -u root -p ``` @@ -143,7 +143,7 @@ bind-address = 0.0.0.0 To finish restart MySQL server: -``` +```bash sudo service mysql restart ``` @@ -203,6 +203,12 @@ $ editor service.yaml $ tsuru service-create create service.yaml ``` +Prepare for production: + +```bash +$ tsuru env-set -a mysqlapi MYSQLAPI_DEBUG=0 +``` + To list your services: ```bash From e278550c0f616f712f4bf2efd40193dd98c19eaf Mon Sep 17 00:00:00 2001 From: tancou Date: Fri, 20 Mar 2020 16:34:21 +0100 Subject: [PATCH 10/17] Add scalability to bind multiple apps to one instance --- mysqlapi/api/models.py | 15 ++++++++------- mysqlapi/api/views.py | 20 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/mysqlapi/api/models.py b/mysqlapi/api/models.py index 81c043d..3543f0d 100644 --- a/mysqlapi/api/models.py +++ b/mysqlapi/api/models.py @@ -36,12 +36,13 @@ def generate_password(): return hashlib.sha1(str(os.urandom(256)).encode('utf-8')).hexdigest() -def generate_user(username): - if len(username) > 16: - _username = username[:12] + generate_password()[:4] +def generate_user(username, host): + userhost = username + '-' + host + if len(userhost) > 20: + _userhost = userhost[:20] else: - _username = username - return _username + _userhost = userhost + return (_userhost + '-' + hashlib.sha1(str(username + host).encode('utf-8')).hexdigest())[:32] class DatabaseManager(object): @@ -86,7 +87,7 @@ def drop_database(self): def create_user(self, username, host): self.conn.open() cursor = self.conn.cursor() - username = generate_user(username) + username = generate_user(username, host) password = generate_password() if settings.MSQL_5_VERSION_ENABLED: @@ -104,7 +105,7 @@ def create_user(self, username, host): def drop_user(self, username, host): self.conn.open() cursor = self.conn.cursor() - username = generate_user(username) + username = generate_user(username, host) cursor.execute("drop user '{0}'@'%'".format(username)) self.conn.close() diff --git a/mysqlapi/api/views.py b/mysqlapi/api/views.py index f47f0b0..2b204e2 100644 --- a/mysqlapi/api/views.py +++ b/mysqlapi/api/views.py @@ -8,6 +8,7 @@ import subprocess from django.http import HttpResponse +from django.http import QueryDict from django.views.decorators.http import require_http_methods from django.views.generic.base import View @@ -31,8 +32,15 @@ def post(self, request, name, *args, **kwargs): msg = u"You can't bind to this instance because it's not running." return HttpResponse(msg, status=412) db = instance.db_manager() + + if "app-name" not in request.POST: + return HttpResponse("Instance app-name is missing", status=500) + appname = request.POST.get("app-name") + if not appname: + return HttpResponse("Instance app-name is empty", status=500) + try: - username, password = db.create_user(name, None) + username, password = db.create_user(name, appname) except Exception as e: return HttpResponse(e.args[-1], status=500) config = { @@ -51,8 +59,16 @@ def delete(self, request, name, *args, **kwargs): except Instance.DoesNotExist: return HttpResponse("Instance not found.", status=404) db = instance.db_manager() + + request_delete = QueryDict(request.body) + if "app-name" not in request_delete: + return HttpResponse("Instance app-name is missing", status=500) + appname = request_delete.get("app-name") + if not appname: + return HttpResponse("Instance app-name is empty", status=500) + try: - db.drop_user(name, None) + db.drop_user(name, appname) except Exception as e: return HttpResponse(e.args[-1], status=500) return HttpResponse("", status=200) From 37c6bd7be19cfea44391f80e0ab865249a222184 Mon Sep 17 00:00:00 2001 From: tancou Date: Fri, 20 Mar 2020 17:16:53 +0100 Subject: [PATCH 11/17] Typo Readme --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4f13a32..ba9f841 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#mysqlapi +# mysqlapi [![Build Status](https://secure.travis-ci.org/tsuru/mysqlapi.png?branch=master)](http://travis-ci.org/tsuru/mysqlapi) @@ -10,9 +10,9 @@ Installation on dedicated host In order to have mysql API ready to receive requests, we need some bootstrap stuff. -Requirements : `Python 3.7` +**Requirements :** `Python 3.7` -The first step is to install the dependencies. Let's use pip to do it: +The first step is to install the dependencies. Let's use `pip to do it: $ pip install -r requirements.txt @@ -20,7 +20,7 @@ Now we need to run a migration before serving: $ python manage.py migrate -Exporting enviroment variable to set the settings location: +Exporting environment variable to set the settings location: $ export DJANGO_SETTINGS_MODULE=mysqlapi.settings @@ -32,7 +32,7 @@ There are three modes to configure the API usage behavior: - `shared`: this configuration forces all applications to share the same mysql installation, in this mode, mysql API will create a new user and a new - database when added/binded by an app. + database when added/binded by one app. - `dedicated (on-demmand)`: every app using mysql will have a single vm for it's usage, in this mode, mysql API will create a vm, install everything needed to run mysql based on a predefined AMI and create a user and password. @@ -49,11 +49,11 @@ Shared Configuration -------------------- To run the API in shared mode, is needed to have a mysql installed and export -two enviroment variables. +two environment variables. One variable is to set the mysql host. If the shared mysql database is installed in the sabe vm that the app is, you can use `localhost` for -``MYSQLAPI_SHARED_SERVER``, but you'll also need to set up a externally +``MYSQLAPI_SHARED_SERVER``, but you'll also need to set up an externally accessible endpoint to be used by the apps that are using the service: $ MYSQLAPI_SHARED_SERVER=mysqlhost.com @@ -91,19 +91,19 @@ You can deploy `mysqlapi` as tsuru appplication. First you should have a MySQL server. In Debian/Ubuntu, use `apt` to install it. -*Mysql 8 (Newer version)* +**Mysql 8 (Newer version, default)** ```bash $ sudo apt install mysql ``` -*Old Mysql 5.6 version* +**Old Mysql 5.6 version** ```bash $ sudo apt install mysql-server-5.6 ``` During install the installation script will aks you the password for `root` user. -By default, mysqlapi use Mysql 8. Set the environment variable `MSQL_5_VERSION_ENABLED=True` to enable Mysql 5.6 version. +By default, `mysqlapi uses Mysql 8. Set the environment variable `MSQL_5_VERSION_ENABLED=True` **to enable Mysql 5.6 version**. ```bash $ tsuru env-set -a mysqlapi MSQL_5_VERSION_ENABLED=True ``` @@ -111,7 +111,7 @@ $ tsuru env-set -a mysqlapi MSQL_5_VERSION_ENABLED=True #### Create database for mysqlapi After install MySQL, you need to create a user and a database for `mysqlapi`, -that is needed to store informations about created instances. +that is needed to store information about created instances. ```bash mysql -u root -p @@ -150,7 +150,7 @@ sudo service mysql restart ### Install service -Now you can install `mysqlapi` service. In your tsuru client machine (with crane installed): +Now you can install `mysqlapi` service. In your tsuru client machine: ```bash $ git clone https://github.com/tsuru/mysqlapi @@ -166,13 +166,13 @@ $ tsuru env-set -a mysqlapi MYSQLAPI_DB_USER=mysqlapi $ tsuru env-set -a mysqlapi MYSQLAPI_DB_PASSWORD=mysqlpass $ tsuru env-set -a mysqlapi MYSQLAPI_DB_HOST=db.192.168.50.4.nip.io -# Exporting enviroment variable to set the settings location +# Exporting environment variable to set the settings location tsuru env-set -a mysqlapi DJANGO_SETTINGS_MODULE=mysqlapi.settings ``` Export these variables to specify the shared cluster: -In that configuration, *`root` user has to be accessible by the app*. If needed, create a new root user on Mysql with a secure ip range. +In that configuration, **`root` user has to be accessible by the app**. If needed, create a new root user on Mysql with a secure ip range corresponding to your app machine ip. ```bash # these settings can be different with mysqlapi's database From 1b8b881e12f11443dfdf492b7e50adc9808b315e Mon Sep 17 00:00:00 2001 From: tancou Date: Tue, 24 Mar 2020 21:38:47 +0100 Subject: [PATCH 12/17] FIX unicode in database name --- mysqlapi/api/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysqlapi/api/models.py b/mysqlapi/api/models.py index 3543f0d..c4d1aa6 100644 --- a/mysqlapi/api/models.py +++ b/mysqlapi/api/models.py @@ -269,6 +269,6 @@ def _create_dedicate_database(instance, ec2_client): def canonicalize_db_name(name): if re.search(r"[\W\s]", name) is not None: - prefix = hashlib.sha1(name).hexdigest()[:10] + prefix = hashlib.sha1(str(name).encode('utf-8')).hexdigest()[:10] name = re.sub(r"[\W\s]", "_", name) + prefix return name From 46208afc3e9d1017e77db3fb53948d1eaf53b992 Mon Sep 17 00:00:00 2001 From: tancou Date: Wed, 25 Mar 2020 16:01:14 +0100 Subject: [PATCH 13/17] Update foreign key for Django 1.9 --- mysqlapi/api/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysqlapi/api/models.py b/mysqlapi/api/models.py index c4d1aa6..bc71f1b 100644 --- a/mysqlapi/api/models.py +++ b/mysqlapi/api/models.py @@ -176,7 +176,7 @@ def db_manager(self): class ProvisionedInstance(models.Model): - instance = models.ForeignKey(Instance, null=True, blank=True, unique=True, on_delete = models.CASCADE) + instance = models.OneToOneField(Instance, null=True, blank=True, on_delete = models.CASCADE) host = models.CharField(max_length=500) port = models.IntegerField(default=3306) admin_user = models.CharField(max_length=255, default="root") From 32d65202ec672ba2f432812b220d3ef4bd7179ad Mon Sep 17 00:00:00 2001 From: tancou Date: Wed, 25 Mar 2020 16:01:31 +0100 Subject: [PATCH 14/17] Typo Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ba9f841..96d5ed5 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,7 @@ Configure the service template and point it to your application: $ tsuru app-info -a mysqlapi | grep Address # set production address $ editor service.yaml -$ tsuru service-create create service.yaml +$ tsuru service-create service.yaml ``` Prepare for production: From f063e5e813777f82494d45daf16a39ab30d7b2a0 Mon Sep 17 00:00:00 2001 From: tancou Date: Wed, 25 Mar 2020 16:01:52 +0100 Subject: [PATCH 15/17] FIX migration doesn't create table with Django 1.9 --- tsuru.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsuru.yaml b/tsuru.yaml index 6b59cae..4e0170f 100644 --- a/tsuru.yaml +++ b/tsuru.yaml @@ -1,5 +1,6 @@ hooks: build: + #- python manage.py syncdb --noinput - python manage.py makemigrations --noinput - - python manage.py migrate --noinput + - python manage.py migrate --run-syncdb --noinput From a3adde49409716729600278b3c6f16d9ac9493c7 Mon Sep 17 00:00:00 2001 From: tancou Date: Wed, 25 Mar 2020 16:31:28 +0100 Subject: [PATCH 16/17] Update Issue info in Readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 96d5ed5..953825f 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,11 @@ tsuru env-set -a mysqlapi DJANGO_SETTINGS_MODULE=mysqlapi.settings Export these variables to specify the shared cluster: In that configuration, **`root` user has to be accessible by the app**. If needed, create a new root user on Mysql with a secure ip range corresponding to your app machine ip. +Please, be careful of security issues concerned about those two commands : +```sql +CREATE USER 'root'@'%' IDENTIFIED BY 'rootpassword'; +GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; +``` ```bash # these settings can be different with mysqlapi's database @@ -214,3 +219,9 @@ To list your services: ```bash $ tsuru service-list ``` + +## Issue + +You can not bind an intance with a dash `-` into the name. Django 1.9 can't parse the name on the url `/resources/firstpart-secondpart/bind-app`. Resulting on a 404 error. + +Instead use underscore `_` in names. \ No newline at end of file From 5a45d3e0eaa1acc9bc0fc7d374635d6073071a62 Mon Sep 17 00:00:00 2001 From: tancou Date: Wed, 25 Mar 2020 21:48:12 +0100 Subject: [PATCH 17/17] Typo Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 953825f..be870d0 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ $ sudo apt install mysql-server-5.6 During install the installation script will aks you the password for `root` user. -By default, `mysqlapi uses Mysql 8. Set the environment variable `MSQL_5_VERSION_ENABLED=True` **to enable Mysql 5.6 version**. +By default, `mysqlapi` uses Mysql 8. Set the environment variable `MSQL_5_VERSION_ENABLED=True` **to enable Mysql 5.6 version**. ```bash $ tsuru env-set -a mysqlapi MSQL_5_VERSION_ENABLED=True ```