Skip to content

Commit

Permalink
New DayOfMonth tag supported.
Browse files Browse the repository at this point in the history
Hopefully fixed timezone issues and created seperate file to maintain regions and their timezones.
  • Loading branch information
AnykeyNL committed Mar 13, 2022
1 parent 31d87d4 commit 0dd11c9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 77 deletions.
109 changes: 33 additions & 76 deletions AutoScaleALL.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/home/opc/py36env/bin/python
#################################################################################################################
# OCI - Scheduled Auto Scaling Script
# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
# This software is licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl
#
# Written by: Richard Garsthagen
Expand Down Expand Up @@ -33,12 +33,14 @@
import sys
import argparse
import os
import Regions

# You can modify / translate the tag names used by this script - case sensitive!!!
AnyDay = "AnyDay"
Weekend = "Weekend"
WeekDay = "WeekDay"
Version = "2022.02.03"
DayOfMonth = "DayOfMonth"
Version = "2022.03.13"

# ============== CONFIGURE THIS SECTION ======================
# OCI Configuration
Expand Down Expand Up @@ -73,90 +75,29 @@ def print_header(name):
# Get Current Hour per the region
##########################################################################
def get_current_hour(region, ignore_region_time=False):
if region[:2] == 'eu':
timezdiff = 2
elif region[:2] == 'uk':
timezdiff = 0
elif region == 'af-johannesburg-1':
timezdiff = 2
elif region == 'ap-chiyoda-1':
timezdiff = 9
elif region == 'ap-chuncheon-1':
timezdiff = 9
elif region == 'ap-hyderabad-1':
timezdiff = 5.5
elif region == 'ap-melbourne-1':
timezdiff = 10
elif region == 'ap-mumbai-1':
timezdiff = 5.5
elif region == 'ap-osaka-1':
timezdiff = 9
elif region == 'ap-seoul-1':
timezdiff = 9
elif region == 'ap-singapore-1':
timezdiff = 8
elif region == 'ap-sydney-1':
timezdiff = 10
elif region == 'ap-tokyo-1':
timezdiff = 9
elif region == 'ca-montreal-1':
timezdiff = -4
elif region == 'ca-toronto-1':
timezdiff = -4
elif region == 'il-jerusalem-1':
timezdiff = 3
elif region == 'me-abudhabi-1':
timezdiff = 4
elif region == 'me-dubai-1':
timezdiff = 4
elif region == 'me-jeddah-1':
timezdiff = 3
elif region == 'sa-santiago-1':
timezdiff = -4
elif region == 'sa-saopaulo-1':
timezdiff = -3
elif region == 'sa-vinhedo-1':
timezdiff = -3
elif region == 'us-ashburn-1':
timezdiff = -4
elif region == 'us-gov-ashburn-1':
timezdiff = -4
elif region == 'us-gov-chicago-1':
timezdiff = -5
elif region == 'us-gov-fortworth-1':
timezdiff = -5
elif region == 'us-gov-fortworth-2':
timezdiff = -5
elif region == 'us-gov-phoenix-1':
timezdiff = -7
elif region == 'us-gov-sterling-1 ':
timezdiff = -4
elif region == 'us-gov-sterling-2':
timezdiff = -4
elif region == 'us-langley-1':
timezdiff = -5
elif region == 'us-luke-1':
timezdiff = -7
elif region == 'us-phoenix-1':
timezdiff = -7
elif region == 'us-sanjose-1':
timezdiff = -7
else:
timezdiff = 0

timezdiff = 0 # Default value if no region match is found

# Find matching time zone for region
for r in Regions.RegionTime:
if r[0] == region:
timezdiff = r[1]

# Get current host time
current_time = current_host_time

# if need to use region time
if not ignore_region_time:
current_time = current_utc_time + datetime.timedelta(hours=timezdiff)
print ("Debug: {}".format(current_time))

# get the variables to return
iDayOfWeek = current_time.weekday() # Day of week as a number
iDay = calendar.day_name[iDayOfWeek] # Day of week as string
iCurrentHour = current_time.hour
iDayOfMonth = current_time.date().day # Day of the month as a number

return iDayOfWeek, iDay, iCurrentHour
return iDayOfWeek, iDay, iCurrentHour, iDayOfMonth


##########################################################################
Expand Down Expand Up @@ -495,17 +436,18 @@ def autoscale_region(region):
###############################################
# Get Current Day, time
###############################################
DayOfWeek, Day, CurrentHour = get_current_hour(region, cmd.ignore_region_time)
DayOfWeek, Day, CurrentHour, CurrentDayOfMonth = get_current_hour(region, cmd.ignore_region_time)

if AlternativeWeekend:
MakeLog("Using Alternative weekend (Friday and Saturday as weekend")
if cmd.ignore_region_time:
MakeLog("Ignoring Region Datetime, Using local time")

MakeLog("Day of week: {}, IsWeekday: {}, Current hour: {}".format(Day, isWeekDay(DayOfWeek), CurrentHour))
MakeLog("Day of week: {}, IsWeekday: {}, Current hour: {}, Current DayOfMonth: {}".format(Day, isWeekDay(DayOfWeek), CurrentHour, CurrentDayOfMonth))

# Investigatin BUG: temporary disabling below logic
# Array start with 0 so decrease CurrentHour with 1, if hour = 0 then 23
CurrentHour = 23 if CurrentHour == 0 else CurrentHour - 1
#CurrentHour = 23 if CurrentHour == 0 else CurrentHour - 1

###############################################
# Find all resources with a Schedule Tag
Expand Down Expand Up @@ -637,6 +579,14 @@ def autoscale_region(region):
schedule = resourceDetails.defined_tags[PredefinedTag]
ActiveSchedule = ""

