Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active December 27, 2015 10:19
Show Gist options
  • Save julianeon/7310764 to your computer and use it in GitHub Desktop.
Save julianeon/7310764 to your computer and use it in GitHub Desktop.
A tentative work in progress script to create schedules from the command line.
# Copyright (c) 2014, PagerDuty, Inc. <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of PagerDuty Inc nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'json'
require 'active_support/core_ext'
require 'date'
subdomain="insert-here"
api_key="insert-here"
endpoint="https://#{subdomain}.pagerduty.com/api/v1/schedules/"
def curl_command_post_schedules(token_string,schedule_json,endpoint)
curl_command="curl -H \"Content-type: application/json\" -H \"Authorization: Token token=#{token_string}\" -X POST \
-d #{schedule_json} #{endpoint}"
end
schedule_name="Weekday"
module LayerMaker
def self.make_layer_users(user_array)
user_string="\"users\": ["
user_array.each_with_index do |user_id,index|
user_string+="{"
user_string+="\"user\": {"
user_string+="\"id\": \"#{user_id}\""
user_string+="},"
user_string+="\"member_order\": #{index+1}"
user_string+="},"
end
user_string=user_string[0..-2]
user_string+="],"
end
def self.date_maker(layer_date)
date_length = layer_date.length
layer_date_fields=layer_date.split("/")
day=layer_date_fields[1].to_i
mon=layer_date_fields[0].to_i
time=Time.new
today_day = time.day
today_month = time.mon
if mon > today_month
yr="13"
elsif mon < today_month
yr="14"
elsif day < today_day
yr="14"
else
yr="13"
end
unless date_length > 15
date_unix="\"20#{yr}-#{mon}-#{day}T00:00:00\""
else
date_unix="\"#{layer_date}\""
end
end
def self.make_layer_json(hash={})
start=date_maker(hash[:layer_start])
stop=date_maker(hash[:layer_stop])
shift_begins = date_maker(hash[:shift_start])
hash[:shift_hour] ||= "00:00:00"
hash[:repetition] ||= "Daily"
hash[:rotation_length] ||= 86400
hash[:priority] ||= 1
hash[:layer_name] ||= " "
sched_block = "{\n"
sched_block += "\"start\": #{start},\n"
sched_block += "#{make_layer_users(hash[:users])}\n"
sched_block += "\"restriction_type\": \"#{hash[:repetition]}\",\n"
sched_block += "\"rotation_virtual_start\": #{shift_begins},\n"
sched_block += "\"rotation_turn_length_seconds\": #{hash[:rotation_length]},\n"
sched_block += "\"priority\": #{hash[:priority]},\n"
sched_block += "\"restrictions\": [\n"
sched_block += "{\n"
sched_block += "\"duration_seconds\": #{hash[:shift_length]},\n"
sched_block += "\"start_time_of_day\": \"#{hash[:shift_hour]}\",\n"
sched_block += "}\n"
sched_block += "],\n"
sched_block += "\"end\": #{stop},\n"
sched_block += "\"id\": null,\n"
week_start=hash[:layer_start]
week_stop=hash[:layer_stop]
chopper = lambda { |string| if string.length> 10 then string="#{string.split(":")[0][5..-1]}" else string end }
sched_block += "\"name\": \"#{chopper.call(week_start)} to #{chopper.call(week_stop)} #{hash[:layer_name]}\",\n"
sched_block += "}"
end
def self.dates_conventional_to_unix(layer_hash)
start_mon,start_day = layer_hash[:layer_start].split("/")
stop_mon,stop_day = layer_hash[:layer_stop].split("/")
layer_hash[:layer_start_year] ||= "2013"
start_year=layer_hash[:layer_start_year]
unixtime = lambda { |x,y,z| Time.local(x.to_i,y.to_i,z.to_i).to_time }
unixtime_extended = Proc.new do |start_date|
start_arr=start_date.split(":")
year,month,day=start_arr[0].split("-")
hour=start_arr[1].gsub(/[A-Z]/,"").to_i
min=start_arr[2].gsub(/[A-Z]/,"").to_i
sec=start_arr[3].gsub(/[A-Z]/,"").to_i
Time.local(year,month,day,hour,min,sec)
end
if layer_hash[:layer_start].length>10
start_here = layer_hash[:layer_start]
start_here= unixtime_extended.call(start_here)
else
start_here= unixtime.call(start_year,start_mon,start_day)
end
if layer_hash[:layer_stop].length>10
stop_here = layer_hash[:layer_stop]
stop_here= unixtime_extended.call(stop_here)
else
stop_here= unixtime.call(start_year,stop_mon,stop_day)
end
array_start_stop=Array.new
array_start_stop.push start_here
array_start_stop.push stop_here
end
def self.layers_json(layer_hash)
start_here,stop_here=dates_conventional_to_unix(layer_hash)
all_layers=""
index=0
weeks_in_year=52
weeks_in_year.times{
puts "We start and stop. #{start_here} #{stop_here}"
syear,smon,sday=start_here.to_s.split[0].split("-")
eyear,emon,eday=stop_here.to_s.split[0].split("-")
sdate="#{smon}/#{sday}"
edate="#{emon}/#{eday}"
sdate_unix="#{syear}-#{smon}-#{sday}:T00:00:00"
edate_unix="#{eyear}-#{emon}-#{eday}:T00:00:00"
layer_hash[:layer_start]=sdate_unix
layer_hash[:layer_stop]=edate_unix
index+=1
layer_hash[:layer_name]="Week #{index}"
layer_hash[:priority]=index
layer_text = LayerMaker.make_layer_json(layer_hash)
all_layers+=layer_text
all_layers+=",\n"
start_here += 7.days
stop_here += 7.days
}
all_layers
end
end
user_array=["P8MMFBJ","PKAZEXQ","PH1ODI0"]
layer_hash = {
layer_start: "11/11",
layer_stop: "11/16",
shift_start: "11/11",
shift_length: 28800,
shift_hour: "00:00:00",
users: user_array,
layer_name: "Weekday"
}
all_layers_json=LayerMaker.layers_json(layer_hash)
all_layers_json=all_layers_json[0..-2]
schedule = "'{
\"since\": \"2013-11-04T00:00:00\",
\"overflow\": 0,
\"until\": \"2013-12-08T00:00:00\",
\"schedule\": {
\"schedule_layers\": [
#{all_layers_json}
],
\"time_zone\": \"Pacific Time (US & Canada)\",
\"id\": null,
\"name\": \"#{schedule_name}\"
}
}' \ "
curl_string = curl_command_post_schedules(api_key,schedule,endpoint)
puts curl_string
IO.popen(curl_string).each do |line|
parsed=JSON.parse(line)
puts parsed
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment