-
Notifications
You must be signed in to change notification settings - Fork 12
/
dockerutils.py
184 lines (149 loc) · 5.37 KB
/
dockerutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sublime
import os, re, subprocess
import time, shutil
import os.path
opt_cleanup = '--rm'
# Used by logDockerCommand(command) below:
#SUBLIME_DOCKER_LOGFILE='/tmp/sublime-docker.log'
SUBLIME_DOCKER_LOGFILE=None
DOCKER_NOT_INSTALLED_LINUX_MSG='''Docker is not installed.
Install it to use SublimeDocker: open a Terminal and run
'curl -sSL https://get.docker.com/ | sh'
'''
DOCKER_NOT_INSTALLED_OSX_MSG='''Docker is not installed.
Install it to use SublimeDocker. Visit the following URL for installation instructions:
https://docs.docker.com/en/latest/installation/
'''
DOCKER_NOT_RUNNING_LINUX_MSG='''Docker engine is not running.
Start it to use SublimeDocker.
'''
DOCKER_NOT_RUNNING_OSX_MSG='''Docker engine is not running.
Start it to use SublimeDocker: open a Terminal and run 'docker-machine start $(docker-machine active)' or 'boot2docker up'
'''
def isDockerInstalled():
platform = sublime.platform()
if platform == 'linux':
return isDockerInstalledOnLinux()
if platform == 'osx':
return isDockerInstalledOnOSX()
def isDockerRunning():
platform = sublime.platform()
if platform == 'linux':
return isDockerRunningOnLinux()
if platform == 'osx':
return isDockerRunningOnOSX()
def isDockerRunningOnLinux():
""" Check is Docker daemon is running:
We assume that the path to the daemon which appears in full ps output
is of the form */bin/docker
"""
if len(os.popen("ps -aef | grep '/bin/docker ' | grep -v grep").read().strip()) > 0:
return True
if len(os.popen("ps -aef | grep '/bin/docker.io ' | grep -v grep").read().strip()) > 0:
return True
return False
def isDockerRunningOnOSX():
return (
(os.path.isfile('/usr/local/bin/boot2docker')
and isBoot2DockerRunning()) or
(os.path.isfile('/usr/local/bin/docker-machine')
and isDockerMachineRunning()))
def isBoot2DockerRunning():
if len(os.popen("ps -aef | grep 'boot2docker' | grep -v grep").read().strip()) < 1:
return False
try:
os.environ["DOCKER_HOST"]
os.environ["DOCKER_CERT_PATH"]
os.environ["DOCKER_TLS_VERIFY"]
except KeyError:
boot2docker_init_cmd = subprocess.check_output(["/usr/local/bin/boot2docker", "shellinit"], stderr=None).strip()
env = dict(re.findall(r'(\S+)=(".*?"|\S+)', boot2docker_init_cmd.decode()))
for key,value in env.items():
os.environ[key]=value
return True
def isDockerMachineRunning():
machine_ls = os.popen("/usr/local/bin/docker-machine ls -q").read().strip()
if len(machine_ls) < 1:
return False
try:
os.environ["DOCKER_HOST"]
os.environ["DOCKER_CERT_PATH"]
os.environ["DOCKER_TLS_VERIFY"]
os.environ["DOCKER_MACHINE_NAME"]
except KeyError:
setEnvVariables()
return True
def isDockerInstalledOnLinux():
if shutil.which('docker') != None :
return True
return False
def isDockerInstalledOnOSX():
if not os.path.isfile('/usr/local/bin/docker'):
print("which(docker) returned None")
return False
if not os.path.isfile('/usr/local/bin/boot2docker') and not os.path.isfile('/usr/local/bin/docker-machine'):
print("which(boot2docker and docker-machine) returned None")
return False
return True
def isNotRunningMessage():
platform = sublime.platform()
if platform == 'linux':
isNotRunningMessageLinux()
if platform == 'osx':
isNotRunningMessageOSX()
def isNotInstalledMessage():
platform = sublime.platform()
if platform == 'linux':
isNotInstalledMessageLinux()
if platform == 'osx':
isNotInstalledMessageOSX()
def isNotInstalledMessageLinux():
sublime.error_message(DOCKER_NOT_INSTALLED_LINUX_MSG)
def isNotInstalledMessageOSX():
sublime.error_message(DOCKER_NOT_INSTALLED_OSX_MSG)
def isNotRunningMessageLinux():
sublime.error_message(DOCKER_NOT_RUNNING_LINUX_MSG)
def isNotRunningMessageOSX():
sublime.error_message(DOCKER_NOT_RUNNING_OSX_MSG)
def isUnsupportedFileType(file_name):
return False
def getFileFullPath():
win = sublime.active_window()
if win:
view = win.active_view()
if view and view.file_name():
return view.file_name()
return ""
def getFileDir():
filefullpath = getFileFullPath()
dirname = os.path.dirname(filefullpath)
if os.path.exists(dirname):
return dirname
else:
return ""
def getFileName():
filefullpath = getFileFullPath()
return os.path.basename(filefullpath)
def getView():
win = sublime.active_window()
return win.active_view()
def getCommand():
platform = sublime.platform()
if platform == 'linux':
return "docker"
if platform == 'osx':
return "/usr/local/bin/docker"
def logDockerCommand(command):
if SUBLIME_DOCKER_LOGFILE != None:
with open(SUBLIME_DOCKER_LOGFILE, 'a+') as f:
f.write(time.strftime("\n%d/%m/%Y %H:%M:%S ") + str(command))
f.close()
def setEnvVariables():
env_out = run_machine_env()
vars = dict(re.findall(r'(\S+)=(".*?"|\S+)', env_out))
for key,value in vars.items():
os.environ[key]=value.strip('"')
def run_machine_env():
home_dir = os.path.expanduser('~')
cmd = "/usr/local/bin/docker-machine -s " + home_dir + "/.docker/machine env default"
return os.popen(cmd).read().strip()