1 from datetime import datetime, timedelta
2
3 from starcluster import utils
4 from starcluster import static
5
6 from base import CmdBase
7
8
9 -class CmdSpotHistory(CmdBase):
10 """
11 spothistory [options] <instance_type>
12
13 Show spot instance pricing history stats (last 30 days by default)
14
15 Examples:
16
17 Show the current, max, and average spot price for m1.small instance type:
18
19 $ starcluster spothistory m1.small
20
21 Do the same but also plot the spot history over time using matplotlib:
22
23 $ starcluster spothistory -p m1.small
24 """
25 names = ['spothistory', 'shi']
26
27 - def addopts(self, parser):
28 now_tup = datetime.now()
29 now = utils.datetime_tuple_to_iso(now_tup)
30 delta = now_tup - timedelta(days=30)
31 thirty_days_ago = utils.datetime_tuple_to_iso(delta)
32 parser.add_option("-d", "--days", dest="days_ago",
33 action="store", type="float",
34 help="provide history in the last DAYS_AGO days "
35 "(overrides -s and -e options)")
36 parser.add_option("-s", "--start-time", dest="start_time",
37 action="store", type="string",
38 default=thirty_days_ago,
39 help="show price history after START_TIME "
40 "(e.g. 2010-01-15T22:22:22)")
41 parser.add_option("-e", "--end-time", dest="end_time",
42 action="store", type="string", default=now,
43 help="show price history up until END_TIME "
44 "(e.g. 2010-02-15T22:22:22)")
45 parser.add_option("-p", "--plot", dest="plot",
46 action="store_true", default=False,
47 help="plot spot history using matplotlib")
48
49 - def execute(self, args):
50 instance_types = ', '.join(static.INSTANCE_TYPES.keys())
51 if len(args) != 1:
52 self.parser.error(
53 'please provide an instance type (options: %s)' %
54 instance_types)
55 instance_type = args[0]
56 if not instance_type in static.INSTANCE_TYPES:
57 self.parser.error('invalid instance type. possible options: %s' %
58 ', '.join(static.INSTANCE_TYPES))
59 start = self.opts.start_time
60 end = self.opts.end_time
61 if self.opts.days_ago:
62 now = datetime.now()
63 end = utils.datetime_tuple_to_iso(now)
64 start = utils.datetime_tuple_to_iso(
65 now - timedelta(days=self.opts.days_ago))
66 browser_cmd = self.cfg.globals.get("web_browser")
67 self.ec2.get_spot_history(instance_type, start, end, self.opts.plot,
68 plot_web_browser=browser_cmd)
69