22
15
from txaws.credentials import AWSCredentials
26
class AWSStatusIndicator(object):
21
class AWSStatusIcon(gtk.StatusIcon):
22
"""A status icon shown when instances are running."""
27
24
def __init__(self, reactor):
28
# Even though we have appindicator, we may still need the status
29
# icon because we're on something that does not show them.
30
self.status_icon = gtk.StatusIcon()
31
self.status_icon.set_from_stock(gtk.STOCK_NETWORK)
32
self.status_icon.set_visible(True)
33
self.status_icon.connect("activate", self.on_activate)
35
self.indicator = appindicator.Indicator("aws-status","stock_weather-cloudy",appindicator.CATEGORY_OTHER)
36
self.indicator.set_status(appindicator.STATUS_PASSIVE)
25
from txaws.service import AWSServiceRegion
26
gtk.StatusIcon.__init__(self)
27
self.set_from_stock(gtk.STOCK_NETWORK)
28
self.set_visible(True)
37
29
self.reactor = reactor
30
self.connect("activate", self.on_activate)
38
31
self.probing = False
39
32
# Nested import because otherwise we get "reactor already installed".
40
33
self.password_dialog = None
43
35
creds = AWSCredentials()
45
37
creds = self.from_gnomekeyring()
46
if self.region is None:
47
self.set_region(creds)
38
self.region = AWSServiceRegion(creds)
48
39
self.create_client(creds)
51
42
<menubar name="Menubar">
52
43
<menu action="Menu">
53
<menuitem action="Refresh"/>
54
44
<menuitem action="Stop instances"/>
55
<menuitem action="Quit"/>
61
50
("Menu", None, "Menu"),
62
("Refresh", gtk.STOCK_REFRESH, "_Refresh...", None,
63
"Refresh", self.on_activate),
64
51
("Stop instances", gtk.STOCK_STOP, "_Stop instances...", None,
65
52
"Stop instances", self.on_stop_instances),
66
("Quit", gtk.STOCK_QUIT, "_Quit...", None,
67
"Quit", self.on_quit),
69
54
ag = gtk.ActionGroup("Actions")
70
55
ag.add_actions(actions)
73
58
self.manager.add_ui_from_string(menu)
74
59
self.menu = self.manager.get_widget(
75
60
"/Menubar/Menu/Stop instances").props.parent
76
self.status_icon.connect("popup-menu", self.on_popup_menu)
78
self.indicator.set_menu(self.menu)
80
self.on_activate(None)
83
def set_region(self, creds):
84
from txaws.service import AWSServiceRegion
85
self.region = AWSServiceRegion(creds)
61
self.connect("popup-menu", self.on_popup_menu)
87
63
def create_client(self, creds):
88
64
if creds is not None:
89
if self.region is None:
90
self.set_region(creds)
91
65
self.client = self.region.get_ec2_client()
92
66
self.on_activate(None)
146
120
deferred = self.client.describe_instances()
147
121
deferred.addCallbacks(self.showhide, self.describe_error)
149
def on_quit(self, data):
152
123
def on_popup_menu(self, status, button, time):
153
124
self.menu.popup(None, None, None, button, time)
158
129
deferred.addCallbacks(self.shutdown_instances, self.show_error)
160
131
def save_key(self, response_id, data):
133
dialog = self.password_dialog
162
135
if data != gtk.RESPONSE_ACCEPT:
163
136
# User cancelled. They can ask for the password again somehow.
183
156
for instance in reservation:
184
157
if instance.instance_state == "running":
187
self.status_icon.set_visible(False)
188
if have_appindicator:
189
self.indicator.set_label("")
190
self.indicator.set_status(appindicator.STATUS_PASSIVE)
196
self.status_icon.set_tooltip(
197
"AWS Status - %d %s" % (active,word))
198
self.status_icon.set_visible(True)
199
if have_appindicator:
200
self.indicator.set_label("%d %s" % (active,word), "10 instances")
201
self.indicator.set_status(appindicator.STATUS_ACTIVE)
159
self.set_tooltip("AWS Status - %d instances" % active)
160
self.set_visible(active != 0)
202
161
self.queue_check()
204
163
def shutdown_instances(self, reservation):
209
168
def queue_check(self):
210
169
self.probing = False
211
self.reactor.callLater(29, self.on_activate, None)
170
self.reactor.callLater(60, self.on_activate, None)
213
172
def show_error(self, error):
214
173
# debugging output for now.
245
204
from twisted.internet import gtk2reactor
246
205
gtk2reactor.install()
247
206
from twisted.internet import reactor
249
AWSStatusIndicator(reactor)
250
gobject.set_application_name("aws-status")
253
# In this case, the user cancelled, and the exception bubbled to here.
207
status = AWSStatusIcon(reactor)
208
gobject.set_application_name("aws-status")