#!/bin/bash #================================================================================ # File: create_bookmark_template.sh # Type: bash-shell script # Date: 10-Apr 2020 # Author: v4- Mouhssie Saidi - Delphix Professional Services # v3- Carlos Cuellar - Delphix Professional Services # v2- Carlos Cuellar - Delphix Professional Services # v1- Diego Loureda - Delphix Professional Services # Ownership: This script is owned and maintained by the user, not by Delphix # # Licensed under the Apache License, Version 2.0 (the "License"); # 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 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Copyright (c) 2019 by Delphix. All rights reserved. # # Prerequisites: # # -Python 2 installed # -Python Modules OS, SYS, JSON, REQUESTS, TIME and LOGGING installed --> python -c "print( help('modules'))" # # Description: # # Script to be used as a PostSync hook on a dSource to create a new bookmark # on selfservice/jetstream templates where dSource is the Source. # #================================================================================ # # Please set the following variables to suit your purposes. # Set this to the Delphix admin user name # DELPHIX_ADMIN=admin # Set this to the password for the Delphix admin user DELPHIX_PASS=Delphix_123! #Days until bookmark expires DXBMKEXP=60 #Dsource name DSOURCE_NAME=asesrc1_ #Bookmark name to be created BNAME=snapsync-$(date +%Y-%m-%d-%H-%M-%S) #================================================================================= # Delphix Engine name DelphixEngine=`echo $SSH_CLIENT|awk '{ print $1}'` env > /tmp/environment.log echo $DelphixEngine > /tmp/engine.log echo $DSOURCE_NAME > /tmp/dsourcename.log #Below variables are fixed, no need to modify export BNAME export DSOURCE_NAME_CONTAINER export DSOURCE_NAME_TEMPLATE export DSOURCE_NAME_BRANCH export API_PATH cat << EOF > create_bm_self_temp.py #!/bin/python import sys import requests import json import time import os import logging import re def _setup_logger(): # This will log the time, level, filename, line number, and log message. log_message_format = '[%(asctime)s] [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s' log_message_date_format = '%Y-%m-%d %H:%M:%S' # Create a custom formatter. This will help with diagnosability. formatter = logging.Formatter(log_message_format, datefmt= log_message_date_format) platform_handler = logging.FileHandler('/tmp/create_dx_bookmark_template.log') platform_handler.setFormatter(formatter) logger = logging.getLogger() logger.addHandler(platform_handler) # By default the root logger's level is logging.WARNING. logger.setLevel(logging.INFO) # Setup the logger. _setup_logger() # logging.getLogger(__name__) is the convention way to get a logger in Python. # It returns a new logger per module and will be a child of the root logger. # Since we setup the root logger, nothing else needs to be done to set this # one up. logger = logging.getLogger(__name__) # # Below is an example of the repository discovery operation. # # NOTE: The decorators are defined on the 'plugin' object created above. # # Mark the function below as the operation that does repository discovery. from datetime import datetime, timedelta today = datetime.today() today_date = datetime.today().strftime('%Y-%m-%d %H:%M:%S') DMUSER=sys.argv[1] DMPASS=sys.argv[2] DX_ENGINE=sys.argv[3] DX_SOURCE=sys.argv[4] bmexp = today + timedelta(days=int(sys.argv[5])) bmexpiration = bmexp.strftime('%Y-%m-%dT%H:%M:%S.999Z') BASEURL='http://' + DX_ENGINE + '/resources/json/delphix' DXBOOKMARK='snapsync-' + str(bmexpiration) # # Request Headers ... # req_headers = { 'Content-Type': 'application/json' } # # Creating log... # log_msg = "Starting script to create bookmarks for all templates that include dSource " + DX_SOURCE + " and they will expire on " + str(bmexpiration) logger.info(log_msg) # # Python session, also handles the cookies ... # session = requests.session() # # Create session ... # formdata = '{ "type": "APISession", "version": { "type": "APIVersion", "major": 1, "minor": 10, "micro": 0 } }' r = session.post(BASEURL+'/session', data=formdata, headers=req_headers, allow_redirects=False) # # Login ... # formdata = '{ "type": "LoginRequest", "username": "' + DMUSER + '", "password": "' + DMPASS + '" }' r = session.post(BASEURL+'/login', data=formdata, headers=req_headers, allow_redirects=False) # #Get all API outputs from dsources, selfservice templates and Branches # dxsource = session.get(BASEURL+'/source', headers=req_headers, allow_redirects=False) dxsourceconfig = session.get(BASEURL+'/sourceconfig', headers=req_headers, allow_redirects=False) dxdatasource = session.get(BASEURL+'/selfservice/datasource', headers=req_headers, allow_redirects=False) dxbranch = session.get(BASEURL+'/selfservice/branch', headers=req_headers, allow_redirects=False) dxsystem = session.get(BASEURL+'/system', headers=req_headers, allow_redirects=False) dxsource_j = json.loads(dxsource.text) dxsourceconfig_j = json.loads(dxsourceconfig.text) dxdatasource_j = json.loads(dxdatasource.text) dxbranch_j = json.loads(dxbranch.text) dxsystem_j = json.loads(dxsystem.text) # Get all references for dbobj in dxsource_j['result']: if not dbobj['virtual']: if re.match(r'^ASE(Linked|Virtual)Source$', dbobj['type']): dxsourcename = dbobj['name'] if dxsourcename == DX_SOURCE: dxsource_ref = dbobj['reference'] dxcontainer_ref = dbobj['container'] #print ("dSource reference:" + dxsource_ref) #print ("dSource Container reference:" + dxcontainer_ref) # # Updating log... # log_msg = "dSource reference:" + dxsource_ref logger.info(log_msg) log_msg = "dSource Container reference:" + dxcontainer_ref logger.info(log_msg) for dbobj2 in dxsourceconfig_j['result']: if dbobj2['name'] == DX_SOURCE: dsourceconfig_ref = dbobj2['reference'] ##print ("dSource Config reference:" + dsourceconfig_ref) # # Updating log... # log_msg = "dSource Config reference:" + dsourceconfig_ref logger.info(log_msg) for dbobj3 in dxdatasource_j['result']: if dbobj3['container'] == dxcontainer_ref: dx_temp_datalayout= dbobj3['dataLayout'] ##print ("Self Service template reference:" + dx_temp_datalayout) # # Updating log... # log_msg = "Self Service template reference:" + dx_temp_datalayout logger.info(log_msg) for dbobj4 in dxbranch_j['result']: if dbobj4['dataLayout'] == dx_temp_datalayout : dxbranch_active = dbobj4['reference'] ##print ("Active Branch reference:" + dxbranch_active) ##print ('Bookmark set to expire on : ' + bmexpiration) # # Updating log... # log_msg = "Active Branch reference:" + dxbranch_active logger.info(log_msg) log_msg = "Bookmark set to expire on : " + bmexpiration logger.info(log_msg) formdata = '{ "type": "JSBookmarkCreateParameters", "bookmark": { "type" : "JSBookmark", "name" : "' + DXBOOKMARK + '", "branch": "' + dxbranch_active + '", "expiration": "' + bmexpiration + '" }, "timelinePointParameters": { "type" : "JSTimelinePointLatestTimeInput", "sourceDataLayout": "' + dx_temp_datalayout + '"} }' create_bookmark = session.post(BASEURL+'/selfservice/bookmark', data=formdata, headers=req_headers, allow_redirects=False) after_bm = json.loads(create_bookmark.text) check_bm = after_bm['result'] time.sleep (2) dxbookmark_chk = session.get(BASEURL+'/selfservice/bookmark', headers=req_headers, allow_redirects=False) dbookmark_chk_j = json.loads(dxbookmark_chk.text) # for dbobjbm in dbookmark_chk_j['result']: if dbobjbm['reference'] == check_bm: ##print ('Bookmark has been created successfully! ' + dbobjbm['name'] + ' ' + dbobjbm['reference']) # # Updating log... # log_msg = "Bookmark has been created successfully! " + dbobjbm['name'] + " " + dbobjbm['reference'] logger.info(log_msg) EOF chmod 755 create_bm_self_temp.py python2 create_bm_self_temp.py $DELPHIX_ADMIN $DELPHIX_PASS $DelphixEngine $DSOURCE_NAME $DXBMKEXP rm -f create_bm_self_temp.py