-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidget-check-vm-state.py
More file actions
105 lines (87 loc) · 4.14 KB
/
widget-check-vm-state.py
File metadata and controls
105 lines (87 loc) · 4.14 KB
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/bin/env python
# Python script for the Interoute Virtual Data Centre API:
# Name: widget-check-vm-state.py
# Purpose: GUI widget to display the states of the VMs in a VDC
# Requires: class VDCApiCall in the file vdc_api_call.py
# [https://gist.github.com/InterouteGIST/aff2f957b7f0d766fab9]
# Copyright (C) Interoute Communications Limited, 2014
# This program is used in the blog post:
# http://cloudstore.interoute.com/main/knowledge-centre/blog/vdc-api-programming-fun-part-04
# How to use (for Linux and Mac OS):
# (0) You must have Python version 2.6 or 2.7 installed in your machine
# (1) Create a configuration file '.vdcapi' for access to the VDC API according to the instructions at
# http://cloudstore.interoute.com/main/knowledge-centre/library/vdc-api-introduction-api
# (2) Put this file and the file vdc_api_call.py in any location
# (3) You can run this file using the command 'python widget-check-vm-state.py'
from Tkinter import *
import vdc_api_call as vdc
import json
import os
import datetime
class Application(Frame):
def vmStates_update(self):
self.vmStatesLabel["text"]=self.get_vm_info()
# VM information will update after 60000 millisecs = 1 minute
# ...set this value as you like
self.vmStatesLabel.after(60000, self.vmStates_update)
def get_vm_info(self):
#this method performs the API call 'listVirtualMachines'
request={}
checkTime = datetime.datetime.utcnow() # get the current time (UTC = GMT)
try:
result = self.api.listVirtualMachines(request)
testdict = result['virtualmachine'][0] #???this should throw exception if dictionary lookup fails
except:
return "***************************\nError: VM data not returned by API\n***************************"
vmresultstring=""
vmcounter=0
for vm in result['virtualmachine']:
vmcounter = vmcounter + 1
if vm['state'] == 'Running':
vmresultstring = vmresultstring + "\n [%2d] %s " % (vmcounter,vm['name'])
elif vm['state'] == 'Stopped':
vmresultstring = vmresultstring + "\n [%2d] %s (%s) " % (vmcounter, vm['name'],vm['state'])
else:
vmresultstring = vmresultstring + "\n [%2d] %s (%s) " % (vmcounter, vm['name'],vm['state'])
return "%d VMs in the account '%s'\nchecked at %s" % (result['count'],result['virtualmachine'][1]['account'],checkTime.strftime("%Y-%m-%d %H:%M:%S UTC")) + vmresultstring
def refresh_states(self):
#this method is called when the 'REFRESH' button is pressed
self.vmStatesLabel["text"]=self.get_vm_info()
def createWidgets(self):
self.vmStatesLabel = Label(self)
self.vmStatesLabel.pack({"side":"top"})
self.vmStatesLabel["font"]=('Courier','14')
self.vmStatesLabel["justify"]=LEFT
self.vmStatesLabel["text"]=self.get_vm_info()
# VM information will update after 60000 millisecs = 1 minute
# ...set this value as you like
self.vmStatesLabel.after(60000, self.vmStates_update)
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack({"side": "right"})
self.refresh = Button(self)
self.refresh["text"] = "REFRESH",
self.refresh["command"] = self.refresh_states
self.refresh.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
config_file = os.path.join(os.path.expanduser('~'), '.vdcapi')
if os.path.isfile(config_file):
with open(config_file) as fh:
data = fh.read()
config = json.loads(data)
api_url = config['api_url']
apiKey = config['api_key']
secret = config['api_secret']
# Create the api access object
self.api = vdc.VDCApiCall(api_url, apiKey, secret)
# INITIALISE THE GUI
self.pack()
self.createWidgets()
root = Tk()
root.title("VM State Widget")
app = Application(master=root)
app.mainloop()
root.destroy()