Yang Xu | 3faaa3d | 2018-03-24 05:33:19 +0000 | [diff] [blame] | 1 | # 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 | |
| 13 | import subprocess |
| 14 | import json |
| 15 | import re |
| 16 | from decimal import Decimal |
| 17 | |
| 18 | |
| 19 | def get_container_list(ip): |
| 20 | """ |
| 21 | Get the list of containers running on the host |
| 22 | Args: |
| 23 | param1 (str): host ip |
| 24 | |
| 25 | Returns: |
| 26 | 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() |
| 38 | print error |
| 39 | else: |
| 40 | for line in result: |
| 41 | token = line.decode('ascii').strip() |
| 42 | containers.append(token) |
| 43 | |
| 44 | return containers |
| 45 | |
| 46 | |
| 47 | def get_container_volume_size(ip, container): |
| 48 | """ |
| 49 | Get container total volume usage |
| 50 | Args: |
| 51 | param1 (str): host ip |
| 52 | param2 (str): container name |
| 53 | |
| 54 | Returns: |
| 55 | float number in GB if the container has volume(s), None otherwise |
| 56 | """ |
| 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() |
| 68 | print error |
| 69 | 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 | |
| 83 | def get_volume_size(ip, volume): |
| 84 | """ |
| 85 | Get a volume size |
| 86 | Args: |
| 87 | param1 (str): host ip |
| 88 | param2 (str): volume name |
| 89 | |
| 90 | 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() |
| 101 | (output, err) = p2.communicate() |
| 102 | size = output.split()[2] |
| 103 | return convert_to_GB(size) |
| 104 | |
| 105 | |
| 106 | def convert_to_GB(s): |
| 107 | """ |
| 108 | Convert volume size to GB |
| 109 | Args: |
| 110 | param1 (str): volume size with unit |
| 111 | |
| 112 | Returns: |
| 113 | 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 |
| 125 | |
| 126 | |