forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.py
81 lines (61 loc) · 1.5 KB
/
mysql.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
from pyinfra import host, state
from pyinfra.modules import apt, files, mysql, python
SUDO = True
if host.fact.linux_name != 'Debian':
# Raises an exception mid-deploy
python.raise_exception(
{'Ensure we are Debian'},
NotImplementedError,
'`mysql.py` only works on Debian',
)
apt.packages(
{'Install mysql server & client'},
['mysql-server'],
update=True,
cache_time=3600,
)
# Setup a MySQL role & database
#
mysql.user(
{'Create the pyinfra@localhost MySQL user'},
'pyinfra',
password='somepassword',
)
mysql.database(
{'Create the pyinfra_stuff database'},
'pyinfra_stuff',
user='pyinfra',
user_privileges=['SELECT', 'INSERT'],
charset='utf8',
)
# Upload & import a SQL file into the pyinfra_stuff database
#
filename = 'files/a_db.sql'
temp_filename = state.get_temp_filename(filename)
files.put(
{'Upload the a_db.sql file'},
filename, temp_filename,
)
mysql.load(
{'Import the a_db.sql file'},
temp_filename,
database='pyinfra_stuff',
)
# Now duplicate the pyinfra_stuff database -> pyinfra_stuff_copy
#
mysql.database(
{'Create the pyinfra_stuff_copy database'},
'pyinfra_stuff_copy',
charset='utf8',
)
dump_filename = state.get_temp_filename('mysql_dump')
mysql.dump(
{'Dump the pyinfra_stuff database'},
dump_filename,
database='pyinfra_stuff',
)
mysql.load(
{'Import the pyinfra_stuff dump into pyinfra_stuff_copy'},
dump_filename,
database='pyinfra_stuff_copy',
)