1
|
|
|
'''checks to see if docker repository image is updated''' |
2
|
|
|
import json |
3
|
|
|
import docker |
4
|
|
|
from helpers.queuehelper import QueueName |
5
|
|
|
from backend.fcmapp import Component |
6
|
|
|
|
7
|
|
|
COMPONENTUPDATE = Component('fullcycle') |
8
|
|
|
|
9
|
|
|
def when_updateweb(channel, method, properties, body): |
10
|
|
|
'''event handler when updateweb event is raised''' |
11
|
|
|
try: |
12
|
|
|
print("[{0}] Received update web message".format(COMPONENTUPDATE.app.now())) |
13
|
|
|
doupdateweb(body.decode()) |
14
|
|
|
except Exception as ex: |
15
|
|
|
COMPONENTUPDATE.app.logexception(ex) |
16
|
|
|
|
17
|
|
|
def doupdateweb(msg): |
18
|
|
|
'''check if web app should be updated''' |
19
|
|
|
doupdate = False |
20
|
|
|
repository_name = COMPONENTUPDATE.app.configuration.get('update.fullcycleweb.name.repository') |
21
|
|
|
client = docker.client.APIClient() |
22
|
|
|
|
23
|
|
|
webstatus = client.pull(repository_name) |
24
|
|
|
print(webstatus) |
25
|
|
|
for line in webstatus.splitlines(): |
26
|
|
|
jline = json.loads(line) |
27
|
|
|
if 'status' in jline and jline['status'].startswith('Status'): |
28
|
|
|
print(jline['status']) |
29
|
|
|
doupdate = jline['status'].find('is up to date') < 0 |
30
|
|
|
|
31
|
|
|
if doupdate: |
32
|
|
|
COMPONENTUPDATE.app.alert('Web application needs update') |
33
|
|
|
container_name = COMPONENTUPDATE.app.configuration.get('update.fullcycleweb.name.container') |
34
|
|
|
try: |
35
|
|
|
#docker stop |
36
|
|
|
client.stop(container_name) |
37
|
|
|
#docker rm |
38
|
|
|
client.remove_container(container_name) |
39
|
|
|
except BaseException: |
40
|
|
|
pass |
41
|
|
|
#docker pull |
42
|
|
|
client.pull(repository_name) |
43
|
|
|
#docker run --name fullcycleweb -d --network=host --restart unless-stopped fullcycle/web |
44
|
|
|
client = docker.from_env() |
45
|
|
|
client.containers.run(repository_name, name=container_name, detach=True, network_mode='host', restart_policy={"Name": "always"}) |
46
|
|
|
COMPONENTUPDATE.app.alert('Web application updated') |
47
|
|
|
|
48
|
|
|
def main(): |
49
|
|
|
if COMPONENTUPDATE.app.isrunnow: |
50
|
|
|
doupdateweb('updateweb') |
51
|
|
|
COMPONENTUPDATE.app.shutdown() |
52
|
|
|
else: |
53
|
|
|
COMPONENTUPDATE.listeningqueue = COMPONENTUPDATE.app.subscribe(QueueName.Q_UPDATEWEB, when_updateweb) |
54
|
|
|
COMPONENTUPDATE.listen() |
55
|
|
|
|
56
|
|
|
if __name__ == "__main__": |
57
|
|
|
main() |
58
|
|
|
|