-
Notifications
You must be signed in to change notification settings - Fork 19
/
dx_refresh_db.py
executable file
·1009 lines (853 loc) · 34.4 KB
/
dx_refresh_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# DEPRECATED
# Adam Bowen - Apr 2016
# This script refreshes a vdb
# Updated by Corey Brune Oct 2016
# requirements
# pip install --upgrade setuptools pip docopt delphixpy
# The below doc follows the POSIX compliant standards and allows us to use
# this doc to also define our arguments for the script. This thing is brilliant.
"""Refresh a vdb
Usage:
dx_refresh_db.py (--name <name> | --dsource <name> | --all_vdbs [--group_name <name>]| --host <name> | --list_timeflows | --list_snapshots)
[--timestamp_type <type>]
[--timestamp <timepoint_semantic> --timeflow <timeflow>]
[-d <identifier> | --engine <identifier> | --all]
[--debug] [--parallel <n>] [--poll <n>]
[--config <path_to_file>] [--logdir <path_to_file>]
dx_refresh_db.py -h | --help | -v | --version
Refresh a Delphix VDB
Examples:
dx_refresh_db.py --name "aseTest" --group_name "Analytics"
dx_refresh_db.py --dsource "dlpxdb1"
dx_refresh_db.py --all_vdbs --host LINUXSOURCE --parallel 4 --debug -d landsharkengine
dx_refresh_db.py --all_vdbs --group_name "Analytics" --all
Options:
--name <name> Name of the object you are refreshing.
--all_vdbs Refresh all VDBs that meet the filter criteria.
--dsource <name> Name of dsource in Delphix to execute against.
--group_name <name> Name of the group to execute against.
--list_timeflows List all timeflows
--list_snapshots List all snapshots
--host <name> Name of environment in Delphix to execute against.
--timestamp_type <type> The type of timestamp you are specifying.
Acceptable Values: TIME, SNAPSHOT
[default: SNAPSHOT]
--timestamp <timepoint_semantic>
The Delphix semantic for the point in time on
the source from which you want to refresh your VDB.
Formats:
latest point in time or snapshot: LATEST
point in time: "YYYY-MM-DD HH24:MI:SS"
snapshot name: "@YYYY-MM-DDTHH24:MI:SS.ZZZ"
snapshot time from GUI: "YYYY-MM-DD HH24:MI"
[default: LATEST]
--timeflow <name> Name of the timeflow to refresh a VDB
-d <identifier> Identifier of Delphix engine in dxtools.conf.
--engine <type> Alt Identifier of Delphix engine in dxtools.conf.
--all Run against all engines.
--debug Enable debug logging
--parallel <n> Limit number of jobs to maxjob
--poll <n> The number of seconds to wait between job polls
[default: 10]
--config <path_to_file> The path to the dxtools.conf file
[default: ./dxtools.conf]
--logdir <path_to_file> The path to the logfile you want to use.
[default: ./dx_refresh_db.log]
-h --help Show this screen.
-v --version Show version.
"""
from __future__ import print_function
import json
import logging
import sys
import traceback
from os.path import basename
from time import sleep
from time import time
from docopt import docopt
from delphixpy.v1_8_0 import job_context
from delphixpy.v1_8_0.delphix_engine import DelphixEngine
from delphixpy.v1_8_0.exceptions import HttpError
from delphixpy.v1_8_0.exceptions import JobError
from delphixpy.v1_8_0.exceptions import RequestError
from delphixpy.v1_8_0.web import database
from delphixpy.v1_8_0.web import environment
from delphixpy.v1_8_0.web import group
from delphixpy.v1_8_0.web import job
from delphixpy.v1_8_0.web import source
from delphixpy.v1_8_0.web import timeflow
from delphixpy.v1_8_0.web.snapshot import snapshot
from delphixpy.v1_8_0.web.vo import OracleRefreshParameters
from delphixpy.v1_8_0.web.vo import RefreshParameters
from delphixpy.v1_8_0.web.vo import TimeflowPointLocation
from delphixpy.v1_8_0.web.vo import TimeflowPointSemantic
from delphixpy.v1_8_0.web.vo import TimeflowPointTimestamp
from lib.DlpxException import DlpxException
from lib.DxLogging import logging_est
from lib.DxLogging import print_debug
from lib.DxLogging import print_exception
from lib.DxLogging import print_info
from lib.GetReferences import find_obj_by_name
from lib.GetSession import GetSession
VERSION = "v.0.1.615"
def run_async(func):
"""
http://code.activestate.com/recipes/576684-simple-threading-decorator/
run_async(func)
function decorator, intended to make "func" run in a separate
thread (asynchronously).
Returns the created Thread object
E.g.:
@run_async
def task1():
do_something
@run_async
def task2():
do_something_too
t1 = task1()
t2 = task2()
...
t1.join()
t2.join()
"""
from threading import Thread
from functools import wraps
@wraps(func)
def async_func(*args, **kwargs):
func_hl = Thread(target=func, args=args, kwargs=kwargs)
func_hl.start()
return func_hl
return async_func
def find_all_databases_by_dsource_name(
engine, server, dsource_name, exclude_js_container=True
):
"""
Easy way to quickly find databases by dSource
"""
# First search for the dSource name specified and return its reference
dsource_obj = find_obj_by_name(engine, server, database, dsource_name)
if dsource_obj:
return database.get_all(
server,
provision_container=dsource_obj.reference,
no_js_container_data_source=exclude_js_container,
)
def find_all_databases_by_group_name(
engine, server, group_name, exclude_js_container=True
):
"""
Easy way to quickly find databases by group name
"""
# First search groups for the name specified and return its reference
group_obj = find_obj_by_name(engine, server, group, group_name)
if group_obj:
return database.get_all(
server,
group=group_obj.reference,
no_js_container_data_source=exclude_js_container,
)
def find_database_by_name_and_group_name(engine, server, group_name, database_name):
databases = find_all_databases_by_group_name(engine, server, group_name)
for each in databases:
if each.name == database_name:
print_debug(engine["hostname"] + ": Found a match " + str(each.reference))
return each
print_info(
engine["hostname"] + ': Unable to find "' + database_name + '" in ' + group_name
)
def find_snapshot_by_database_and_name(engine, server, database_obj, snap_name):
snapshots = snapshot.get_all(server, database=database_obj.reference)
matches = []
for snapshot_obj in snapshots:
if str(snapshot_obj.name).startswith(arguments["--timestamp"]):
matches.append(snapshot_obj)
if len(matches) == 1:
print_debug(
engine["hostname"] + ": Found one and only one match. This is good."
)
print_debug(engine["hostname"] + ": " + matches[0])
return matches[0]
elif len(matches) > 1:
print_error(
"The name specified was not specific enough. " "More than one match found."
)
for each in matches:
print_debug(engine["hostname"] + ": " + each.name)
else:
print_error("No matches found for the time specified")
print_error("No matching snapshot found")
def find_snapshot_by_database_and_time(engine, server, database_obj, snap_time):
"""
Find snapshot object by database name and timetamp
engine:
server: A Delphix engine object.
database_obj: The database reference to retrieve the snapshot
snap_time: timstamp of the snapshot
"""
snapshots = snapshot.get_all(server, database=database_obj.reference)
matches = []
for snapshot_obj in snapshots:
if (
str(snapshot_obj.latest_change_point.timestamp) == snap_time
or str(snapshot_obj.first_change_point.timestamp) == snap_time
):
matches.append(snapshot_obj)
if len(matches) == 1:
snap_match = get_obj_name(server, database, matches[0].container)
print_debug(
engine["hostname"] + ": Found one and only one match. This is good."
)
print_debug(engine["hostname"] + ": " + snap_match)
return matches[0]
elif len(matches) > 1:
print_debug(engine["hostname"] + ": " + matches)
raise DlpxException(
"The time specified was not specific enough."
" More than one match found.\n"
)
else:
raise DlpxException("No matches found for the time specified.\n")
def find_source_by_database(engine, server, database_obj):
# The source tells us if the database is enabled/disables, virtual,
# vdb/dSource, or is a staging database.
source_obj = source.get_all(server, database=database_obj.reference)
# We'll just do a little sanity check here to ensure we only have a
# 1:1 result.
if len(source_obj) == 0:
print_error(
engine["hostname"]
+ ": Did not find a source for "
+ database_obj.name
+ ". Exiting"
)
sys.exit(1)
elif len(source_obj) > 1:
print_error(
engine["hostname"]
+ ": More than one source returned for "
+ database_obj.name
+ ". Exiting"
)
print_error(source_obj)
sys.exit(1)
return source_obj
def get_config(config_file_path):
"""
This function reads in the dxtools.conf file
"""
# First test to see that the file is there and we can open it
try:
config_file = open(config_file_path).read()
except:
print_error(
"Was unable to open "
+ config_file_path
+ ". Please check the path and permissions, then try again."
)
sys.exit(1)
# Now parse the file contents as json and turn them into a
# python dictionary, throw an error if it isn't proper json
try:
config = json.loads(config_file)
except:
print_error(
"Was unable to read "
+ config_file_path
+ " as json. Please check file in a json formatter and "
"try again."
)
sys.exit(1)
# Create a dictionary of engines (removing the data node from the
# dxtools.json, for easier parsing)
delphix_engines = {}
for each in config["data"]:
delphix_engines[each["hostname"]] = each
print_debug(delphix_engines)
return delphix_engines
def job_mode(server):
"""
This function tells Delphix how to execute jobs, based on the
single_thread variable at the beginning of the file
"""
# Synchronously (one at a time)
if single_thread == True:
job_m = job_context.sync(server)
print_debug("These jobs will be executed synchronously")
# Or asynchronously
else:
job_m = job_context.asyncly(server)
print_debug("These jobs will be executed asynchronously")
return job_m
def job_wait():
"""
This job stops all work in the thread/process until all jobs on the
engine are completed.
"""
# Grab all the jos on the server (the last 25, be default)
all_jobs = job.get_all(server)
# For each job in the list, check to see if it is running (not ended)
for jobobj in all_jobs:
if not (jobobj.job_state in ["CANCELED", "COMPLETED", "FAILED"]):
print_debug(
"Waiting for "
+ jobobj.reference
+ " (currently: "
+ jobobj.job_state
+ ") to finish running against the container"
)
# If so, wait
job_context.wait(server, jobobj.reference)
def get_obj_name(server, f_object, obj_reference):
"""
Return the object name from obj_reference
engine: A Delphix engine object.
obj_reference: The object reference to retrieve the name
"""
try:
obj_name = f_object.get(server, obj_reference)
return obj_name.name
except RequestError as e:
raise dlpxExceptionHandler(e)
except HttpError as e:
raise DlpxException(e)
def list_snapshots(server):
"""
List all snapshots with timestamps
"""
header = "Snapshot Name, First Change Point, Location, Latest Change Point"
snapshots = snapshot.get_all(server)
print(header)
for snap in snapshots:
container_name = get_obj_name(server, database, snap.container)
snap_range = snapshot.timeflow_range(server, snap.reference)
print(
"{}, {}, {}, {}, {}".format(
str(snap.name),
container_name,
snap_range.start_point.timestamp,
snap_range.start_point.location,
snap_range.end_point.timestamp,
)
)
@run_async
def main_workflow(engine):
"""
This function is where we create our main workflow.
Use the @run_async decorator to run this function asynchronously.
The @run_async decorator allows us to run against multiple Delphix Engine
simultaneously
"""
# Pull out the values from the dictionary for this engine
engine_address = engine["ip_address"]
engine_username = engine["username"]
engine_password = engine["password"]
# Establish these variables as empty for use later
databases = []
environment_obj = None
source_objs = None
jobs = {}
# Setup the connection to the Delphix Engine
server = serversess(engine_address, engine_username, engine_password)
# If an environment/server was specified
if host_name:
print_debug(engine["hostname"] + ": Getting environment for " + host_name)
# Get the environment object by the hostname
environment_obj = find_obj_by_name(engine, server, environment, host_name)
if environment_obj != None:
# Get all the sources running on the server
env_source_objs = source.get_all(
server, environment=environment_obj.reference
)
# If the server doesn't have any objects, exit.
if env_source_objs == None:
print_error(host_name + "does not have any objects. Exiting")
sys.exit(1)
# If we are only filtering by the server, then put those objects in
# the main list for processing
if not (arguments["--group_name"] and database_name):
source_objs = env_source_objs
all_dbs = database.get_all(server, no_js_container_data_source=True)
databases = []
for source_obj in source_objs:
if source_obj.staging == False and source_obj.virtual == True:
database_obj = database.get(server, source_obj.container)
if database_obj in all_dbs:
databases.append(database_obj)
else:
print_error(
engine["hostname"]
+ ":No environment found for "
+ host_name
+ ". Exiting"
)
sys.exit(1)
# If we specified a specific database by name....
if arguments["--name"]:
# Get the database object from the name
database_obj = find_obj_by_name(engine, server, database, arguments["--name"])
if database_obj:
databases.append(database_obj)
# Else if we specified a group to filter by....
elif arguments["--group_name"]:
print_debug(
engine["hostname"]
+ ":Getting databases in group "
+ arguments["--group_name"]
)
# Get all the database objects in a group.
databases = find_all_databases_by_group_name(
engine, server, arguments["--group_name"]
)
# Else if we specified a dSource to filter by....
elif arguments["--dsource"]:
print_debug(
engine["hostname"]
+ ":Getting databases for dSource"
+ arguments["--dsource"]
)
# Get all the database objects in a group.
databases = find_all_databases_by_dsource_name(
engine, server, arguments["--dsource"]
)
# Else, if we said all vdbs ...
elif arguments["--all_vdbs"] and not arguments["--host"]:
print_debug(engine["hostname"] + ":Getting all VDBs ")
# Grab all databases, but filter out the database that are in JetStream
# containers, because we can't refresh those this way.
databases = database.get_all(server, no_js_container_data_source=True)
elif arguments["--list_timeflows"]:
list_timeflows(server)
elif arguments["--list_snapshots"]:
list_snapshots(server)
# reset the running job count before we begin
i = 0
with job_mode(server):
# While there are still running jobs or databases still to process....
while len(jobs) > 0 or len(databases) > 0:
# While there are databases still to process and we are still under
# the max simultaneous jobs threshold (if specified)
while len(databases) > 0 and (
arguments["--parallel"] == None or i < int(arguments["--parallel"])
):
# Give us the next database in the list, and then remove it
database_obj = databases.pop()
# Get the source of the database.
source_obj = find_source_by_database(engine, server, database_obj)
# If we applied the environment/server filter AND group filter,
# find the intersecting matches
if environment_obj != None and (arguments["--group_name"]):
match = False
for env_source_obj in env_source_objs:
if source_obj[0].reference in env_source_obj.reference:
match = True
break
if match == False:
print_error(
engine["hostname"]
+ ": "
+ database_obj.name
+ " does not exist on "
+ host_name
+ ". Exiting"
)
return
# Refresh the database
refresh_job = refresh_database(
engine, server, jobs, source_obj[0], database_obj
)
# If refresh_job has any value, then we know that a job was
# initiated.
if refresh_job:
# increment the running job count
i += 1
# Check to see if we are running at max parallel processes, and
# report if so.
if arguments["--parallel"] != None and i >= int(arguments["--parallel"]):
print_info(engine["hostname"] + ": Max jobs reached (" + str(i) + ")")
i = update_jobs_dictionary(engine, server, jobs)
print_info(
engine["hostname"]
+ ": "
+ str(i)
+ " jobs running. "
+ str(len(databases))
+ " jobs waiting to run"
)
# If we have running jobs, pause before repeating the checks.
if len(jobs) > 0:
sleep(float(arguments["--poll"]))
def print_error(print_obj):
"""
Call this function with a log message to prefix the message with ERROR
"""
print("ERROR: " + str(print_obj))
logging.error(str(print_obj))
def print_warning(print_obj):
"""
Call this function with a log message to prefix the message with WARNING
"""
print("WARNING: " + str(print_obj))
logging.warning(str(print_obj))
def refresh_database(engine, server, jobs, source_obj, container_obj):
"""
This function actually performs the refresh
engine:
server: Engine object
jobs: list containing running jobs
source_obj: source object used to refresh from snapshot or timeflow
container_obj: VDB container
"""
# Sanity check to make sure our source object has a reference
if source_obj.reference:
# We can only refresh VDB's
if source_obj.virtual != True:
print_warning(
engine["hostname"]
+ ": "
+ container_obj.name
+ " is not a virtual object. Skipping."
)
# Ensure this source is not a staging database. We can't act upon those.
elif source_obj.staging == True:
print_warning(
engine["hostname"]
+ ": "
+ container_obj.name
+ " is a staging database. Skipping."
)
# Ensure the source is enabled. We can't refresh disabled databases.
elif source_obj.runtime.enabled == "ENABLED":
source_db = database.get(server, container_obj.provision_container)
if not source_db:
print_error(
engine["hostname"]
+ ":Was unable to retrieve the source container for "
+ container_obj.name
)
print_info(
engine["hostname"]
+ ": Refreshing "
+ container_obj.name
+ " from "
+ source_db.name
)
print_debug(engine["hostname"] + ": Type: " + source_obj.type)
print_debug(engine["hostname"] + ":" + source_obj.type)
# If the vdb is a Oracle type, we need to use a
# OracleRefreshParameters
if str(container_obj.reference).startswith("ORACLE"):
refresh_params = OracleRefreshParameters()
else:
refresh_params = RefreshParameters()
try:
refresh_params.timeflow_point_parameters = set_timeflow_point(
engine, server, source_db
)
print_debug(engine["hostname"] + ":" + str(refresh_params))
# Sync it
database.refresh(server, container_obj.reference, refresh_params)
jobs[container_obj] = server.last_job
except RequestError as e:
print(
"\nERROR: Could not set timeflow point:\n%s\n" % (e.message.action)
)
sys.exit(1)
except DlpxException as e:
print("ERROR: Could not set timeflow point:\n%s\n" % (e.message))
sys.exit(1)
# return the job object to the calling statement so that we can
# tell if a job was created or not (will return None, if no job)
return server.last_job
# Don't do anything if the database is disabled
else:
print_warning(
engine["hostname"]
+ ": "
+ container_obj.name
+ " is not enabled. Skipping sync"
)
def run_job(engine):
"""
This function runs the main_workflow aynchronously against all the
servers specified
"""
# Create an empty list to store threads we create.
threads = []
# If the --all argument was given, run against every engine in dxtools.conf
if arguments["--all"]:
print_info("Executing against all Delphix Engines in the dxtools.conf")
# For each server in the dxtools.conf...
for delphix_engine in dxtools_objects:
engine = dxtools_objects[delphix_engine]
# Create a new thread and add it to the list.
threads.append(main_workflow(engine))
else:
# Else if the --engine argument was given, test to see if the engine
# exists in dxtools.conf
if arguments["--engine"]:
try:
engine = dxtools_objects[arguments["--engine"]]
print_info("Executing against Delphix Engine: " + arguments["--engine"])
except:
print_error(
'Delphix Engine "{}" cannot be found in "{}"'.format(
arguments["--engine"],
config_file_path,
)
)
print_error("Please check your value and try again. Exiting")
sys.exit(1)
# Else if the -d argument was given, test to see if the engine exists
# in dxtools.conf
elif arguments["-d"]:
try:
engine = dxtools_objects[arguments["-d"]]
print_info("Executing against Delphix Engine: " + arguments["-d"])
except:
print_error(
'Delphix Engine "'
+ arguments["-d"]
+ '" cannot be found in '
+ config_file_path
)
print_error("Please check your value and try again. Exiting")
sys.exit(1)
else:
# Else search for a default engine in the dxtools.conf
for delphix_engine in dxtools_objects:
if dxtools_objects[delphix_engine]["default"] == "true":
engine = dxtools_objects[delphix_engine]
print_info(
"Executing against the default Delphix Engine"
" in the dxtools.conf: "
+ dxtools_objects[delphix_engine]["hostname"]
)
break
if engine == None:
print_error("No default engine found. Exiting")
sys.exit(1)
# run the job against the engine
threads.append(main_workflow(engine))
# For each thread in the list...
for each in threads:
# join them back together so that we wait for all threads to complete
# before moving on
each.join()
def serversess(f_engine_address, f_engine_username, f_engine_password):
"""
Function to setup the session with the Delphix Engine
"""
server_session = DelphixEngine(
f_engine_address, f_engine_username, f_engine_password, "DOMAIN"
)
return server_session
def list_timeflows(server):
"""
Retrieve and print all timeflows for a given engine
"""
ret_timeflow_dct = {}
all_timeflows = timeflow.get_all(server)
print("DB Name, Timeflow Name, Timestamp")
for tfbm_lst in all_timeflows:
try:
db_name = get_obj_name(server, database, tfbm_lst.container)
print(
"%s, %s, %s\n"
% (
str(db_name),
str(tfbm_lst.name),
str(tfbm_lst.parent_point.timestamp),
)
)
except AttributeError:
print("%s, %s\n" % (str(tfbm_lst.name), str(db_name)))
except TypeError as e:
raise DlpxException(
"Listing Timeflows encountered an error:\n%s" % (e.message)
)
except RequestError as e:
dlpx_err = e.message
raise DlpxException(dlpx_err.action)
def set_timeflow_point(engine, server, container_obj):
"""
This returns the reference of the timestamp specified.
engine:
server: Delphix Engine object
container_obj: VDB object
"""
if arguments["--timestamp_type"].upper() == "SNAPSHOT":
if arguments["--timestamp"].upper() == "LATEST":
print_debug(engine["hostname"] + ": Using the latest Snapshot")
timeflow_point_parameters = TimeflowPointSemantic()
timeflow_point_parameters.location = "LATEST_SNAPSHOT"
elif arguments["--timestamp"].startswith("@"):
print_debug(engine["hostname"] + ": Using a named snapshot")
snapshot_obj = find_snapshot_by_database_and_name(
engine, server, container_obj, arguments["--timestamp"]
)
if snapshot_obj:
timeflow_point_parameters = TimeflowPointLocation()
timeflow_point_parameters.timeflow = snapshot_obj.timeflow
timeflow_point_parameters.location = (
snapshot_obj.latest_change_point.location
)
else:
raise DlpxException(
"ERROR: Was unable to use the specified "
"snapshot %s for database %s.\n"
% (arguments["--timestamp"], container_obj.name)
)
elif arguments["--timestamp"]:
print_debug(engine["hostname"] + ": Using a time-designated snapshot")
snapshot_obj = find_snapshot_by_database_and_time(
engine, server, container_obj, arguments["--timestamp"]
)
if snapshot_obj:
timeflow_point_parameters = TimeflowPointTimestamp()
timeflow_point_parameters.timeflow = snapshot_obj.timeflow
timeflow_point_parameters.timestamp = (
snapshot_obj.latest_change_point.timestamp
)
else:
raise DlpxException(
"Was unable to find a suitable time"
" for %s for database %s"
% (arguments["--timestamp"], container_obj.name)
)
elif arguments["--timestamp_type"].upper() == "TIME":
if arguments["--timestamp"].upper() == "LATEST":
timeflow_point_parameters = TimeflowPointSemantic()
timeflow_point_parameters.location = "LATEST_POINT"
elif arguments["--timestamp"]:
timeflow_point_parameters = TimeflowPointTimestamp()
timeflow_point_parameters.type = "TimeflowPointTimestamp"
timeflow_obj = find_obj_by_name(
engine, server, timeflow, arguments["--timeflow"]
)
timeflow_point_parameters.timeflow = timeflow_obj.reference
timeflow_point_parameters.timestamp = arguments["--timestamp"]
return timeflow_point_parameters
else:
raise DlpxException(
arguments["--timestamp_type"] + " is not a valied timestamp_type. Exiting"
)
timeflow_point_parameters.container = container_obj.reference
return timeflow_point_parameters
def time_elapsed():
"""
This function calculates the time elapsed since the beginning of the script.
Call this anywhere you want to note the progress in terms of time
"""
elapsed_minutes = round((time() - time_start) / 60, +1)
return elapsed_minutes
def update_jobs_dictionary(engine, server, jobs):
"""
This function checks each job in the dictionary and updates its status or
removes it if the job is complete.
Return the number of jobs still running.
"""
# Establish the running jobs counter, as we are about to update the count
# from the jobs report.
i = 0
# get all the jobs, then inspect them
for j in jobs.keys():
job_obj = job.get(server, jobs[j])
print_debug(engine["hostname"] + ": " + str(job_obj))
print_info(engine["hostname"] + ": " + j.name + ": " + job_obj.job_state)
if job_obj.job_state in ["CANCELED", "COMPLETED", "FAILED"]:
# If the job is in a non-running state, remove it from the running
# jobs list.
del jobs[j]
else:
# If the job is in a running state, increment the running job count.
i += 1
return i
def main(argv):
# We want to be able to call on these variables anywhere in the script.
global single_thread
global usebackup
global time_start
global host_name
global database_name
global config_file_path
global dxtools_objects
try:
# Declare globals that will be used throughout the script.
logging_est(arguments["--logdir"])
print_debug(arguments)
time_start = time()
engine = None
single_thread = False
database_name = arguments["--name"]
host_name = arguments["--host"]
config_file_path = arguments["--config"]
# Parse the dxtools.conf and put it into a dictionary
dxtools_objects = get_config(config_file_path)
# This is the function that will handle processing main_workflow for
# all the servers.
run_job(engine)
elapsed_minutes = time_elapsed()
print_info("script took " + str(elapsed_minutes) + " minutes to get this far.")
# Here we handle what we do when the unexpected happens
except SystemExit as e:
"""
This is what we use to handle our sys.exit(#)
"""
sys.exit(e)
except HttpError as e:
"""
We use this exception handler when our connection to Delphix fails
"""
print_error("Connection failed to the Delphix Engine")
print_error("Please check the ERROR message below")
print_error(e.message)
sys.exit(2)
except JobError as e:
"""
We use this exception handler when a job fails in Delphix so that we
have actionable data
"""
print_error("A job failed in the Delphix Engine")
print_error(e.job)
elapsed_minutes = time_elapsed()
print_info(
basename(__file__)
+ " took "
+ str(elapsed_minutes)
+ " minutes to get this far."
)
sys.exit(3)
except KeyboardInterrupt:
"""
We use this exception handler to gracefully handle ctrl+c exits
"""
print_debug("You sent a CTRL+C to interrupt the process")
elapsed_minutes = time_elapsed()
print_info(
basename(__file__)
+ " took "
+ str(elapsed_minutes)
+ " minutes to get this far."
)
except:
"""
Everything else gets caught here
"""
print_error(sys.exc_info()[0])
print_error(traceback.format_exc())
elapsed_minutes = time_elapsed()
print_info(
basename(__file__)
+ " took "
+ str(elapsed_minutes)
+ " minutes to get this far."
)
sys.exit(1)