Adding note on who approved committer
[ric-plt/e2mgr.git] / Automation / Scripts / k8s_ms.py
1 #!/usr/bin/env python
2
3
4 import sys, os, traceback
5 import docker
6 # Getting the Arguments. if argument are missing exit the script with exit 1
7 try:
8     ms = sys.argv[1].lower()
9     action = sys.argv[2].lower()
10     script = os.path.basename(sys.argv[0])
11 except:
12     print("Usage: %s <microservice> <action> for now only stop action is allowd" % \
13             (os.path.basename(sys.argv[0])))
14     sys.exit(1)
15
16 ms=sys.argv[1].lower()
17 action=sys.argv[2].lower()
18 docker_host_ip=os.environ.get('DOCKER_HOST_IP', False)
19 cms=[]
20 if not docker_host_ip:
21     print('The DOCKER_HOST_IP env varibale is not defined, exiting!')
22     sys.exit(1)
23
24 def get_ms():
25     try: 
26         client = docker.DockerClient(base_url='tcp://%s:2376' % docker_host_ip)
27
28         for ms in client.containers.list():
29             if ms.name == sys.argv[1]:
30                 cms.append(ms)
31                 return cms[0]
32     except:
33         print('Can\'t  connect to docker API, Exiting!')
34         print(traceback.format_exc()) 
35         sys.exit(1)
36          
37 if action == 'stop':
38     print('Stop the %s pod'  % ms )
39     get_ms().stop()
40     sys.exit(0)
41 else:
42     print ('Only stop commnad is allowed!, exiting!')
43     sys.exit(1)