forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresql.py
More file actions
87 lines (67 loc) · 1.68 KB
/
postgresql.py
File metadata and controls
87 lines (67 loc) · 1.68 KB
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
from pyinfra import host, state
from pyinfra.modules import apt, files, postgresql, python
SUDO = True
if host.fact.linux_name != 'Ubuntu':
# Raises an exception mid-deploy
python.raise_exception(
{'Ensure we are Ubuntu'},
NotImplementedError,
'`postgresql.py` only works on Ubuntu',
)
apt.packages(
{'Install postgresql server & client'},
['postgresql'],
update=True,
cache_time=3600,
)
# Setup a PostgreSQL role & database
#
postgresql.role(
{'Create the pyinfra PostgreSQL role'},
'pyinfra',
password='somepassword',
superuser=True,
login=True,
sudo_user='postgres',
)
postgresql.database(
{'Create the pyinfra_stuff database'},
'pyinfra_stuff',
owner='pyinfra',
encoding='UTF8',
sudo_user='postgres',
)
# 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,
)
postgresql.load(
{'Import the uploaded a_db.sql file'},
temp_filename,
database='pyinfra_stuff',
sudo_user='postgres',
)
# Now duplicate the pyinfra_stuff database -> pyinfra_stuff_copy
#
postgresql.database(
{'Create the pyinfra_stuff_copy database'},
'pyinfra_stuff_copy',
sudo_user='postgres',
)
dump_filename = state.get_temp_filename('psql_dump')
postgresql.dump(
{'Dump the pyinfra_stuff database'},
dump_filename,
database='pyinfra_stuff',
sudo_user='postgres',
)
postgresql.load(
{'Import the pyinfra_stuff dump into pyinfra_stuff_copy'},
dump_filename,
database='pyinfra_stuff_copy',
sudo_user='postgres',
)