blob: 021e48c989555806d6bc653a8c174c4439e666ff [file] [log] [blame]
Yang Xu3faaa3d2018-03-24 05:33:19 +00001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10# See the License for the specific language governing permissions and
11# limitations under the License.
12
13import subprocess
14import json
15import re
16from decimal import Decimal
17
18
19def get_container_list(ip):
20 """
21 Get the list of containers running on the host
22 Args:
23 param1 (str): host ip
24
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010025 Returns:
Yang Xu3faaa3d2018-03-24 05:33:19 +000026 list of containers in string
27 """
28
29 cmd = ['ssh', '-i', 'onap_dev']
30 cmd.append('ubuntu@' + ip)
31 cmd.append("sudo docker ps --format '{{.Names}}'")
32 ssh = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE,
33 stderr=subprocess.PIPE)
34 result = ssh.stdout.readlines()
35 containers = []
36 if result == []:
37 error = ssh.stderr.readlines()
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010038 print(error)
Yang Xu3faaa3d2018-03-24 05:33:19 +000039 else:
40 for line in result:
41 token = line.decode('ascii').strip()
42 containers.append(token)
43
44 return containers
45
46
47def get_container_volume_size(ip, container):
48 """
49 Get container total volume usage
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010050 Args:
Yang Xu3faaa3d2018-03-24 05:33:19 +000051 param1 (str): host ip
52 param2 (str): container name
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010053
Yang Xu3faaa3d2018-03-24 05:33:19 +000054 Returns:
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010055 float number in GB if the container has volume(s), None otherwise
Yang Xu3faaa3d2018-03-24 05:33:19 +000056 """
57
58 cmd = ['ssh', '-i', 'onap_dev']
59 cmd.append('ubuntu@' + ip)
60 cmd.append("sudo docker inspect -f '{{ json .Mounts }}'")
61 cmd.append(container)
62 ssh = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE,
63 stderr=subprocess.PIPE)
64 result = ssh.stdout.readlines()
65 total = None
66 if result == []:
67 error = ssh.stderr.readlines()
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010068 print(error)
Yang Xu3faaa3d2018-03-24 05:33:19 +000069 else:
70 data = json.loads(result[0])
71 for entry in data:
72 if entry['Type'] == 'volume':
73 name = entry['Name']
74 size = get_volume_size(ip, name)
75 if total is None:
76 total = size
77 else:
78 total = total + size
79
80 return total
81
82
83def get_volume_size(ip, volume):
84 """
85 Get a volume size
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010086 Args:
Yang Xu3faaa3d2018-03-24 05:33:19 +000087 param1 (str): host ip
88 param2 (str): volume name
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +010089
Yang Xu3faaa3d2018-03-24 05:33:19 +000090 Returns:
91 float number in GB
92 """
93
94 cmd = ['ssh', '-i', 'onap_dev']
95 cmd.append('ubuntu@' + ip)
96 cmd.append('sudo docker system df -v')
97 p1 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
98 p2 = subprocess.Popen(['grep', volume], stdin=p1.stdout,
99 stdout=subprocess.PIPE)
100 p1.stdout.close()
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +0100101 (output, err) = p2.communicate() # pylint: disable=W0612
Yang Xu3faaa3d2018-03-24 05:33:19 +0000102 size = output.split()[2]
103 return convert_to_GB(size)
104
105
106def convert_to_GB(s):
107 """
108 Convert volume size to GB
109 Args:
110 param1 (str): volume size with unit
111
Bartek Grzybowski2ee028b2020-03-05 14:35:29 +0100112 Returns:
Yang Xu3faaa3d2018-03-24 05:33:19 +0000113 float number representing volume size in GB
114 """
115
116 if s.endswith('GB'):
117 d = float(re.sub('[^0-9\\.]', '', s))
118 if s.endswith('MB'):
119 d = round(Decimal(float(re.sub('[^0-9\\.]', '', s)) / 1000.0),
120 1)
121 if s.endswith('kB'):
122 d = round(Decimal(float(re.sub('[^0-9\\.]', '', s))
123 / 1000000.0), 1)
124 return d