Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/cwltool/docker_id.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author | shellac |
---|---|
date | Sat, 02 May 2020 07:14:21 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26e78fe6e8c4 |
---|---|
1 """Helper functions for docker.""" | |
2 from __future__ import absolute_import, print_function | |
3 | |
4 from typing import List, Optional, Tuple, cast # pylint: disable=unused-import | |
5 | |
6 from typing_extensions import Text # pylint: disable=unused-import | |
7 # move to a regular typing import when Python 3.3-3.6 is no longer supported | |
8 | |
9 from .utils import subprocess | |
10 | |
11 | |
12 def docker_vm_id(): # type: () -> Tuple[Optional[int], Optional[int]] | |
13 """ | |
14 Return the User ID and Group ID of the default docker user inside the VM. | |
15 | |
16 When a host is using boot2docker or docker-machine to run docker with | |
17 boot2docker.iso (As on Mac OS X), the UID that mounts the shared filesystem | |
18 inside the VirtualBox VM is likely different than the user's UID on the host. | |
19 :return: A tuple containing numeric User ID and Group ID of the docker account inside | |
20 the boot2docker VM | |
21 """ | |
22 if boot2docker_running(): | |
23 return boot2docker_id() | |
24 if docker_machine_running(): | |
25 return docker_machine_id() | |
26 return (None, None) | |
27 | |
28 | |
29 def check_output_and_strip(cmd): # type: (List[Text]) -> Optional[Text] | |
30 """ | |
31 Pass a command list to subprocess.check_output. | |
32 | |
33 Returning None if an expected exception is raised | |
34 :param cmd: The command to execute | |
35 :return: Stripped string output of the command, or None if error | |
36 """ | |
37 try: | |
38 result = subprocess.check_output(cmd, stderr=subprocess.STDOUT, universal_newlines=True) | |
39 return cast(str, result).strip() | |
40 except (OSError, subprocess.CalledProcessError, TypeError, AttributeError): | |
41 # OSError is raised if command doesn't exist | |
42 # CalledProcessError is raised if command returns nonzero | |
43 # AttributeError is raised if result cannot be strip()ped | |
44 return None | |
45 | |
46 | |
47 def docker_machine_name(): # type: () -> Optional[Text] | |
48 """ | |
49 Get the machine name of the active docker-machine machine. | |
50 | |
51 :return: Name of the active machine or None if error | |
52 """ | |
53 return check_output_and_strip(['docker-machine', 'active']) | |
54 | |
55 | |
56 def cmd_output_matches(check_cmd, expected_status): | |
57 # type: (List[Text], Text) -> bool | |
58 """ | |
59 Run a command and compares output to expected. | |
60 | |
61 :param check_cmd: Command list to execute | |
62 :param expected_status: Expected output, e.g. "Running" or "poweroff" | |
63 :return: Boolean value, indicating whether or not command result matched | |
64 """ | |
65 return check_output_and_strip(check_cmd) == expected_status | |
66 | |
67 | |
68 def boot2docker_running(): # type: () -> bool | |
69 """ | |
70 Check if boot2docker CLI reports that boot2docker vm is running. | |
71 | |
72 :return: True if vm is running, False otherwise | |
73 """ | |
74 return cmd_output_matches(['boot2docker', 'status'], 'running') | |
75 | |
76 | |
77 def docker_machine_running(): # type: () -> bool | |
78 """ | |
79 Ask docker-machine for the active machine and checks if its VM is running. | |
80 | |
81 :return: True if vm is running, False otherwise | |
82 """ | |
83 machine_name = docker_machine_name() | |
84 if not machine_name: | |
85 return False | |
86 return cmd_output_matches(['docker-machine', 'status', machine_name], 'Running') | |
87 | |
88 | |
89 def cmd_output_to_int(cmd): # type: (List[Text]) -> Optional[int] | |
90 """ | |
91 Run the provided command and returns the integer value of the result. | |
92 | |
93 :param cmd: The command to run | |
94 :return: Integer value of result, or None if an error occurred | |
95 """ | |
96 result = check_output_and_strip(cmd) # may return None | |
97 if result is not None: | |
98 try: | |
99 return int(result) | |
100 except ValueError: | |
101 # ValueError is raised if int conversion fails | |
102 return None | |
103 return None | |
104 | |
105 | |
106 def boot2docker_id(): # type: () -> Tuple[Optional[int], Optional[int]] | |
107 """ | |
108 Get the UID and GID of the docker user inside a running boot2docker vm. | |
109 | |
110 :return: Tuple (UID, GID), or (None, None) if error (e.g. boot2docker not present or stopped) | |
111 """ | |
112 uid = cmd_output_to_int(['boot2docker', 'ssh', 'id', '-u']) | |
113 gid = cmd_output_to_int(['boot2docker', 'ssh', 'id', '-g']) | |
114 return (uid, gid) | |
115 | |
116 def docker_machine_id(): # type: () -> Tuple[Optional[int], Optional[int]] | |
117 """ | |
118 Ask docker-machine for active machine and gets the UID of the docker user. | |
119 | |
120 inside the vm | |
121 :return: tuple (UID, GID), or (None, None) if error (e.g. docker-machine not present or stopped) | |
122 """ | |
123 machine_name = docker_machine_name() | |
124 if not machine_name: | |
125 return (None, None) | |
126 uid = cmd_output_to_int(['docker-machine', 'ssh', machine_name, "id -u"]) | |
127 gid = cmd_output_to_int(['docker-machine', 'ssh', machine_name, "id -g"]) | |
128 return (uid, gid) | |
129 | |
130 | |
131 if __name__ == '__main__': | |
132 print(docker_vm_id()) |