-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdx_provision_vdb.py
executable file
·1253 lines (1048 loc) · 44.6 KB
/
dx_provision_vdb.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
# Adam Bowen - Apr 2016
# This script provisions a vdb or dSource
# Updated by Corey Brune Aug 2016
# --- Create vFiles VDB
# requirements
# pip install 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.
# TODO:
# Refactor provisioning functions
# Documentation
"""Provision VDB's
Usage:
dx_provision_db.py --source <name> --target_grp <name> --target <name>
(--db <name> | --vfiles_path <path>) [--no_truncate_log]
(--environment <name> --type <type>) [ --envinst <name>]
[--template <name>] [--mapfile <file>]
[--timestamp_type <type>] [--timestamp <timepoint_semantic>]
[--timeflow <name>]
[--instname <sid>] [--mntpoint <path>] [--noopen]
[--uniqname <name>][--source_grp <name>]
[--engine <identifier> | --all]
[--vdb_restart <bool> ]
[--debug] [--parallel <n>] [--poll <n>]
[--config <path_to_file>] [--logdir <path_to_file>]
[--postrefresh <name>] [--prerefresh <name>]
[--configure-clone <name>]
[--prerollback <name>] [--postrollback <name>]
dx_provision_db.py -h | --help | -v | --version
Provision VDB from a defined source on the defined target environment.
Examples:
dx_provision_vdb.py --engine landsharkengine --source_grp Sources --source "ASE pubs3 DB" --db vase --target testASE --target_grp Analytics --environment LINUXTARGET --type ase --envinst "LINUXTARGET"
dx_provision_vdb.py --source_grp Sources --source "Employee Oracle 11G DB" --instname autod --uniqname autoprod --db autoprod --target autoprod --target_grp Analytics --environment LINUXTARGET --type oracle --envinst "/u01/app/oracle/product/11.2.0/dbhome_1"
dx_provision_vdb.py --source_grp Sources --source "AdventureWorksLT2008R2" --db vAW --target testAW --target_grp Analytics --environment WINDOWSTARGET --type mssql --envinst MSSQLSERVER --all
dx_provision_vdb.py --source UF_Source --target appDataVDB --target_grp Untitled --environment LinuxTarget --type vfiles --vfiles_path /mnt/provision/appDataVDB --prerollback "/u01/app/oracle/product/scripts/PreRollback.sh" --postrollback "/u01/app/oracle/product/scripts/PostRollback.sh" --vdb_restart true
Options:
--source_grp <name> The group where the source resides.
--source <name> Name of the source object
--target_grp <name> The group into which Delphix will place the VDB.
--target <name> The unique name that you want to call this object
in Delphix
--db <name> The name you want to give the database (Oracle Only)
--vfiles_path <path> The full path on the Target server where Delphix
will provision the vFiles
--no_truncate_log Don't truncate log on checkpoint (ASE only)
--environment <name> The name of the Target environment in Delphix
--type <type> The type of VDB this is.
oracle | mssql | ase | vfiles
--prerefresh <name> Pre-Hook commands
--postrefresh <name> Post-Hook commands
--prerollback <name> Post-Hook commands
--postrollback <name> Post-Hook commands
--configure-clone <name> Configure Clone commands
--vdb_restart <bool> Either True or False. Default: False
--envinst <name> The identifier of the instance in Delphix.
ex. "/u01/app/oracle/product/11.2.0/dbhome_1"
ex. LINUXTARGET
--timeflow <name> Name of the timeflow from which you are provisioning
--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 from
which you want to provision 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]
--template <name> Target VDB Template name (Oracle Only)
--mapfile <file> Target VDB mapping file (Oracle Only)
--instname <sid> Target VDB SID name (Oracle Only)
--uniqname <name> Target VDB db_unique_name (Oracle Only)
--mntpoint <path> Mount point for the VDB
[default: /mnt/provision]
--noopen Don't open database after provision (Oracle Only)
--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_provision_vdb.log]
-h --help Show this screen.
-v --version Show version.
"""
from __future__ import print_function
import re
import signal
import sys
import time
import traceback
from os.path import basename
from time import sleep
from time import time
from docopt import docopt
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 repository
from delphixpy.v1_8_0.web import snapshot
from delphixpy.v1_8_0.web import source
from delphixpy.v1_8_0.web.database import template
from delphixpy.v1_8_0.web.vo import AppDataDirectSourceConfig
from delphixpy.v1_8_0.web.vo import AppDataProvisionParameters
from delphixpy.v1_8_0.web.vo import AppDataVirtualSource
from delphixpy.v1_8_0.web.vo import ASEDBContainer
from delphixpy.v1_8_0.web.vo import ASEInstanceConfig
from delphixpy.v1_8_0.web.vo import ASEProvisionParameters
from delphixpy.v1_8_0.web.vo import ASESIConfig
from delphixpy.v1_8_0.web.vo import ASEVirtualSource
from delphixpy.v1_8_0.web.vo import MSSqlDatabaseContainer
from delphixpy.v1_8_0.web.vo import MSSqlProvisionParameters
from delphixpy.v1_8_0.web.vo import MSSqlSIConfig
from delphixpy.v1_8_0.web.vo import MSSqlVirtualSource
from delphixpy.v1_8_0.web.vo import OracleDatabaseContainer
from delphixpy.v1_8_0.web.vo import OracleInstance
from delphixpy.v1_8_0.web.vo import OracleProvisionParameters
from delphixpy.v1_8_0.web.vo import OracleSIConfig
from delphixpy.v1_8_0.web.vo import OracleVirtualSource
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 delphixpy.v1_8_0.web.vo import VirtualSourceOperations
from lib.DlpxException import DlpxException
from lib.DxLogging import logging_est
from lib.DxLogging import print_debug
from lib.DxLogging import print_info
from lib.DxTimeflow import DxTimeflow
from lib.GetReferences import find_dbrepo
from lib.GetReferences import find_obj_by_name
from lib.GetSession import GetSession
VERSION = "v.0.2.305"
def create_ase_vdb(
engine, server, jobs, vdb_group, vdb_name, environment_obj, container_obj
):
"""
Create a Sybase ASE VDB
"""
vdb_obj = find_database_by_name_and_group_name(
engine, server, vdb_group.name, vdb_name
)
if vdb_obj == None:
vdb_params = ASEProvisionParameters()
vdb_params.container = ASEDBContainer()
if arguments["--no_truncate_log"]:
vdb_params.truncate_log_on_checkpoint = False
else:
vdb_params.truncate_log_on_checkpoint = True
vdb_params.container.group = vdb_group.reference
vdb_params.container.name = vdb_name
vdb_params.source = ASEVirtualSource()
vdb_params.source_config = ASESIConfig()
vdb_params.source_config.database_name = arguments["--db"]
vdb_params.source_config.instance = ASEInstanceConfig()
vdb_params.source_config.instance.host = environment_obj.host
vdb_repo = find_dbrepo_by_environment_ref_and_name(
engine,
server,
"ASEInstance",
environment_obj.reference,
arguments["--envinst"],
)
vdb_params.source_config.repository = vdb_repo.reference
vdb_params.timeflow_point_parameters = set_timeflow_point(
engine, server, container_obj
)
vdb_params.timeflow_point_parameters.container = container_obj.reference
print_info("Provisioning " + vdb_name)
database.provision(server, vdb_params)
# Add the job into the jobs dictionary so we can track its progress
jobs[engine["hostname"]] = server.last_job
# 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
else:
print_info(engine["hostname"] + ": " + vdb_name + " already exists.")
return vdb_obj.reference
def create_mssql_vdb(engine, jobs, vdb_group, vdb_name, environment_obj, container_obj):
"""
Create a MSSQL VDB
engine:
jobs:
vdb_group:
vdb_name,
environment_obj:
container_obj:
"""
vdb_obj = find_database_by_name_and_group_name(
engine, dx_session_obj.server_session, vdb_group.name, vdb_name
)
if vdb_obj == None:
vdb_params = MSSqlProvisionParameters()
vdb_params.container = MSSqlDatabaseContainer()
vdb_params.container.group = vdb_group.reference
vdb_params.container.name = vdb_name
vdb_params.source = MSSqlVirtualSource()
vdb_params.source.allow_auto_vdb_restart_on_host_reboot = False
vdb_params.source_config = MSSqlSIConfig()
vdb_params.source_config.database_name = arguments["--db"]
vdb_params.source_config.repository = find_dbrepo(
dx_session_obj.server_session,
"MSSqlInstance",
environment_obj.reference,
arguments["--envinst"],
).reference
vdb_params.timeflow_point_parameters = set_timeflow_point(
engine, dx_session_obj.server_session, container_obj
)
if not vdb_params.timeflow_point_parameters:
return
vdb_params.timeflow_point_parameters.container = container_obj.reference
print_info(engine["hostname"] + ":Provisioning " + vdb_name)
database.provision(dx_session_obj.server_session, vdb_params)
# Add the job into the jobs dictionary so we can track its progress
jobs[engine["hostname"]] = dx_session_obj.server_session.last_job
# 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 dx_session_obj.server_session.last_job
else:
print_info(engine["hostname"] + ": " + vdb_name + " already exists.")
return vdb_obj.reference
def create_vfiles_vdb(
engine,
jobs,
vfiles_group,
vfiles_name,
environment_obj,
container_obj,
pre_refresh=None,
post_refresh=None,
pre_rollback=None,
post_rollback=None,
configure_clone=None,
):
"""
Create a Vfiles VDB
"""
vfiles_obj = None
try:
vfiles_obj = find_obj_by_name(
dx_session_obj.server_session, database, vfiles_name
)
except DlpxException:
pass
if vfiles_obj is None:
vfiles_repo = find_repo_by_environment_ref(
engine, "Unstructured Files", environment_obj.reference
)
vfiles_params = AppDataProvisionParameters()
vfiles_params.source = AppDataVirtualSource()
vfiles_params.source_config = AppDataDirectSourceConfig()
vdb_restart_reobj = re.compile("true", re.IGNORECASE)
if vdb_restart_reobj.search(str(arguments["--vdb_restart"])):
vfiles_params.source.allow_auto_vdb_restart_on_host_reboot = True
elif vdb_restart_reobj.search(str(arguments["--vdb_restart"])) is None:
vfiles_params.source.allow_auto_vdb_restart_on_host_reboot = False
vfiles_params.container = {
"type": "AppDataContainer",
"group": vfiles_group.reference,
"name": vfiles_name,
}
vfiles_params.source_config.name = arguments["--target"]
vfiles_params.source_config.path = arguments["--vfiles_path"]
vfiles_params.source_config.environment_user = environment_obj.primary_user
vfiles_params.source_config.repository = vfiles_repo.reference
vfiles_params.source.parameters = {}
vfiles_params.source.name = vfiles_name
vfiles_params.source.name = vfiles_name
vfiles_params.source.operations = VirtualSourceOperations()
if pre_refresh:
vfiles_params.source.operations.pre_refresh = [
{"type": "RunCommandOnSourceOperation", "command": pre_refresh}
]
if post_refresh:
vfiles_params.source.operations.post_refresh = [
{"type": "RunCommandOnSourceOperation", "command": post_refresh}
]
if pre_rollback:
vfiles_params.source.operations.pre_rollback = [
{"type": "RunCommandOnSourceOperation", "command": pre_rollback}
]
if post_rollback:
vfiles_params.source.operations.post_rollback = [
{"type": "RunCommandOnSourceOperation", "command": post_rollback}
]
if configure_clone:
vfiles_params.source.operations.configure_clone = [
{"type": "RunCommandOnSourceOperation", "command": configure_clone}
]
if arguments["--timestamp_type"] is None:
vfiles_params.timeflow_point_parameters = {
"type": "TimeflowPointSemantic",
"container": container_obj.reference,
"location": "LATEST_POINT",
}
elif arguments["--timestamp_type"].upper() == "SNAPSHOT":
try:
dx_timeflow_obj = DxTimeflow(dx_session_obj.server_session)
dx_snap_params = dx_timeflow_obj.set_timeflow_point(
container_obj,
arguments["--timestamp_type"],
arguments["--timestamp"],
arguments["--timeflow"],
)
except RequestError as e:
raise DlpxException("Could not set the timeflow point:\n%s" % (e))
if dx_snap_params.type == "TimeflowPointSemantic":
vfiles_params.timeflow_point_parameters = {
"type": dx_snap_params.type,
"container": dx_snap_params.container,
"location": dx_snap_params.location,
}
elif dx_snap_params.type == "TimeflowPointTimestamp":
vfiles_params.timeflow_point_parameters = {
"type": dx_snap_params.type,
"timeflow": dx_snap_params.timeflow,
"timestamp": dx_snap_params.timestamp,
}
print_info("%s: Provisioning %s\n" % (engine["hostname"], vfiles_name))
try:
database.provision(dx_session_obj.server_session, vfiles_params)
except (JobError, RequestError, HttpError) as e:
raise DlpxException(
"\nERROR: Could not provision the database:" "\n%s" % (e)
)
# Add the job into the jobs dictionary so we can track its progress
jobs[engine["hostname"]] = dx_session_obj.server_session.last_job
# 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 dx_session_obj.server_session.last_job
else:
print_info(
"\nERROR %s: %s already exists. \n" % (engine["hostname"], vfiles_name)
)
return vfiles_obj.reference
def create_oracle_si_vdb(
engine,
jobs,
vdb_name,
vdb_group_obj,
environment_obj,
container_obj,
pre_refresh=None,
post_refresh=None,
pre_rollback=None,
post_rollback=None,
configure_clone=None,
):
"""
Create an Oracle SI VDB
"""
vdb_obj = None
try:
vdb_obj = find_obj_by_name(dx_session_obj.server_session, database, vdb_name)
except DlpxException:
pass
if vdb_obj == None:
vdb_params = OracleProvisionParameters()
vdb_params.open_resetlogs = True
if arguments["--noopen"]:
vdb_params.open_resetlogs = False
vdb_params.container = OracleDatabaseContainer()
vdb_params.container.group = vdb_group_obj.reference
vdb_params.container.name = vdb_name
vdb_params.source = OracleVirtualSource()
vdb_params.source.allow_auto_vdb_restart_on_host_reboot = False
if arguments["--instname"]:
inst_name = arguments["--instname"]
elif arguments["--instname"] == None:
inst_name = vdb_name
if arguments["--uniqname"]:
unique_name = arguments["--uniqname"]
elif arguments["--uniqname"] == None:
unique_name = vdb_name
if arguments["--db"]:
db = arguments["--db"]
elif arguments["--db"] == None:
db = vdb_name
vdb_params.source.mount_base = arguments["--mntpoint"]
if arguments["--mapfile"]:
vdb_params.source.file_mapping_rules = arguments["--mapfile"]
if arguments["--template"]:
template_obj = find_obj_by_name(
dx_session_obj.server_session,
database.template,
arguments["--template"],
)
vdb_params.source.config_template = template_obj.reference
vdb_params.source_config = OracleSIConfig()
vdb_params.source.operations = VirtualSourceOperations()
if pre_refresh:
vdb_params.source.operations.pre_refresh = [
{"type": "RunCommandOnSourceOperation", "command": pre_refresh}
]
if post_refresh:
vdb_params.source.operations.post_refresh = [
{"type": "RunCommandOnSourceOperation", "command": post_refresh}
]
if pre_rollback:
vdb_params.source.operations.pre_rollback = [
{"type": "RunCommandOnSourceOperation", "command": pre_rollback}
]
if post_rollback:
vdb_params.source.operations.post_rollback = [
{"type": "RunCommandOnSourceOperation", "command": post_rollback}
]
if configure_clone:
vdb_params.source.operations.configure_clone = [
{"type": "RunCommandOnSourceOperation", "command": configure_clone}
]
vdb_repo = find_dbrepo_by_environment_ref_and_install_path(
engine,
dx_session_obj.server_session,
"OracleInstall",
environment_obj.reference,
arguments["--envinst"],
)
vdb_params.source_config.database_name = db
vdb_params.source_config.unique_name = unique_name
vdb_params.source_config.instance = OracleInstance()
vdb_params.source_config.instance.instance_name = inst_name
vdb_params.source_config.instance.instance_number = 1
vdb_params.source_config.repository = vdb_repo.reference
dx_timeflow_obj = DxTimeflow(dx_session_obj.server_session)
vdb_params.timeflow_point_parameters = dx_timeflow_obj.set_timeflow_point(
container_obj, arguments["--timestamp_type"], arguments["--timestamp"]
)
print(vdb_params, "\n\n\n")
print_info(engine["hostname"] + ": Provisioning " + vdb_name)
database.provision(dx_session_obj.server_session, vdb_params)
# Add the job into the jobs dictionary so we can track its progress
jobs[engine["hostname"]] = dx_session_obj.server_session.last_job
# 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 dx_session_obj.server_session.last_job
else:
raise DlpxException(
"\nERROR: %s: %s alread exists\n" % (engine["hostname"], vdb_name)
)
def find_all_databases_by_group_name(
engine, server, group_name, exclude_js_container=False
):
"""
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(dx_session_obj.server_session, group, group_name)
if group_obj:
databases = database.get_all(
server,
group=group_obj.reference,
no_js_container_data_source=exclude_js_container,
)
return databases
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(
"%s: Found a match %s" % (engine["hostname"], str(each.reference))
)
return each
print_info(
"%s unable to find %s in %s" % (engine["hostname"], database_name, group_name)
)
def find_dbrepo_by_environment_ref_and_install_path(
engine, server, install_type, f_environment_ref, f_install_path
):
"""
Function to find database repository objects by environment reference and
install path, and return the object's reference as a string
You might use this function to find Oracle and PostGreSQL database repos.
"""
print_debug(
"%s: Searching objects in the %s class for one with the "
"environment reference of %s and an install path of %s"
% (engine["hostname"], install_type, f_environment_ref, f_install_path),
debug,
)
for obj in repository.get_all(server, environment=f_environment_ref):
if install_type == "PgSQLInstall":
if obj.type == install_type and obj.installation_path == f_install_path:
print_debug(
"%s: Found a match %s" % (engine["hostname"], str(obj.reference)),
debug,
)
return obj
elif install_type == "OracleInstall":
if obj.type == install_type and obj.installation_home == f_install_path:
print_debug(
"%s: Fount a match %s" % (engine["hostname"], str(obj.reference)),
debug,
)
return obj
else:
raise DlpxException(
"%s: No Repo match found for type %s.\n"
% (engine["hostname"], install_type)
)
def find_repo_by_environment_ref(
engine, repo_type, f_environment_ref, f_install_path=None
):
"""
Function to find unstructured file repository objects by environment
reference and name, and return the object's reference as a string
You might use this function to find Unstructured File repos.
"""
print_debug(
"\n%s: Searching objects in the %s class for one with the"
"environment reference of %s\n"
% (engine["hostname"], repo_type, f_environment_ref),
debug,
)
obj_ref = ""
all_objs = repository.get_all(
dx_session_obj.server_session, environment=f_environment_ref
)
for obj in all_objs:
if obj.name == repo_type:
print_debug(engine["hostname"] + ": Found a match " + str(obj.reference))
return obj
elif obj.type == repo_type:
print_debug(
"%s Found a match %s" % (engine["hostname"], str(obj.reference)), debug
)
return obj
raise DlpxException(
"%s: No Repo match found for type %s\n" % (engine["hostname"], repo_type)
)
def find_dbrepo_by_environment_ref_and_name(
engine, repo_type, f_environment_ref, f_name
):
"""
Function to find database repository objects by environment reference and
name, and return the object's reference as a string
You might use this function to find MSSQL database repos.
"""
print_debug(
"%s: Searching objects in the %s class for one with the "
"environment reference of %s and a name of %s."
% (engine["hostname"], repo_type, f_environment_ref, f_name),
debug,
)
obj_ref = ""
all_objs = repository.get_all(server, environment=f_environment_ref)
for obj in all_objs:
if repo_type == "MSSqlInstance" or repo_type == "ASEInstance":
if obj.type == repo_type and obj.name == f_name:
print_debug(
"%s: Found a match %s" % (engine["hostname"], str(obj.reference)),
debug,
)
return obj
elif repo_type == "Unstructured Files":
if obj.value == install_type:
print_debug(
"%s: Found a match %s" % (engine["hostname"], str(obj.reference)),
debug,
)
return obj
raise DlpxException(
"%s: No Repo match found for type %s\n" % (engine["hostname"], repo_type)
)
def find_snapshot_by_database_and_name(engine, database_obj, snap_name):
"""
Find snapshots by database and name. Return snapshot reference.
engine: Dictionary of engines from config file.
database_obj: Database object to find the snapshot against
snap_name: Name of the snapshot
"""
snapshots = snapshot.get_all(
dx_session_obj.server_session, database=database_obj.reference
)
matches = []
for snapshot_obj in snapshots:
if str(snapshot_obj.name).startswith(arguments["--timestamp"]):
matches.append(snapshot_obj)
for each in matches:
print_debug(each.name, debug)
if len(matches) == 1:
print_debug(
"%s: Found one and only one match. This is good.\n %s"
% (engine["hostname"], matches[0]),
debug,
)
return matches[0]
elif len(matches) > 1:
raise DlpxException(
"%s: The name specified was not specific enough."
" More than one match found.\n" % (engine["hostname"],)
)
else:
raise DlpxException(
"%s: No matches found for the time specified.\n" % (engine["hostname"])
)
def find_snapshot_by_database_and_time(engine, database_obj, snap_time):
snapshots = snapshot.get_all(
dx_session_obj.server_session, database=database_obj.reference
)
matches = []
for snapshot_obj in snapshots:
if str(snapshot_obj.latest_change_point.timestamp).startswith(
arguments["--timestamp"]
):
matches.append(snapshot_obj)
if len(matches) == 1:
print_debug(
'%s": Found one and only one match. This is good.\n%s'
% (engine["hostname"], matches[0]),
debug,
)
return matches[0]
elif len(matches) > 1:
print_debug(matches, debug)
raise DlpxException(
"%s: The time specified was not specific enough."
"More than one match found.\n" % (engine["hostname"])
)
else:
raise DlpxException(
"%s: No matches found for the time specified.\n" % (engine["hostname"])
)
def find_source_by_database(engine, 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:
raise DlpxException(
"%s: Did not find a source for %s. Exiting.\n"
% (engine["hostname"], database_obj.name)
)
elif len(source_obj) > 1:
raise DlpxException(
"%s: More than one source returned for %s. "
"Exiting.\n" % (engine["hostname"], database_obj.name + ". Exiting")
)
return source_obj
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
@run_async
def main_workflow(engine):
"""
This function actually runs the jobs.
Use the @run_async decorator to run this function asynchronously.
This allows us to run against multiple Delphix Engine simultaneously
engine: Dictionary containing engine information
"""
# Establish these variables as empty for use later
environment_obj = None
source_objs = None
jobs = {}
try:
# Setup the connection to the Delphix Engine
dx_session_obj.serversess(
engine["ip_address"], engine["username"], engine["password"]
)
group_obj = find_obj_by_name(
dx_session_obj.server_session, group, arguments["--target_grp"]
)
# Get the reference of the target environment.
print_debug("Getting environment for %s\n" % (host_name), debug)
# Get the environment object by the hostname
environment_obj = find_obj_by_name(
dx_session_obj.server_session, environment, host_name
)
except DlpxException as e:
print(
"\nERROR: Engine %s encountered an error while provisioning "
"%s:\n%s\n" % (engine["hostname"], arguments["--target"], e)
)
sys.exit(1)
print_debug(
"Getting database information for %s\n" % (arguments["--source"]), debug
)
try:
# Get the database reference we are copying from the database name
database_obj = find_obj_by_name(
dx_session_obj.server_session, database, arguments["--source"]
)
except DlpxException:
return
thingstodo = ["thingtodo"]
# reset the running job count before we begin
i = 0
try:
with dx_session_obj.job_mode(single_thread):
while len(jobs) > 0 or len(thingstodo) > 0:
arg_type = arguments["--type"].lower()
if len(thingstodo) > 0:
if arg_type == "oracle":
create_oracle_si_vdb(
engine,
jobs,
database_name,
group_obj,
environment_obj,
database_obj,
arguments["--prerefresh"],
arguments["--postrefresh"],
arguments["--prerollback"],
arguments["--postrollback"],
arguments["--configure-clone"],
)
elif arg_type == "ase":
create_ase_vdb(
engine,
server,
jobs,
group_obj,
database_name,
environment_obj,
database_obj,
)
elif arg_type == "mssql":
create_mssql_vdb(
engine,
jobs,
group_obj,
database_name,
environment_obj,
database_obj,
)
elif arg_type == "vfiles":
create_vfiles_vdb(
engine,
jobs,
group_obj,
database_name,
environment_obj,
database_obj,
arguments["--prerefresh"],
arguments["--postrefresh"],
arguments["--prerollback"],
arguments["--postrollback"],
arguments["--configure-clone"],
)
thingstodo.pop()
# get all the jobs, then inspect them
i = 0
for j in jobs.keys():
job_obj = job.get(dx_session_obj.server_session, jobs[j])
print_debug(job_obj, debug)
print_info(
engine["hostname"] + ": VDB Provision: " + 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
print_info("%s: %s jobs running." % (engine["hostname"], str(i)))
# If we have running jobs, pause before repeating the checks.
if len(jobs) > 0:
sleep(float(arguments["--poll"]))
except (DlpxException, JobError) as e:
print("\nError while provisioning %s:\n%s" % (database_name, e.message))
sys.exit(1)
def run_job():
"""
This function runs the main_workflow aynchronously against all the servers
specified
No arguments required for run_job().
"""
# 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")
try:
# For each server in the dxtools.conf...
for delphix_engine in dx_session_obj.dlpx_engines:
engine = dx_session_obj[delphix_engine]
# Create a new thread and add it to the list.
threads.append(main_workflow(engine))
except DlpxException as e:
print("Error encountered in main_workflow:\n%s" % (e))
sys.exit(1)
elif arguments["--all"] is False:
# Else if the --engine argument was given, test to see if the engine
# exists in dxtools.conf
if arguments["--engine"]:
try:
engine = dx_session_obj.dlpx_engines[arguments["--engine"]]
print_info(
"Executing against Delphix Engine: %s\n" % (arguments["--engine"])
)
except (DlpxException, RequestError, KeyError) as e:
raise DlpxException(
"\nERROR: Delphix Engine %s cannot be "
"found in %s. Please check your value "
"and try again. Exiting.\n"
% (arguments["--engine"], config_file_path)
)
else:
# Else search for a default engine in the dxtools.conf
for delphix_engine in dx_session_obj.dlpx_engines:
if dx_session_obj.dlpx_engines[delphix_engine]["default"] == "true":