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