-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_active.py
executable file
·37 lines (31 loc) · 1.25 KB
/
update_active.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
#!/usr/bin/python
import os
import psycopg2
from docker import Client
import config as con
docker = con.fig.docker
cli=Client(con.fig.base_url)
def update_active():
try:
containers = cli.containers(all=True)
jsonport=cli.inspect_container('pgjson')['HostConfig']['PortBindings']['5432/tcp'][0]['HostPort']
jsonpw = [password for password in str(cli.inspect_container('pgjson')['Config']['Env']).split() \
if 'PGPASSWORD' in password][0].split('=')[1][:-2]
conn_string="dbname=pgjson user=pgjson host=" + con.fig.host + " port=" + \
jsonport + " password=" + jsonpw
conn=psycopg2.connect(conn_string)
cur=conn.cursor()
for con in containers:
if 'Labels' in con and 'DBaaS' in con['Labels'] and con['Labels']['DBaaS']=='true':
Name=con['Names'][0].strip('/')
if con['State'] == 'running':
status='TRUE'
else:
status='FALSE'
sqlcmd= "update updated_containers set active=%s where container=\'%s\';" % (status, Name)
cur.execute(sqlcmd)
conn.commit()
except Exception,e:
print("Error occured %s" % e)
if __name__ == "__main__":
update_active()