This repository has been archived by the owner on May 5, 2023. It is now read-only.
/
database.py
408 lines (363 loc) · 17.6 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Manages Google Cloud SQL instances, databases and users.
See https://cloud.google.com/sql/docs/
"""
import contextlib
import os
import signal
import shutil
import subprocess
import time
from typing import Optional
import django
from django import db
from django.core import management
from django_cloud_deploy import crash_handling
import pexpect
from pexpect import popen_spawn
from googleapiclient import discovery
from googleapiclient import errors
from google.auth import credentials
class DatabaseError(Exception):
pass
class DatabaseClient(object):
"""A class for managing Google Cloud SQL objects."""
def __init__(self, sqladmin_service: discovery.Resource):
self._sqladmin_service = sqladmin_service
@classmethod
def from_credentials(cls, credentials: credentials.Credentials):
return cls(
discovery.build('sqladmin',
'v1beta4',
credentials=credentials,
cache_discovery=False))
def create_instance_sync(self,
project_id: str,
instance: str,
number_cpus: int = 1,
memory_size: str = 3840,
database_version: str = 'POSTGRES_9_6',
region: str = 'us-west1'):
"""Creates a new Google Cloud SQL instance and wait for provisioning.
See https://cloud.google.com/sql/docs/postgres/create-instance for valid
arguments.
Args:
project_id: The id of the project to provision the SQL instance in.
instance: The name of the new instance being provisioned.
number_cpus: The number of virtual CPUs to provision for the SQL
instance.
memory_size: The amount of memory, in MiB, to provision for the SQL
instance.
database_version: The type of database to provision.
region: The geographic region to provision the SQL instance in.
Raises:
ValueError: for invalid argument combinations.
DatabaseError: if unable to provision the SQL instance.
"""
# See:
# https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances
if not (0 < number_cpus <= 64):
raise ValueError('unexpected cpu count {!r}'.format(number_cpus))
if not (3840 <= memory_size <= 425984):
raise ValueError('unexpected memory size {!r}'.format(memory_size))
tier = 'db-custom-{}-{}'.format(number_cpus, memory_size)
database_instance_body = {
'name': instance,
'region': region,
'databaseVersion': database_version,
'settings': {
'tier': tier,
'backupConfiguration': {
'enabled': True
}
}
}
request = self._sqladmin_service.instances().insert(
project=project_id, body=database_instance_body)
# See
# https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/insert
try:
request.execute(num_retries=5)
except errors.HttpError as e:
if e.resp.status == 409:
# A cloud SQL instance with the same name already exist. This is
# fine because we can reuse this instance.
return
while True:
request = self._sqladmin_service.instances().get(project=project_id,
instance=instance)
response = request.execute(num_retries=5)
# Response format:
# https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances#resource
if response['state'] == 'RUNNABLE':
return
elif response['state'] == 'PENDING_CREATE':
time.sleep(5)
continue
else:
raise DatabaseError(
'unexpected instance status after creation: {!r} [{!r}]'.
format(response['state'], response))
def create_database_sync(self, project_id: str, instance: str,
database: str):
"""Creates a new database in a Cloud SQL instance and wait for completion.
Args:
project_id: The id of the project to create the database in.
instance: The name of the instance to create the database in.
database: The name of the new database to create.
Raises:
DatabaseError: if unable to create the new database.
"""
# See:
# https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/databases/insert
request = self._sqladmin_service.databases().get(project=project_id,
instance=instance,
database=database)
response = []
try:
response = request.execute(num_retries=5)
except errors.HttpError as e:
if e.resp.status == 404:
# The database we would like to create does not exist yet. This
# is what we want.
pass
else:
raise DatabaseError(
('Unexpected response when getting status of database '
'{!r}: [{!r}]').format(database, response))
# This means the database already exist. In this case we do not need
# to create the same database again.
if 'name' in response:
return
request = self._sqladmin_service.databases().insert(project=project_id,
instance=instance,
body={
'instance':
instance,
'project':
project_id,
'name': database
})
response = request.execute(num_retries=5)
while response['status'] in ['PENDING']:
request = self._sqladmin_service.databases().get(project=project_id,
instance=instance,
database=database)
response = request.execute(num_retries=5)
time.sleep(2)
if response['status'] not in ['DONE', 'RUNNING']:
raise DatabaseError(
'unexpected database status after creation: {!r} [{!r}]'.format(
response['status'], response))
def create_database_user(self, project_id: str, instance: str, user: str,
password: str):
"""Create a new database user.
Args:
project_id: The id of the project for the database user.
instance: The name of the instance for the database user.
user: The name of the database user e.g. "postgres".
password: The new password to set.
Raises:
DatabaseError: if unable to create the database user.
"""
# See:
# https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/users/insert
request = self._sqladmin_service.users().insert(project=project_id,
instance=instance,
body={
'name': user,
'password': password
})
response = request.execute(num_retries=5)
if response['status'] not in ['DONE', 'RUNNING']:
raise DatabaseError(
('Unexpected database status after user creation: '
'{!r} [{!r}]').format(response['status'], response))
def set_database_password(self, project_id: str, instance: str, user: str,
password: str):
"""Set the password for a database user.
Args:
project_id: The id of the project for the database user.
instance: The name of the instance for the database user.
user: The name of the database user e.g. "postgres".
password: The new password to set.
Raises:
DatabaseError: if unable to set the new password.
"""
# See:
# https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/users/update
request = self._sqladmin_service.users().update(
project=project_id,
instance=instance,
host='no-host',
name=user,
body={'password': password})
response = request.execute(num_retries=5)
if response['status'] not in ['DONE', 'RUNNING']:
raise DatabaseError(
'unexpected database status after creation: {!r} [{!r}]'.format(
response['status'], response))
@contextlib.contextmanager
def with_cloud_sql_proxy(self,
project_id: str,
instance_name: str,
cloud_sql_proxy_path: Optional[str] = None,
region: str = 'us-west1',
port: int = 5432):
"""A context manager to run and kill cloud sql proxy subprocesses.
Used to provides secure access to your Cloud SQL Second Generation
instances without having to whitelist IP addresses or configure SSL.
For more information:
https://cloud.google.com/sql/docs/postgres/sql-proxy
Args:
project_id: GCP project id.
instance_name: Name of the Cloud SQL instance cloud sql proxy
targets at.
cloud_sql_proxy_path: The command to run your cloud sql proxy.
region: Where the Cloud SQL instance is in.
port: The port your Postgres database is using. By default it is
5432.
Yields:
None
Raises:
DatabaseError: If cloud sql proxy failed to start after 5 seconds.
"""
try:
db.close_old_connections()
except django.core.exceptions.ImproperlyConfigured:
# The Django environment is not correctly setup. This might be
# because we are calling Django management commands with subprocess
# calls. In this case the subprocess we are calling will handle
# closing of old connections.
pass
instance_connection_string = '{0}:{1}:{2}'.format(
project_id, region, instance_name)
instance_flag = '-instances={}=tcp:{}'.format(
instance_connection_string, port)
if cloud_sql_proxy_path is None:
cloud_sql_proxy_path = shutil.which('cloud_sql_proxy')
assert cloud_sql_proxy_path, 'could not find cloud_sql_proxy_path'
process = popen_spawn.PopenSpawn([cloud_sql_proxy_path, instance_flag])
try:
# Make sure cloud sql proxy is started before doing the real work
process.expect('Ready for new connections', timeout=60)
yield
except pexpect.exceptions.TIMEOUT:
raise DatabaseError(
('Cloud SQL Proxy was unable to start after 60 seconds. Output '
'of cloud_sql_proxy: \n{}').format(process.before))
except pexpect.exceptions.EOF:
raise DatabaseError(
('Cloud SQL Proxy exited unexpectedly. Output of '
'cloud_sql_proxy: \n{}').format(process.before))
finally:
process.kill(signal.SIGTERM)
def migrate_database(self,
project_dir: str,
project_id: str,
instance_name: str,
cloud_sql_proxy_path: str = 'cloud_sql_proxy',
region: str = 'us-west1',
port: Optional[int] = 5432):
"""Migrate to Cloud SQL database.
This function should be called after we do the following:
1. Generated the Django project source files.
2. Setup Django environment so that it is using configuration files
of the newly generated project.
3. Created the Cloud SQL instance and database user.
Args:
project_dir: Absolute path of the Django project directory.
project_id: GCP project id.
instance_name: Name of the Cloud SQL instance where the database you
want to migrate is in.
cloud_sql_proxy_path: The command to run your cloud sql proxy.
region: Where the Cloud SQL instance is in.
port: The port being forwarded by cloud sql proxy.
"""
with self.with_cloud_sql_proxy(project_id, instance_name,
cloud_sql_proxy_path, region, port):
try:
# The environment variable must exist. This is the prerequisite
# of calling this function
settings_module = os.environ['DJANGO_SETTINGS_MODULE']
# "makemigrations" will generate migration files based on
# definitions in models.py.
makemigrations_args = [
'django-admin', 'makemigrations',
'='.join(['--pythonpath', project_dir]),
'='.join(['--settings', settings_module])
]
subprocess.check_call(makemigrations_args,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
# "migrate" will modify cloud sql database.
migrate_args = [
'django-admin', 'migrate',
'='.join(['--pythonpath', project_dir]),
'='.join(['--settings', settings_module])
]
subprocess.check_call(migrate_args,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except Exception as e:
raise crash_handling.UserError(
'Not able to migrate database.') from e
def create_super_user(self,
superuser_name: str,
superuser_email: str,
superuser_password: str,
project_id: str,
instance_name: str,
cloud_sql_proxy_path: str = 'cloud_sql_proxy',
region: str = 'us-west1',
port: Optional[int] = 5432):
"""Create a super user in the cloud sql database.
This function should be called after we did the following:
1. Generated the Django project source files.
2. Setup Django environment so that it is using configuration files
of the newly generated project.
3. Created the Cloud SQL instance and database user.
4. Migrated database. Otherwise the schema for superuser does not
exist.
Args:
superuser_name: Name of the super user you want to create.
superuser_email: Email of the super user you want to create.
superuser_password: Password of the super user you want to create.
project_id: GCP project id.
instance_name: The Cloud SQL instance name in which you want to
create the super user.
cloud_sql_proxy_path: The command to run your cloud sql proxy.
region: Where the Cloud SQL instance is in.
port: The port being forwarded by cloud sql proxy.
"""
with self.with_cloud_sql_proxy(project_id, instance_name,
cloud_sql_proxy_path, region, port):
# This can only be imported after django.setup() is called
try:
from django.contrib.auth import get_user_model
User = get_user_model()
# Check whether the super user we want to create exist or not
# If a superuser with the same name already exist, we will skip
# creation
users = User.objects.filter(username=superuser_name)
for user in users:
if user.is_superuser:
return
User.objects.create_superuser(username=superuser_name,
email=superuser_email,
password=superuser_password)
except Exception as e:
raise crash_handling.UserError(
'Not able to create super user.') from e