# Checking the right schedule based on priority
# from low to high:
#
# - Anyday
# - WeekDay or Weekend
# - Name of Day (Monday, Tuesday....)
# - Day of month

if AnyDay in schedule:
ActiveSchedule = schedule[AnyDay]
if isWeekDay(DayOfWeek): # check for weekday / weekend
Expand All @@ -649,6 +599,13 @@ def autoscale_region(region):
if Day in schedule: # Check for day specific tag (today)
ActiveSchedule = schedule[Day]

if DayOfMonth in schedule:
specificDays = schedule[DayOfMonth].split(",")
for specificDay in specificDays:
day, schedulesize = specificDay.split(":")
if int(day) == CurrentDayOfMonth:
ActiveSchedule = ("{},".format(schedulesize)*24)[:-1]

#################################################################
# Check if the active schedule contains exactly 24 numbers for each hour of the day
#################################################################
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Welcome to the Scheduled Auto Scaling Script for OCI (Oracle Cloud Infrastructur
The **AutoScaleALL** script: A single Auto Scaling script for all OCI resources that support scaling up/down and power on/off operations.

# NEW
- Support for a Day of the Month Schedule (Like 1st of the month or 15th of the month)
- Support running on all regions
- Added flags as parameters for execution:

Expand Down Expand Up @@ -80,14 +81,27 @@ and 1 minute after the hour scaling up/power on operations.
To control what to scale up/down or power on/off, you need to create a predefined tag called **Schedule**. If you want to
localize this, that is possible in the script. For the predefined tag, you need entries for the days of the week, weekdays, weekends and anyday. The tags names are case sensitive!

A single resource can contain multiple tags. A Weekend/Weekday tag overrules an AnyDay tag. A specific day of the week tag (ie. Monday) overrules all other tags.
A single resource can contain multiple tags. The priority of tags is as followed (from low to high)
- Anyday
- Weekday or Weekend
- Day of the week (Like Monday, Tuesday...)
- Day of the month (Example 1 = 1st or 15 = 15th of the month)

### Values for the AnyDay, Weekday, Weekend and Day of week tags:
The value of the tag needs to contain 24 numbers and/or wildcards (*) (else it is ignored), separated by commas. If the value is 0 it will power off the resource (if that is supported for that resource). Any number higher then 0 will re-scale the resource to that number. If the resource is powered off, it first will power-on the resource and then scale to the correct size.

When a wild card is used, the service will stay unmodified for that hour. For example, the below schedule will turn of a compute instance in the evening/night, but allows the user to manage the state during the day.

Schedule.AnyDay : 0,0,0,0,0,0,0,0,\*,\*,\*,\*,\*,\*,\*,\*,0,0,0,0,0,0,0,0

### Values for DayOfMonth tags
The DayOfMonth tag allows to set a resource on a specific day of the month. This can be specified by day:size

For each day <b>Only one value</b> can be specified! This value is valid for all hours of that day

The below example tag schedules the resource on the 1st of the month to 4, on the 3rd of the month to 2 and on the 28th of the month back to 5:

Schedule.DayOfMonth : 1:4,3:2,28:4

![Scaling Example Instance Pool](http://oc-blog.com/wp-content/uploads/2019/06/ScaleExamplePool.png)

Expand Down
41 changes: 41 additions & 0 deletions Regions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# OCI Regions according to:
# https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm

RegionTime = []
RegionTime.append(["ap-sydney-1", 10])
RegionTime.append(["ap-melbourne-1", 10])
RegionTime.append(["sa-saopaulo-1", -3])
RegionTime.append(["sa-vinhedo-1", -3])
RegionTime.append(["ca-montreal-1", -4])
RegionTime.append(["ca-toronto-1", -4])
RegionTime.append(["sa-santiago-1", -3])
RegionTime.append(["eu-marseille-1", 1])
RegionTime.append(["eu-frankfurt-1", 1])
RegionTime.append(["ap-hyderabad-1", 5.5])
RegionTime.append(["ap-mumbai-1", 5.5])
RegionTime.append(["il-jerusalem-1", 2])
RegionTime.append(["eu-milan-1", 1])
RegionTime.append(["ap-osaka-1", 9])
RegionTime.append(["ap-tokyo-1", 9])
RegionTime.append(["eu-amsterdam-1", 1])
RegionTime.append(["me-jeddah-1", 3])
RegionTime.append(["ap-singapore-1", 8])
RegionTime.append(["af-johannesburg-1", 2])
RegionTime.append(["ap-seoul-1", 9])
RegionTime.append(["ap-chuncheon-1", 9])
RegionTime.append(["eu-stockholm-1", 1])
RegionTime.append(["eu-zurich-1", 1])
RegionTime.append(["me-abudhabi-1", 4])
RegionTime.append(["me-dubai-1", 4])
RegionTime.append(["uk-london-1", 0])
RegionTime.append(["uk-cardiff-1", 0])
RegionTime.append(["us-ashburn-1", -4])
RegionTime.append(["us-phoenix-1", -7])
RegionTime.append(["us-sanjose-1", -7])
RegionTime.append(["us-langley-1", -5])
RegionTime.append(["us-luke-1", -7])
RegionTime.append(["us-gov-ashburn-1", -4])
RegionTime.append(["us-gov-chicago-1", -5])
RegionTime.append(["us-gov-phoenix-1", -7])
RegionTime.append(["uk-gov-london-1", 0])
RegionTime.append(["uk-gov-cardiff-1", 0])

0 comments on commit 0dd11c9

Please sign in to comment.