|
| 1 | +# Copyright (C) 2018 The Board of Trustees of the Leland Stanford Junior |
| 2 | +# University. |
| 3 | +# Copyright (C) 2017-2018 Vanessa Sochat. |
| 4 | + |
| 5 | +# This program is free software: you can redistribute it and/or modify it |
| 6 | +# under the terms of the GNU Affero General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or (at your |
| 8 | +# option) any later version. |
| 9 | + |
| 10 | +# This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public |
| 13 | +# License for more details. |
| 14 | + |
| 15 | +# You should have received a copy of the GNU Affero General Public License |
| 16 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | + |
| 19 | +def parse_table(table_string, header, remove_rows=1): |
| 20 | + '''parse a table to json from a string, where a header is expected by default. |
| 21 | + Return a jsonified table. |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ========== |
| 25 | + table_string: the string table, ideally with a header |
| 26 | + header: header of expected table, must match dimension (number columns) |
| 27 | + remove_rows: an integer to indicate a number of rows to remove from top |
| 28 | + the default is 1 assuming we don't want the header |
| 29 | + ''' |
| 30 | + rows = [x for x in table_string.split('\n') if x] |
| 31 | + rows = rows[0+remove_rows:] |
| 32 | + |
| 33 | + # Parse into json dictionary |
| 34 | + parsed = [] |
| 35 | + |
| 36 | + for row in rows: |
| 37 | + item = {} |
| 38 | + # This assumes no white spaces in each entry, which should be the case |
| 39 | + row = [x for x in row.split(' ') if x] |
| 40 | + for e in range(len(row)): |
| 41 | + item[header[e]] = row[e] |
| 42 | + parsed.append(item) |
| 43 | + return parsed |
| 44 | + |
| 45 | + |
| 46 | +def get(self, name, return_json=False, quiet=False): |
| 47 | + '''get is a list for a single instance. It is assumed to be running, |
| 48 | + and we need to look up the PID, etc. |
| 49 | + ''' |
| 50 | + self._check_install() |
| 51 | + cmd = self._init_command('instance.list') |
| 52 | + cmd.append(name) |
| 53 | + output = run_command(cmd, quiet=True) |
| 54 | + |
| 55 | + # Success, we have instances |
| 56 | + |
| 57 | + if output['return_code'] == 0: |
| 58 | + |
| 59 | + # Only print the table if we are returning json |
| 60 | + if quiet is False: |
| 61 | + print(''.join(output['message'])) |
| 62 | + |
| 63 | + # Prepare json result from table |
| 64 | + |
| 65 | + header = ['daemon_name','pid','container_image'] |
| 66 | + instances = parse_table(output['message'][0], header) |
| 67 | + |
| 68 | + # Does the user want instance objects instead? |
| 69 | + listing = [] |
| 70 | + if return_json is False: |
| 71 | + for i in instances: |
| 72 | + new_instance = Instance(pid=i['pid'], |
| 73 | + name=i['daemon_name'], |
| 74 | + image=i['container_image'], |
| 75 | + start=False) |
| 76 | + |
| 77 | + listing.append(new_instance) |
| 78 | + instances = listing |
| 79 | + |
| 80 | + # Couldn't get UID |
| 81 | + |
| 82 | + elif output['return_code'] == 255: |
| 83 | + bot.error("Couldn't get UID") |
| 84 | + |
| 85 | + # Return code of 0 |
| 86 | + else: |
| 87 | + bot.info('No instances found.') |
| 88 | + |
| 89 | + # If we are given a name, return just one |
| 90 | + if name is not None and len(instances) == 1: |
| 91 | + instances = instances[0] |
| 92 | + |
| 93 | + return instances |
| 94 | + |
0 commit comments