-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bar.py
executable file
·106 lines (78 loc) · 3.43 KB
/
Bar.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
#!/usr/local/bin/python3
from Vagrant import Vagrant
import rumps
from apscheduler.schedulers.background import BackgroundScheduler
# Callbacks
def cb_vm_destroy(sender):
sender.vm.destroy()
def cb_vm_power_off(sender):
sender.vm.poweroff()
def cb_vm_power_on(sender):
sender.vm.poweron()
def cb_vm_provision(sender):
sender.vm.provision()
def cb_vm_reset(sender):
sender.vm.reset()
def cb_vm_ssh(sender):
sender.vm.ssh()
def cb_vm_web(sender):
sender.vm.web()
class VagrantBar(rumps.App):
vagrant = None # Store our vagrant instance
def __init__(self):
super(VagrantBar, self).__init__(name="VB", icon="assets/icon.png")
self.vagrant = Vagrant()
self.menu = []
self.build_menu()
self.menu.add('')
# Here we update the menu in the background:
sched = BackgroundScheduler()
def scheduled_updater():
self.update_menu()
sched.add_job(scheduled_updater, 'interval', seconds=5)
sched.start()
def build_menu(self):
for name,vm in self.vagrant.get_vms():
if name not in self.menu:
submenu = rumps.MenuItem(name)
if vm.state == Vagrant.STATE_POWERON:
submenu.state = 1
submenu.add(self.create_menu_item("Power Off", vm, cb_vm_power_off))
submenu.add(self.create_menu_item("Reset", vm, cb_vm_reset))
submenu.add(self.create_menu_item("Reprovision", vm, cb_vm_provision))
submenu.add('')
submenu.add(self.create_menu_item("Open SSH Console", vm, cb_vm_ssh))
submenu.add(self.create_menu_item("Open in Browser", vm, cb_vm_web))
else:
submenu.state = 0
submenu.add(self.create_menu_item("Power On", vm, cb_vm_power_on))
submenu.add('')
submenu.add(self.create_menu_item("Destroy", vm, cb_vm_destroy))
self.menu.add(submenu)
else:
item = self.menu[name]
if item.state == 0 and vm.state == Vagrant.STATE_POWERON or item.state == 1 and vm.state == Vagrant.STATE_POWEROFF:
# Update here!
item.clear()
if vm.state == Vagrant.STATE_POWERON:
item.state = 1
item.add(self.create_menu_item("Power Off", vm, cb_vm_power_off))
item.add(self.create_menu_item("Reset", vm, cb_vm_reset))
item.add(self.create_menu_item("Reprovision", vm, cb_vm_provision))
item.add('')
item.add(self.create_menu_item("Open SSH Console", vm, cb_vm_ssh))
item.add(self.create_menu_item("Open in Browser", vm, cb_vm_web))
else:
item.state = 0
item.add(self.create_menu_item("Power On", vm, cb_vm_power_on))
item.add('')
item.add(self.create_menu_item("Destroy", vm, cb_vm_destroy))
def update_menu(self):
self.vagrant.update_vms()
self.build_menu()
def create_menu_item(self, title, vm, callback):
item = rumps.MenuItem(title, callback=callback)
item.vm = vm
return item
if __name__ == "__main__":
VagrantBar().run()