import datetime import sys import time import requests import json from collections import deque from json import JSONEncoder headParams = {'User-Agent': 'Mozilla/5.0', 'RX_USEBASICAUTH':'true'} headPutParams ={'User-Agent': 'Mozilla/5.0', 'RX_USEBASICAUTH':'true', 'Content-Type': 'application/json', 'Accept':'*/*'} user="Admin" pwd="demo" ###Publish Site reads config file in plain text with the following format: #Publish type(full, incremental, single-page publish now) #Site name #Publishing Server name #Page ID (if scheduling a publish now) #### def publishSite(server ="http://localhost:9992", site ="", publishingServer = "", pubType = "f", pageId =""): if(pubType.lower() == "f"): url = server + "/Rhythmyx/services/sitemanage/publish/" + site +"/" + publishingServer elif(pubType.lower() == "i"): url = server + "/Rhythmyx/services/sitemanage/publish/incremental/publish/" + site + "/" + publishingServer else: url = server + "/Rhythmyx/services/sitemanage/publish/page/" + pageId print url response = requests.get(url, headers = headParams, auth=(user,pwd)) print response.text return response def getCurrentPublishingStatus(server ="http://localhost:9992"): url = server + "/Rhythmyx/services/sitemanage/pubstatus/current" response = requests.get(url, headers = headParams, auth=(user,pwd)) return response def getCurrentTime(): ts = time.time() return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') def getTimeInSecondsWithAMPM(jobStartTime): time = jobStartTime time = time.split(" ") splitTimes = time[0].split(":") hour = int(splitTimes[0]) minutes = int(splitTimes[1]) seconds = int(splitTimes[2]) if(time[1] == "PM" and hour != 12): hour = hour + 12 comparableTime = (hour * 60) + minutes comparableTime = comparableTime * 60 return comparableTime def getTimeInSecondsFromCurrentTime(time): time = time.split(" ") splitTimes = time[1].split(":") hour = int(splitTimes[0]) minutes = int(splitTimes[1]) seconds = int(splitTimes[2]) comparableTime = (hour * 60) + minutes comparableTime = comparableTime * 60 return comparableTime def isInbetween(jobStartTime,startTime,currentTime): jobStartTime = getTimeInSecondsWithAMPM(jobStartTime) startTime = getTimeInSecondsFromCurrentTime(startTime) currentTime = getTimeInSecondsFromCurrentTime(currentTime) if startTime <= jobStartTime: return True return False def getLogs(startTime= "", server="http://localhost:9992"): data = {"SitePublishLogRequest":[{"siteId":"","maxcount":"6","days":"1","pubServerId":""}]} url = server + "/Rhythmyx/services/sitemanage/pubstatus/logs" logsResponse = requests.post(url, headers = headPutParams, data=json.dumps(data), auth=(user,pwd)) json_object = json.loads(logsResponse.text) relevantJobList = [] listOfJobs = json_object["SitePublishJob"] for job in listOfJobs: if(isInbetween(job["startTime"],startTime,getCurrentTime())): relevantJobList.append(job) return relevantJobList def schedulePublishingKickOff(configName, pollingTime = 30, server ="http://localhost:9992"): startTime = getCurrentTime() config = open(configName,'r') log = open("SchedulePublishing.log","a") log.write("\n\n\n*******************************************************************************\n") log.write("Log for schedule publishing\n") log.write("Scheduled Publishing with config file: " + configName +"\n") log.write("Kicked Off Job at : " + getCurrentTime() +"\n") listOfSites = deque([]) listOfServers = deque([]) listOfPubTypes = deque([]) listOfPages = deque([]) #Read Site and publishing Server log.write("Reading Configuration file...\n") for line in config: pubType = line pubType = pubType.replace('\n','') listOfPubTypes.append(pubType) site = config.next() site = site.replace('\n','') listOfSites.append(site) publishingServer = config.next() publishingServer = publishingServer.replace('\n','') listOfServers.append(publishingServer) if (pubType.lower() == "p"): pageId = config.next() pageId = pageId.replace('\n','') listOfPages.append(pageId) config.close() log.write("Finished Reading Configuration file\n") #Have Read everything in config into queues #look to see if jobs are publishing: response = getCurrentPublishingStatus() print "Checking for jobs running..." print "Current Jobs Response : " print response json_object = json.loads(response.text) siteResponse = json_object["SitePublishJob"] while len(listOfSites) > 0: #get next site and server to publish through site =listOfSites.popleft() publishingServer=listOfServers.popleft() pubType = listOfPubTypes.popleft() if pubType == "p": pageId = listOfPages.popleft() print "\n\nsite : " + str(site) print "server : " + str(publishingServer) #wait while jobs are publishing else publish job while len(siteResponse) == 1: print "Waiting for : " + str(pollingTime) + " seconds..." log.write("Waiting for : " + str(pollingTime) + " seconds...\n") time.sleep(float(pollingTime)) print "Pinging status again..." response = getCurrentPublishingStatus() json_object = json.loads(response.text) siteResponse = json_object["SitePublishJob"] #kick off the publish if pubType.lower() == "i": print "Incremental-Publishing " + str(site) + "..." log.write("Incremental-Publishing " + str(site) + " at " + getCurrentTime() +"\n") elif pubType.lower() == "f" : print "Full-Publishing " + str(site) + "..." log.write("Full-Publishing " + str(site) + " at " + getCurrentTime() +"\n") else: print "Running Publish Now: " if pubType.lower() != "p": publishResponse = publishSite(server,site,publishingServer,pubType) else: publishResponse = publishSite(server,site,publishingServer,pubType,pageId) logs = getLogs(startTime, server) log.write("\n\nFor Site " + str(site) + ": These jobs were completed from the start of the script until now: \n") for job in logs: log.write(json.dumps(job, indent = 4, sort_keys= True)) log.write("\n\n") print "Publishing response : " print publishResponse response = getCurrentPublishingStatus() json_object = json.loads(response.text) siteResponse = json_object["SitePublishJob"] while len(siteResponse) == 1: print "Waiting for Last site to finish publishing..." print "Waiting for : " + str(pollingTime) + " seconds..." log.write("Waiting for : " + str(pollingTime) + " seconds...\n") time.sleep(float(pollingTime)) print "Pinging status again..." response = getCurrentPublishingStatus() json_object = json.loads(response.text) siteResponse = json_object["SitePublishJob"] print "Finished Scheduled Publishing at " + getCurrentTime() + "\n" log.write("Finished Scheduled Publishing at " + getCurrentTime() + "\n") log.write("\n*******************************************************************************\n") #kick off the schedule publishing job with arguments: config file name, polling time, and Optional[server name and port] def main(argv=sys.argv): if(len(argv)<2): print "Please provide Config File Name, and polling time, and optionally provide servername" raise Exception("Incorrect Arguments") server = "http://localhost:9992" if(len(argv)==4): server = argv[3] schedulePublishingKickOff(argv[1],argv[2],server) if __name__ == "__main__": main()