added generate comon url method
[sfa.git] / cmdline / getNodes.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import traceback
5 import urllib
6 from datetime import datetime
7 from optparse import OptionParser
8 from geni.util.rspec import Rspec
9
10 sfi_dir = os.path.expanduser("~/.sfi/")
11
12 def create_parser():
13     command = sys.argv[0]
14     argv = sys.argv[1:]
15     usage = "%(command)s [options]" % locals()
16     description = """getNodes will query comon and generate a list of nodes 
17 (plain or rspec) that meet the specified crieteria. If no criteria is 
18 specified, the default action is to return node comon considers 'alive' 
19 (resptime > 0)"""
20     cmp_options = ['rwfs', 'uptime', 'loads', 'meminfo', 'kernver', 'cpuspeed', 'txrate', 'rxrate', 'numslices', 'liveslices']
21     option = ['numslices', 'liveslices', 'gbfree'] 
22
23     parser = OptionParser(usage=usage,description=description)
24     for opt in options:
25         parser.add_option("--%s" % opt, dest="%s" % opt, action="store_true", 
26                           help = "available options [%s]" % ",".join(cmp_options))
27    
28     return parser    
29
30
31 def download_file(url, localFile):
32     webFile = urllib.urlopen(url)
33     localFile = open(localFile, 'w')
34     localFile.write(webFile.read())
35     localFile.close()    
36     
37
38 def generate_comon_url(options):
39     url = "select = 'resptime > 0"
40     query_dict = {}
41     query_dict['numslices'] = 'numslices %s= %s'
42     query_dict['liveslices'] = 'liveslices %s= %s'
43     query_dict['gbfree'] = 'gbfree %s= %s'
44     
45     if options.numslices:
46         full_value = options.numslices
47          
48     url += "'"
49
50 def get_comon_data():
51     date = datetime.now()
52     year = str(date.year)
53     month = str(date.month)
54     day = str(date.day)
55     if len(month) == 1:
56         month = "0" + month
57     if len(day) == 1:
58         day = "0" + day
59      
60     comon_data_filename = sfi_dir + os.sep + "comon_data.dat" 
61     comon_url = "http://comon.cs.princeton.edu/status/dump_comon_%s%s%s" % (year, month, day)
62     
63     print "storing comon data from %s in %s" % (comon_url, comon_data_filename)     
64     download_file(comon_url, comon_data_filename)
65
66     return comon_data_filename
67         
68 def main():
69     parser = create_parser()
70     (options, args) = parser.parse_args()
71     comon_file = get_comon_data()
72     
73
74 if __name__ == '__main__':
75     main()