-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrant.py
87 lines (65 loc) · 2.25 KB
/
Vagrant.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
import subprocess
import sys
class Vagrant():
SUPPORTED_PROVIDERS = ['virtualbox',]
STATE_POWERON = 'running'
STATE_POWEROFF = 'poweroff'
STATE_SUSPEND = 'suspend'
vms = None
def __init__(self):
self.update_vms()
def get_vms(self):
return self.vms.items()
def update_vms(self):
output = _cmd(['vagrant', 'global-status'])
self.vms = dict()
if output is not None:
lines = output.split('\n')
for line in lines:
line = line.split()
if len(line) >= 3:
if line[2] in self.SUPPORTED_PROVIDERS:
self.vms[line[1]] = VM(line[0], line[1], line[2], line[3], line[4])
class VM():
# These are mainly here for documentation
key = None
name = None
provider = None
state = None
path = None
WEB_PORTS = ['80', '8000', '8080']
def __init__(self, key, name, provider, state, path):
self.key = key
self.name = name
self.provider = provider
self.state = state
self.path = path
def poweroff(self):
_cmd_quiet(['vagrant', 'halt', self.key])
def poweron(self):
_cmd_quiet(['vagrant', 'up', self.key])
def reset(self):
_cmd_quiet(['vagrant', 'reload', self.key])
def provision(self):
_cmd_quiet(['vagrant', 'provision', self.key])
def destroy(self):
_cmd_quiet(['vagrant', 'destroy', '-f', self.key])
def ssh(self):
cmd = "tell application \"Terminal\" to do script \"vagrant ssh {}\"".format(self.key)
_cmd_quiet(['osascript', '-e', cmd])
def web(self):
output = _cmd(['vagrant', 'port', self.key])
if output is not None:
lines = output.split('\n')
for line in lines:
l = line.strip().split()
if len(l) > 4:
if l[0] in self.WEB_PORTS:
url = "http://127.0.0.1:{}".format(l[3])
_cmd_quiet(['open', url])
break
def _cmd(args):
return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0].decode("utf-8")
def _cmd_quiet(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
proc.comminicate()