-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresql-restore-create-container
More file actions
62 lines (51 loc) · 2 KB
/
postgresql-restore-create-container
File metadata and controls
62 lines (51 loc) · 2 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
#!/usr/bin/python3
##container commands to create:
# lxc-create -t download -n postgresql -P /home/lxc
# - centos 7 amd64
# lxc-start -n postgresql -P /home/lxc -d
# lxc-attach -n postgresql -P /home/lxc
##yum update and install postgresql inside the container
# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# yum install postgresql11-server
# yum install postgresql11-contrib
##restart the container because systemctl is buggy
# lxc-stop -n postgresql -P /home/lxc
# lxc-start -n postgresql -P /home/lxc -d
import lxc
import sys
import time
#db files
try:
#load the container object
c = lxc.Container("postgresql", "/home/lxc")
#create the container
c.create(
"download",
lxc.LXC_CREATE_QUIET,
{"dist": "centos", "release": "7", "arch": "amd64"})
#start the container
c.start()
#wait for connectivity
#this is not enough time, need to loop through it a few times until container network is ready
while not c.get_ips(timeout=1):
print('Waiting for network...')
# #sleep for a little longer just to be sure
print('Network found, pausing a moment to let it finish starting...')
time.sleep(5)
#run some network commands to install postgres and update all the things
c.attach_wait(lxc.attach_run_command,
["yum", "install", "https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.
rpm", "-y"])
c.attach_wait(lxc.attach_run_command,
["yum", "install", "postgresql11-server", "-y"])
c.attach_wait(lxc.attach_run_command,
["yum", "install", "postgresql11-contrib", "-y"])
c.attach_wait(lxc.attach_run_command,
["yum", "update", "-y"])
#stop the container
c.stop()
print("PostgreSQL container created.")
except:
errormessage = str(sys.exc_info()[0]) + "\n" + str(sys.exc_info()[1]) + "\n" + str(sys.exc_info()[2])
print("Creating PostgreSQL container failed")
print(errormessage)