comparison env/lib/python3.9/site-packages/cwltool/docker_id.py @ 0:4f3585e2f14b draft default tip

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