Subversion Repositories DevTools

Rev

Rev 5795 | Rev 5809 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5795 Rev 5808
Line 3... Line 3...
3
# Auto-start and stop EC2 instances
3
# Auto-start and stop EC2 instances
4
#
4
#
5
import boto.ec2, datetime, sys
5
import boto.ec2, datetime, sys
6
from time import gmtime, strftime, strptime, sleep
6
from time import gmtime, strftime, strptime, sleep
7
 
7
 
8
#Default start stop times
8
#Default start stop times (7am / 7pm AWST)
9
DEFAULT_START_GMT = "99"
9
DEFAULT_START_GMT = "23"
10
DEFAULT_STOP_GMT = "10"
10
DEFAULT_STOP_GMT = "11"
11
 
11
 
12
#debug setting for dry-run
12
#debug setting for dry-run
13
#IS_DRY_RUN = True
13
#IS_DRY_RUN = True
14
IS_DRY_RUN = False
14
IS_DRY_RUN = False
15
 
15
 
Line 26... Line 26...
26
STATE_STOPPING = 64
26
STATE_STOPPING = 64
27
STATE_STOPPED = 80
27
STATE_STOPPED = 80
28
 
28
 
29
# This is special flag to never kill this instance. Should really only be used
29
# This is special flag to never kill this instance. Should really only be used
30
# on your scripting server instance.
30
# on your scripting server instance.
31
#NEVER_KILL = "NEVER_KILL_ME"
-
 
32
TAG_DATE_TIME_FORMAT = "%Y-%m-%d"
31
TAG_DATE_TIME_FORMAT = "%Y-%m-%d"
33
DEFAULT_NO_BOOT_TIME = "2030-01-01"
32
DEFAULT_NO_BOOT_TIME = "2000-01-01"
34
DEFAULT_NO_KILL_TIME = "2000-12-31"
33
DEFAULT_NO_KILL_TIME = "2000-01-01"
35
 
34
 
36
TODAY_HOUR_DT_FORMAT = '%Y-%m-%d %H'
35
TODAY_HOUR_DT_FORMAT = '%Y-%m-%d %H'
37
DEFAULT_AUTO_TIME = '2000-01-01 00'
36
DEFAULT_AUTO_TIME = '2000-01-01 00'
38
 
37
 
39
TODAY_WEEKDAY_NAME = '%a'
38
TODAY_WEEKDAY_NAME = '%a'
40
DEFAULT_WEEKDAY_ACTION = 'MTWHF__'
39
DEFAULT_WEEKDAY_ACTION = 'MTWHFSU'
41
 
40
 
42
def main():
41
def main():
43
    gmtime_now = gmtime()
42
    gmtime_now = gmtime()
44
 
43
 
45
    #Test cases
44
    #Test cases
Line 91... Line 90...
91
        last_auto_start_time = getTimeFromInstance(instance, 'LAST_AUTO_START', DEFAULT_AUTO_TIME, TODAY_HOUR_DT_FORMAT) 
90
        last_auto_start_time = getTimeFromInstance(instance, 'LAST_AUTO_START', DEFAULT_AUTO_TIME, TODAY_HOUR_DT_FORMAT) 
92
        last_auto_stop_time = getTimeFromInstance(instance, 'LAST_AUTO_STOP', DEFAULT_AUTO_TIME, TODAY_HOUR_DT_FORMAT)
91
        last_auto_stop_time = getTimeFromInstance(instance, 'LAST_AUTO_STOP', DEFAULT_AUTO_TIME, TODAY_HOUR_DT_FORMAT)
93
 
92
 
94
        action_taken = "Do Nothing"
93
        action_taken = "Do Nothing"
95
        try: 
94
        try: 
96
            no_boot_kill_action_taken =  "(boot:" \
-
 
97
                                      + strftime(TAG_DATE_TIME_FORMAT, no_boot_until_time) + ") (kill:" \
-
 
98
                                      + strftime(TAG_DATE_TIME_FORMAT,no_kill_until_time) + ")" 
-
 
99
 
-
 
100
            if ( int(start_hour_gmt) == int(stop_hour_gmt) ):
95
            if ( int(start_hour_gmt) == int(stop_hour_gmt) ):
101
                action_taken = "Not modifying - Start (" + start_hour_gmt + ") and stop (" \
96
                action_taken = "Not modifying - Start (" + start_hour_gmt + ") and stop (" \
102
                             + stop_hour_gmt + ") are same hour."
97
                             + stop_hour_gmt + ") are same hour."
103
            elif ( isNoActionDay(action_days, this_day_name) ):
98
            elif ( isNoActionDay(action_days, this_day_name) ):
104
                action_taken = "Not modifying non actionable day (" + action_days + ") " + this_day_name
99
                action_taken = "Not modifying - Non actionable day (" + action_days + ") " + this_day_name
105
            else:
100
            else:
106
                total_msg = ""
101
                total_msg = ""
107
 
102
 
108
                if ( this_hour_time <= no_boot_until_time ):
103
                if ( this_hour_time <= no_boot_until_time ):
109
                    total_msg += no_boot_kill_action_taken + " no boot until "
104
                    total_msg += "No boot until " + strftime(TAG_DATE_TIME_FORMAT, no_boot_until_time)  
110
                elif ( int(hh) == int(start_hour_gmt) and last_auto_start_time != this_hour_time ):
105
                elif ( int(hh) == int(start_hour_gmt) and last_auto_start_time != this_hour_time ):
111
                    total_msg += processStart( conn, instance, state_code, this_hour_str )
106
                    total_msg += processStart( conn, instance, state_code, this_hour_str )
112
                else:
107
                else:
113
                    total_msg += "No boot action"
108
                    total_msg += "No boot action"
114
           
109
           
-
 
110
                total_msg += " - "
-
 
111
 
115
                if ( this_hour_time <= no_kill_until_time ):
112
                if ( this_hour_time <= no_kill_until_time ):
116
                    total_msg += no_boot_kill_action_taken + " no kill until "
113
                    total_msg += "No kill until " + strftime(TAG_DATE_TIME_FORMAT, no_kill_until_time)
117
                elif( int(hh) == int(stop_hour_gmt) and last_auto_stop_time != this_hour_time ):
114
                elif( int(hh) == int(stop_hour_gmt) and last_auto_stop_time != this_hour_time ):
118
                    total_msg += processStop( conn, instance, state_code, this_hour_str )
115
                    total_msg += processStop( conn, instance, state_code, this_hour_str )
119
                else:
116
                else:
120
                    total_msg += " - No kill action"
117
                    total_msg += "No kill action"
121
        
118
        
122
                if ( total_msg != "" ):
119
                if ( total_msg != "" ):
123
                    action_taken = total_msg
120
                    action_taken = total_msg
124
 
121
 
125
        except:
122
        except:
126
            action_taken = "Unknown Exception"
123
            action_taken = "Unknown Exception"
127
    
124
    
128
        print this_hour_str, ':',  instance_id, state_code, start_hour_gmt, stop_hour_gmt, " --- ", action_taken, "for", name
125
        print this_hour_str, ':',  instance_id, state_code, start_hour_gmt, stop_hour_gmt, " --- ", action_taken, "for:", name
129
   
126
   
130
 
127
 
131
 
128
 
132
def getSetTag(instance, tag, default_value):
129
def getSetTag(instance, tag, default_value):
133
    #Attempts to retrieve the the tag from the instance.
130
    #Attempts to retrieve the the tag from the instance.