Browse Source

Update to snapshotreports.py (#58)

* Add files via upload

* Add files via upload

* adding option to auto-remove transfer packages
master
Mike 6 years ago
committed by GitHub
parent
commit
d8a447afae
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 350
      snapshotreports.py

350
snapshotreports.py

@ -1,171 +1,179 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# snapshotreports.py # snapshotreports.py
# April 2019 # April 2019
# #
# this tool will export all the reports in your viya system to there own # this tool will export all the reports in your viya system to there own
# individual json file in a directory. # individual json file in a directory.
# #
# The purpose of the tools is to be able to have a granular backup of reports # The purpose of the tools is to be able to have a granular backup of reports
# so that you could restore an individual report to a system. Something that # so that you could restore an individual report to a system. Something that
# is not currently supported by Viya backup or promotion # is not currently supported by Viya backup or promotion
# #
# example # example
# #
# save each to their own package all reports that have changed in the last 10 days # save each to their own package all reports that have changed in the last 10 days
# snapshotreports.py -c 10 -d ~/snapshot # snapshotreports.py -c 10 -d ~/snapshot
# #
# Change History # Change History
# #
# 16may2020 add folder path to report name # 16may2020 add folder path to report name
# 16may2020 allow to subset reports exported by the path of the report folder # 16may2020 allow to subset reports exported by the path of the report folder
# # 10aug2020 add option to auto delete transport file after download completes
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. #
# # Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the License); #
# you may not use this file except in compliance with the License. # Licensed under the Apache License, Version 2.0 (the License);
# You may obtain a copy of the License at # you may not use this file except in compliance with the License.
# # You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 #
# # http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, # Unless required by applicable law or agreed to in writing, software
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# limitations under the License. # See the License for the specific language governing permissions and
# # limitations under the License.
#
# Import Python modules
import argparse, sys, subprocess, uuid, time, os, glob # Import Python modules
from datetime import datetime as dt, timedelta as td import argparse, sys, subprocess, uuid, time, os, glob
from sharedfunctions import getfolderid, callrestapi, getpath from datetime import datetime as dt, timedelta as td
from sharedfunctions import getfolderid, callrestapi, getpath
# get python version
version=int(str(sys.version_info[0])) # get python version
version=int(str(sys.version_info[0]))
# CHANGE THIS VARIABLE IF YOUR CLI IS IN A DIFFERENT LOCATION
clidir='/opt/sas/viya/home/bin/' # CHANGE THIS VARIABLE IF YOUR CLI IS IN A DIFFERENT LOCATION
clidir='/opt/sas/viya/home/bin/'
# get input parameters
parser = argparse.ArgumentParser(description="Export Viya Reports each to its own unique transfer package") # get input parameters
parser.add_argument("-d","--directory", help="Directory to store report packages",required='True') parser = argparse.ArgumentParser(description="Export Viya Reports each to its own unique transfer package")
parser.add_argument("-q","--quiet", help="Suppress the are you sure prompt.", action='store_true') parser.add_argument("-d","--directory", help="Directory to store report packages",required='True')
parser.add_argument("-c","--changeddays", help="Reports changed in the how many days (defaults to 1 day)?",default='1') parser.add_argument("-q","--quiet", help="Suppress the are you sure prompt.", action='store_true')
parser.add_argument("-m","--modifiedby", help="Last modified id equals?",default=None) parser.add_argument("-c","--changeddays", help="Reports changed in the how many days (defaults to 1 day)?",default='1')
parser.add_argument("-n","--name", help="Name contains?",default=None) parser.add_argument("-m","--modifiedby", help="Last modified id equals?",default=None)
parser.add_argument("-f","--folderpath", help="Folder Path starts with?",default="/") parser.add_argument("-n","--name", help="Name contains?",default=None)
parser.add_argument("-f","--folderpath", help="Folder Path starts with?",default="/")
args= parser.parse_args() parser.add_argument("-t","--tranferremove", help="Remove transfer file after download?", action='store_true')
basedir=args.directory
quietmode=args.quiet args= parser.parse_args()
basedir=args.directory
changeddays=args.changeddays quietmode=args.quiet
modby=args.modifiedby autotranferremove=args.tranferremove
nameval=args.name
folderpath=args.folderpath changeddays=args.changeddays
modby=args.modifiedby
# calculate time period for files nameval=args.name
now=dt.today()-td(days=int(changeddays)) folderpath=args.folderpath
subset_date=now.strftime("%Y-%m-%dT%H:%M:%S")
datefilter="ge(modifiedTimeStamp,"+subset_date+")" # calculate time period for files
now=dt.today()-td(days=int(changeddays))
# create a list for filter conditions subset_date=now.strftime("%Y-%m-%dT%H:%M:%S")
filtercond=[] datefilter="ge(modifiedTimeStamp,"+subset_date+")"
# there is always a number of days, the default is zero # create a list for filter conditions
filtercond.append(datefilter) filtercond=[]
if nameval!=None: filtercond.append('contains($primary,name,"'+nameval+'")') # there is always a number of days, the default is zero
if modby!=None: filtercond.append("eq(modifiedBy,"+modby+")") filtercond.append(datefilter)
# add the start and end and comma delimit the filter if nameval!=None: filtercond.append('contains($primary,name,"'+nameval+'")')
delimiter = ',' if modby!=None: filtercond.append("eq(modifiedBy,"+modby+")")
completefilter = 'and('+delimiter.join(filtercond)+')'
# add the start and end and comma delimit the filter
# prompt if directory exists because existing json files are deleted delimiter = ','
if os.path.exists(basedir): completefilter = 'and('+delimiter.join(filtercond)+')'
# if the quiet mode flag is not passed then prompt to continue # prompt if directory exists because existing json files are deleted
if not quietmode: if os.path.exists(basedir):
if version > 2: # if the quiet mode flag is not passed then prompt to continue
areyousure=input("The folder exists any existing json files in it will be deleted. Continue? (Y)") if not quietmode:
else:
areyousure=raw_input("The folder already exists any existing json files in it will be deleted. Continue? (Y)") if version > 2:
else: areyousure=input("The folder exists any existing json files in it will be deleted. Continue? (Y)")
areyousure="Y" else:
areyousure=raw_input("The folder already exists any existing json files in it will be deleted. Continue? (Y)")
else: areyousure="Y" else:
areyousure="Y"
# prompt is Y if user selected Y, its a new directory, or user selected quiet mode
if areyousure.upper() =='Y': else: areyousure="Y"
path=basedir # prompt is Y if user selected Y, its a new directory, or user selected quiet mode
if areyousure.upper() =='Y':
# create directory if it doesn't exist
if not os.path.exists(path): os.makedirs(path) path=basedir
else:
filelist=glob.glob(path+"/*.json") # create directory if it doesn't exist
for file in filelist: if not os.path.exists(path): os.makedirs(path)
os.remove(file) else:
filelist=glob.glob(path+"/*.json")
# retrieve all reports in the system for file in filelist:
reqtype='get' os.remove(file)
reqval='/reports/reports?filter='+completefilter+'&limit=10000'
# retrieve all reports in the system
resultdata=callrestapi(reqval,reqtype) reqtype='get'
reqval='/reports/reports?filter='+completefilter+'&limit=10000'
# loop root reports
if 'items' in resultdata: resultdata=callrestapi(reqval,reqtype)
total_items=resultdata['count'] # loop root reports
if 'items' in resultdata:
returned_items=len(resultdata['items'])
total_items=resultdata['count']
if total_items == 0: print("Note: No items returned.")
else: returned_items=len(resultdata['items'])
# export each folder and download the package file to the directory
if total_items == 0: print("Note: No items returned.")
reports_exported=0 else:
# export each folder and download the package file to the directory
for i in range(0,returned_items):
reports_exported=0
id=resultdata['items'][i]["id"]
for i in range(0,returned_items):
path_to_report=getpath("/reports/reports/"+id)
id=resultdata['items'][i]["id"]
if path_to_report.startswith(folderpath):
path_to_report=getpath("/reports/reports/"+id)
reports_exported=reports_exported+1
if path_to_report.startswith(folderpath):
path_to_report=path_to_report.replace("/","_")
reports_exported=reports_exported+1
package_name=str(uuid.uuid1())
json_name=path_to_report+resultdata['items'][i]["name"].replace(" ","")+'_'+str(i) path_to_report=path_to_report.replace("/","_")
json_name=json_name.replace("(","_") package_name=str(uuid.uuid1())
json_name=json_name.replace(")","_") json_name=path_to_report+resultdata['items'][i]["name"].replace(" ","")+'_'+str(i)
json_name=json_name.replace(" ","-")
json_name=json_name.replace("(","_")
command=clidir+'sas-admin transfer export -u /reports/reports/'+id+' --name "'+package_name+'"' json_name=json_name.replace(")","_")
print(command) json_name=json_name.replace(" ","-")
subprocess.call(command, shell=True)
command=clidir+'sas-admin transfer export -u /reports/reports/'+id+' --name "'+package_name+'"'
reqval='/transfer/packages?filter=eq(name,"'+package_name+'")' print(command)
package_info=callrestapi(reqval,reqtype) subprocess.call(command, shell=True)
package_id=package_info['items'][0]['id'] reqval='/transfer/packages?filter=eq(name,"'+package_name+'")'
package_info=callrestapi(reqval,reqtype)
completefile=os.path.join(path,json_name+'.json')
command=clidir+'sas-admin transfer download --file '+completefile+' --id '+package_id package_id=package_info['items'][0]['id']
print(command)
subprocess.call(command, shell=True) completefile=os.path.join(path,json_name+'.json')
command=clidir+'sas-admin transfer download --file '+completefile+' --id '+package_id
print("NOTE: "+str(reports_exported)+" Viya report(s) exported to json files in "+path) print(command)
subprocess.call(command, shell=True)
#time.sleep(1)
else: if autotranferremove:
print("NOTE: Operation cancelled") print(clidir+'sas-admin transfer delete --id '+package_id+"\n")
remTransferObject = subprocess.Popen(clidir+'sas-admin transfer delete --id '+package_id, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
remTransferObjectOutput = remTransferObject.communicate(b'Y\n')
remTransferObject.wait()
print("NOTE: "+str(reports_exported)+" Viya report(s) exported to json files in "+path)
else:
print("NOTE: Operation cancelled")
Loading…
Cancel
Save