Repository navigation
Expand file tree
/
Copy pathppdm_deploy.py
More file actions
1351 lines (1114 loc) · 46.4 KB
/
Copy pathppdm_deploy.py
File metadata and controls
1351 lines (1114 loc) · 46.4 KB
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 python3
"""PowerProtect Data Manager Deployment Automation Script.
This script automates the deployment of Dell PowerProtect Data Manager
in VMware vSphere environments using either ovftool or govc.
Author: Idan Kentor <idan.kentor@dell.com>
Copyright: [2026] [Idan Kentor]
Example Usage:
python ppdm_deploy.py -configfile ppdm-config-minimal.json
python ppdm_deploy.py -configfile ppdm-prod-config.json -vc -dd
python ppdm_deploy.py -configfile ppdm-prod-config.json -vc -dd -ppdm
python ppdm_deploy.py -configfile ppdm-config.json -vc -dd -ppdm -cross
python ppdm_deploy.py -configfile ppdm-prod-config.json -skipova
python ppdm_deploy.py -configfile ppdm_test.json -justova
python ppdm_deploy.py -configfile ppdm-prod-config.json -tool govc
"""
import argparse
import datetime
import json
import logging
import logging.handlers
import os
import platform
import socket
import subprocess
import sys
import tempfile
import time
import requests
import urllib3
urllib3.disable_warnings()
logger = logging.getLogger("ppdm_deploy")
def setup_logger(log_file):
"""Configure logging to file only and log execution start"""
max_bytes = 10 * 1024 * 1024 # 10MB
log_backup_count = 5
logger.setLevel(logging.INFO)
logger.handlers.clear()
try:
file_handler = logging.handlers.RotatingFileHandler(
log_file,
maxBytes=max_bytes,
backupCount=log_backup_count
)
file_formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - "
"%(funcName)s:%(lineno)d - %(message)s"
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
separator = "=" * 80
logger.info(separator)
logger.info("PPDM Deployment Script Started")
logger.info("Execution Time: %s",
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
logger.info("Script: PowerProtect Data Manager Deployment Automation")
logger.info("Command: %s", " ".join(sys.argv))
logger.info(separator)
except (OSError, IOError) as e:
print(f"-> Could not create log file: {e}")
return logger
def get_args():
"""Gets command line args from the user"""
parser = argparse.ArgumentParser(
description="Script to automate PowerProtect Data Manager deployment"
)
parser.add_argument(
"-configfile", "-c", "--config-file",
required=True,
dest="config_file",
help="Full path to the JSON config file",
)
ova_group = parser.add_mutually_exclusive_group()
ova_group.add_argument(
"-skipova", "--skip-ova",
dest="skip_ova",
action="store_true",
help="Skip OVA deployment",
)
ova_group.add_argument(
"-justova", "--just-ova",
dest="just_ova",
action="store_true",
help="Stop after OVA deployment",
)
parser.add_argument(
"-vc", "--register-vcenter",
dest="register_vc",
action="store_true",
help="Register vCenter in PPDM",
)
parser.add_argument(
"-novcplugin", "--prevent-plugin-install",
dest="no_vc_plugin",
action="store_false",
help="Prevent vCenter plugin deployment",
)
parser.add_argument(
"-dd", "--add-dd",
required=False,
dest="add_dd",
action="store_true",
help="Add Data Domain to PPDM",
)
parser.add_argument(
"-ppdm", "--connect-ppdm",
required=False,
dest="connect_peer",
action="store_true",
help="Connect remote PPDM system",
)
parser.add_argument(
"-cross", "--bi-directional",
dest="cross_connect",
action="store_true",
help="Configure bi-directional communication "
"between the two PPDM systems",
)
parser.add_argument(
"-tool", "--deployment-tool",
choices=["ovftool", "govc"],
default="ovftool",
help="Choose deployment tool: ovftool (default) or govc"
)
args = parser.parse_args()
return args
def read_config(config_file):
"""Reads config file, validates params and assigns to the config dict"""
with open(config_file, "r", encoding="utf-8") as file_handle:
try:
config = json.load(file_handle)
except json.decoder.JSONDecodeError as error:
print(f"-> Cannot parse JSON config file: {error}")
logger.error("Cannot parse JSON config file: %s", error)
raise SystemExit(1) from error
for key in list(config.keys()):
if key.startswith("_comment"):
config.pop(key)
config["ppdmIpV6"] = config.get("ppdmIpV6", False)
if config["ppdmIpV6"]:
ipv6_netmask = config.get("ppdmIpV6Netmask")
ipv6_gateway = config.get("ppdmIpV6Gateway")
if not ipv6_netmask or not ipv6_gateway:
print("-> Missing IPv6 configuration parameters")
logger.error("Missing IPv6 configuration parameters")
raise SystemExit(1)
if not config["ppdmIpV6"]:
config["ppdmIpV4"] = config.get("ppdmIpV4", False)
if not config["ppdmIpV4"]:
print("-> Missing PPDM IPv4 address")
logger.error("Missing PPDM IPv4 address")
raise SystemExit(1)
config["ppdmIpV4Netmask"] = config.get("ppdmIpV4Netmask", False)
config["ppdmIpv4Gateway"] = config.get("ppdmIpv4Gateway", False)
if not config["ppdmIpV4Netmask"] or not config["ppdmIpv4Gateway"]:
print("-> Missing IPv4 configuration parameters")
logger.error("Missing IPv4 configuration parameters")
raise SystemExit(1)
if not config.get("ppdmDatastore"):
print("-> No Datastore provided, specify DS for PPDM")
logger.error("No Datastore provided, specify DS for PPDM")
raise SystemExit(1)
if not config.get("ppdmMgmtNetwork"):
print("-> Management Network Port Group must be specified")
logger.error("Management Network Port Group must be specified")
raise SystemExit(1)
if not config.get("ntpServers") or not config.get("dnsServers"):
print("-> Missing DNS or NTP IP addresses")
logger.error("Missing DNS or NTP IP addresses")
raise SystemExit(1)
config["ntpServers"] = config["ntpServers"][0].split(", ")
config["dnsServers"] = config["dnsServers"][0].split(", ")
config["license_file"] = config.get("license_file", "trial")
config["logFile"] = config.get("logFile", "ppdm_deploy.log")
for encrypt_type in ("protectionEncryption", "replicationEncryption"):
config[encrypt_type] = config.get(encrypt_type, True)
if not isinstance(config[encrypt_type], bool):
print(f"-> invalid value for {encrypt_type}")
logger.error("invalid value for %s", encrypt_type)
raise SystemExit(1)
for asset in ("vc", "dd", "peerPpdm"):
fqdn_ip = config.get(f"{asset}FQDNorIP")
user = config.get(f"{asset}User")
password = config.get(f"{asset}Password")
valid = all((fqdn_ip, user, password))
config[f"{asset}Valid"] = bool(valid)
if not valid:
continue
if isinstance(fqdn_ip, str) and fqdn_ip and fqdn_ip[0].isdigit():
parts = fqdn_ip.split(".")
segment = parts[3] if len(parts) > 3 else parts[-1]
config[f"{asset}NiceName"] = f"{asset.upper()}{segment}"
else:
if isinstance(fqdn_ip, str):
config[f"{asset}NiceName"] = fqdn_ip.split(".")[0]
else:
config[f"{asset}NiceName"] = str(fqdn_ip)
smtp_keys = ("smtpMailServer", "smtpMailFrom", "smtpPort")
if all(key in config for key in smtp_keys):
config["smtp"] = True
else:
config["smtp"] = False
if config.get("smtpUser") and config.get("smtpPassword"):
config["smtpAuth"] = True
else:
config["smtpAuth"] = False
if not config.get("autoSupport"):
if config["smtp"]:
config["autoSupport"] = True
else:
config["autoSupport"] = False
return config
def create_ovftool_command(config):
"""Forms the required ovftool command"""
logger.info("Creating OVF tool command")
ovf_tool = config["ovfToolLocation"]
ppdm_exec = (
f'{ovf_tool} --noDestinationSSLVerify --skipManifestCheck '
f'--acceptAllEulas --powerOn --name="{config["ppdmVmName"]}" '
f'--diskMode=thin --datastore={config["ppdmDatastore"]} '
f'--net:"VM Network"="{config["ppdmMgmtNetwork"]}" '
)
if not config["ppdmIpV6"]:
ppdm_exec += (
f'--prop:vami.ip0.brs={config["ppdmIpV4"]} '
f'--prop:vami.netmask0.brs="{config["ppdmIpV4Netmask"]}" '
f'--prop:vami.gateway.brs="{config["ppdmIpv4Gateway"]}" '
)
else:
ppdm_exec += (
f'--prop:vami.ip0.brs={config["ppdmIpV6"]} '
f'--prop:vami.netmask0.brs="{config["ppdmIpV6Netmask"]}" '
f'--prop:vami.gateway.brs="{config["ppdmIpV6Gateway"]}" '
)
ppdm_exec += (
f'--prop:vami.DNS.brs="{", ".join(config["dnsServers"])}" '
f'--prop:vami.fqdn.brs="{config["ppdmFQDN"]}" '
f'--deploymentOption="{config["platform"]}" '
f'"{config["ppdmOVALocation"]}" vi://'
f'"{config["vcUser"]}":"{config["vcPassword"]}"@'
f'{config["vcFQDNorIP"]}/{config["datacenter"]}/'
f'host/{config["esxCluster"]}/'
)
safe_exec = ppdm_exec.replace(config["vcPassword"], "*** REDACTED ***")
logger.info("OVF Tool command: %s", safe_exec)
return ppdm_exec
def create_govc_command(config):
"""Generate govc command with JSON spec for PPDM deployment"""
logger.info("Creating govc command")
spec = {
"DiskProvisioning": "thin",
"IPAllocationPolicy": "fixedPolicy",
"IPProtocol": "IPv4",
"MarkAsTemplate": False,
"Name": config["ppdmVmName"],
"NetworkMapping": [
{
"Name": "VM Network",
"Network": config["ppdmMgmtNetwork"]
}
],
"PowerOn": True,
"WaitForIP": False
}
if not config["ppdmIpV6"]:
spec["PropertyMapping"] = [
{
"Key": "vami.ip0.PPDM",
"Value": config["ppdmIpV4"]
},
{
"Key": "vami.netmask0.PPDM",
"Value": config["ppdmIpV4Netmask"]
},
{
"Key": "vami.gateway.brs",
"Value": config["ppdmIpv4Gateway"]
}
]
else:
spec["PropertyMapping"] = [
{
"Key": "vami.ip0.PPDM",
"Value": config["ppdmIpV6"]
},
{
"Key": "vami.netmask0.PPDM",
"Value": config["ppdmIpV6Netmask"]
},
{
"Key": "vami.gateway.brs",
"Value": config["ppdmIpV6Gateway"]
}
]
spec["PropertyMapping"].extend([
{
"Key": "vami.DNS.brs",
"Value": ", ".join(config["dnsServers"])
},
{
"Key": "vami.fqdn.brs",
"Value": config["ppdmFQDN"]
},
{
"Key": "deploymentOption",
"Value": config["platform"]
}
])
try:
with tempfile.NamedTemporaryFile(
mode='w', suffix='ppdm_govc_temp', delete=False
) as temp_file:
json.dump(spec, temp_file, indent=2)
spec_file = temp_file.name
except (TypeError, ValueError) as error:
logger.error("Failed to create govc spec: %s", error)
raise SystemExit(1) from error
govc_cmd = (
f'govc import.ova -options={spec_file} '
f'-ds={config["ppdmDatastore"]} '
f'-host={config["esxCluster"]} '
f'{config["ppdmOVALocation"]}'
)
logger.debug("Govc command: %s", govc_cmd)
return govc_cmd, spec_file
def exec_ova_provisioning(ovf_exec):
"""Executes ovftool deployment command"""
exit_code = os.system(ovf_exec)
if exit_code == 0:
print("-> OVA deployment completed successfully")
logger.info("OVA deployment completed successfully")
else:
print("-> OVA deployment failed")
logger.error("OVA deployment failed")
raise SystemExit(1)
def exec_govc_provisioning(govc_exec, spec_file):
"""Executes govc deployment command"""
try:
exit_code = os.system(govc_exec)
if exit_code == 0:
print("-> OVA deployment completed successfully")
logger.info("Govc OVA deployment completed successfully")
else:
print("-> OVA deployment failed")
logger.error("Govc OVA deployment failed")
raise SystemExit(1)
finally:
try:
os.unlink(spec_file)
logger.debug("Cleaned up temp spec file: %s", spec_file)
except OSError as e:
logger.warning("Failed to cleanup temp file %s: %s", spec_file, e)
def tcp_check(ip_address):
"""Perform TCP connect check to a given IP address"""
port = 443
timeout = 3
try:
with socket.create_connection((ip_address, port), timeout=timeout):
return True
except OSError:
return False
def check_connectivity(ip_address, ppdm_api_timeout):
"""Generic call to check connectivity to a given IP address"""
interval = 10
start = time.monotonic()
if platform.system().lower() == "windows":
ping_cmd = f"ping {ip_address} -n 3"
else:
ping_cmd = f"ping {ip_address} -c 3"
while True:
if (time.monotonic() - start) > ppdm_api_timeout:
return False
result = subprocess.run(
ping_cmd, shell=True, stdout=subprocess.PIPE, check=False
)
if result.returncode == 0:
return True
if tcp_check(ip_address):
return True
time.sleep(interval)
def check_api_accessibility(ppdm_ip, ppdm_api_timeout):
"""Continuously checks if PPDM API is available"""
api_endpoint = f"https://{ppdm_ip}/eula.html"
interval = 30
start = time.monotonic()
while True:
if (time.monotonic() - start) > ppdm_api_timeout:
print("PPDM API check timed out. Exiting")
logger.error("PPDM API check timed out. Exiting")
raise SystemExit(1)
if init_rest_call("GET", api_endpoint, None, None, None, True):
return True
print("---> PPDM API is unreachable. Retrying")
logger.info("PPDM API is unreachable. Retrying")
time.sleep(interval)
def sanitize_payload_for_logging(payload_str, uri):
"""Sanitize sensitive data in payload for logging"""
if not payload_str:
return payload_str
sensitive_endpoints = ["/login", "/licenses", "/smtp"]
if any(endpoint in uri for endpoint in sensitive_endpoints):
return "*** REDACTED ***"
if "configurations" in uri:
try:
payload_dict = json.loads(payload_str)
safe_payload = payload_dict.copy()
for user in safe_payload.get("osUsers", []):
if "password" in user:
user["password"] = "*** REDACTED ***"
if "newPassword" in user:
user["newPassword"] = "*** REDACTED ***"
if "applicationUserPassword" in safe_payload:
safe_payload["applicationUserPassword"] = "*** REDACTED ***"
for asset in ["dd", "vc", "peerPpdm"]:
if f"{asset}Password" in safe_payload:
safe_payload[f"{asset}Password"] = "*** REDACTED ***"
return json.dumps(safe_payload)
except (json.JSONDecodeError, AttributeError):
return payload_str
return payload_str
def init_rest_call(verb, uri, token, payload=None, params=None, deploy=None):
"""Generic function for REST calls"""
code = {200, 201, 202, 204}
verify = False
timeout = 90
if uri.endswith("/login") or deploy:
headers = {"Content-Type": "application/json"}
else:
headers = {"Content-Type": "application/json",
"Authorization": f"Bearer {token}"}
# Log headers safely - mask bearer token if present
safe_headers = headers.copy()
if token:
safe_headers["Authorization"] = "Bearer ***"
logger.info("REST Call: %s %s", verb, uri)
logger.info("Headers: %s", safe_headers)
payload_str = json.dumps(payload)
logger.info("Payload: %s", sanitize_payload_for_logging(payload_str, uri))
try:
if verb.lower() == "get":
response = requests.get(
uri,
headers=headers,
params=params,
verify=verify,
timeout=timeout
)
else:
response = requests.request(
verb,
uri,
headers=headers,
params=params,
data=payload_str,
verify=verify,
timeout=timeout,
)
response.raise_for_status()
except requests.exceptions.ConnectionError as error:
if deploy:
return False
print(f"->Error Connecting to {uri}: {error}")
logger.error("Error Connecting to %s: %s", uri, error)
raise SystemExit(1) from error
except requests.exceptions.Timeout as error:
print(f"-> Connection timed out {urllib3}: {error}")
logger.error("Connection timed out %s: %s", urllib3, error)
raise SystemExit(1) from error
except requests.exceptions.RequestException as error:
if deploy and response.status_code in (401, 502):
return False
logger.error("The call %s %s failed with exception: %s",
response.request.method, response.url, error)
logger.info("Response Code: %s", response.status_code)
logger.debug("Response: %s", response.text)
if response.status_code not in code:
raise requests.exceptions.HTTPError(
f"Failed to query {uri}, code: {response.status_code}, "
f"body: {response.text}"
)
if not response.content:
return True
if uri.endswith("/login"):
return response.json()["access_token"]
try:
return response.json()
except ValueError:
return response.content
def check_deployment(ppdm_uri, token):
"""Validate that PPDM is ready for deployment"""
nodes_uri = f"{ppdm_uri}/nodes"
nodes = init_rest_call("GET", nodes_uri, token)
if not nodes or "content" not in nodes:
logger.error("Cannot query the Data Manager server. Exiting...")
raise SystemExit("Cannot query the Data Manager server. Exiting...")
if nodes["content"][0]["status"] != "PENDING":
logger.error("PPDM is not available for deployment. Exiting...")
raise SystemExit("PPDM is not available for deployment. Exiting...")
print("---> PPDM is deployment ready")
logger.info("PPDM is deployment ready")
return nodes["content"][0]["id"]
def get_deploy_config(ppdm_uri, token, node_id):
"""Retrieve PPDM deployment configuration"""
config_uri = f"{ppdm_uri}/configurations"
desired_config = False
deploy_config = init_rest_call("GET", config_uri, token)
if not deploy_config or "content" not in deploy_config:
logger.error("Cannot query the Data Manager server. Exiting...")
raise SystemExit("Cannot query the Data Manager server. Exiting...")
for config_item in deploy_config.get("content"):
if config_item.get("nodeId") == node_id:
desired_config = config_item
if not desired_config:
print("Could not detect a valid configuration. Exiting.")
logger.error("Could not detect a valid configuration. Exiting.")
raise SystemExit(1)
return desired_config
def accept_eula(eula_type, ppdm_uri, token):
"""Accept PPDM EULAs by type"""
eula_uri = f"{ppdm_uri}/eulas/{eula_type}"
payload = {"accepted": True}
response = init_rest_call("PATCH", eula_uri, token, payload)
if not response.get("accepted"):
print(f"{eula_type} EULA could not be accepted, exiting...")
logger.error("%s EULA could not be accepted, exiting...", eula_type)
raise SystemExit(1)
print(f"---> {eula_type} EULA accepted")
logger.info("%s EULA accepted", eula_type)
return True
def apply_license(license_file, ppdm_uri, token):
"""Apply PPDM license from file"""
if license_file.strip().lower() == "trial":
print("-> Using Trial license")
logger.info("Using Trial license")
return True
try:
with open(license_file, "r", encoding="utf-8") as file_handle:
license_content = file_handle.read().strip()
except (OSError, FileNotFoundError):
print("-> Could not read license file. Using Trial license")
logger.warning("Could not read license file. Using Trial license")
return True
if not license_content:
print("-> License file is empty. Using Trial license")
logger.warning("License file is empty. Using Trial license")
return True
license_uri = f"{ppdm_uri}/licenses"
payload = {"type": "CAPACITY", "key": license_content}
response = init_rest_call("POST", license_uri, token, payload)
if response.get("status") == "VALID":
print("-> Using Capacity license")
logger.info("Using Capacity license")
return True
print("-> License not accepted. Using Trial license")
logger.warning("License not accepted. Using Trial license")
return False
def config_smtp(config, ppdm_uri, token):
"""Apply SMTP settings"""
smtp_uri = f"{ppdm_uri}/smtp"
payload = {
"mailServer": config["smtpMailServer"],
"mailFrom": config["smtpMailFrom"],
"port": config["smtpPort"],
}
if config["smtpAuth"]:
payload["username"] = config["smtpUser"]
payload["password"] = config["smtpPassword"]
response = init_rest_call("POST", smtp_uri, token, payload)
if "id" in response:
return True
print("Could not apply SMTP settings. Exiting")
logger.error("Could not apply SMTP settings. Exiting")
return False
def apply_encryption_settings(config, ppdm_uri, token):
"""Apply encryption settings"""
encr_uri = f"{ppdm_uri}/common-settings/ENCRYPTION_SETTING"
encr_payload = {"id": "ENCRYPTION_SETTING"}
protect_encrypt = {
"name": "enableProtectionEncryption",
"value": str(config["protectionEncryption"]).lower(),
"type": "BOOLEAN",
}
replication_encrypt = {
"name": "enableReplicationEncryption",
"value": str(config["replicationEncryption"]).lower(),
"type": "BOOLEAN",
}
encr_payload["properties"] = [protect_encrypt, replication_encrypt]
response = init_rest_call("PUT", encr_uri, token, encr_payload)
try:
if response["id"] != encr_payload["id"]:
print("Could not apply encryption settings. Exiting")
logger.error("Could not apply encryption settings. Exiting")
raise SystemExit(1)
for setting in response["properties"]:
if setting["name"] == "enableProtectionEncryption":
if config["replicationEncryption"] != bool(setting["value"]):
print("Could not apply encryption settings. Exiting")
logger.error(
"Could not apply encryption settings. Exiting"
)
raise SystemExit(1)
elif setting["name"] == "enableReplicationEncryption":
if config["protectionEncryption"] != bool(setting["value"]):
print("Could not apply encryption settings. Exiting")
logger.error(
"Could not apply encryption settings. Exiting"
)
raise SystemExit(1)
except KeyError:
print("Could not apply encryption settings. Exiting")
logger.error("Could not apply encryption settings. Exiting")
raise SystemExit(1) from KeyError
return True
def get_time_zone(config, ppdm_uri, token):
"""Determine the time zone"""
local_tz = datetime.datetime.now().astimezone().tzinfo
local_tz_name = str(local_tz).split(" ", maxsplit=1)[0]
config["timeZone"] = (
config.get("timeZone") or config.get("time_zone") or local_tz_name
)
timezones = {"eastern": "EST",
"et": "EST",
"central": "CST6CDT",
"ct": "CST6CDT",
"pacific": "PST8PDT",
"pt": "PST8PDT",
"etc": "Etc/UTC"}
normalized = timezones.get(config["timeZone"].lower(), config["timeZone"])
config["timeZone"] = normalized
timezone_uri = f"{ppdm_uri}/timezones"
tz_list = init_rest_call("GET", timezone_uri, token)
content = tz_list.get("content") if isinstance(tz_list, dict) else None
if isinstance(content, list):
for tz in content:
tz_name = tz.get("name", "")
if config["timeZone"] in tz_name:
config["timeZone"] = tz.get("id", config["timeZone"])
break
print(f"-> Time zone detected: {config['timeZone']}")
logger.info("Time zone detected: %s", config['timeZone'])
return config
def build_deployment_config(config, deploy_config):
"""Form the PPDM deployment config"""
deploy_config["timeZone"] = config["timeZone"]
for network in deploy_config["networks"]:
if "nslookupSuccess" in network:
if network["nslookupSuccess"]:
print("-> Name resolution completed successfully")
logger.info("Name resolution completed successfully")
else:
print("-> Warning: name resolution issues")
logger.warning("Warning: name resolution issues")
break
deploy_config["ntpServers"] = config["ntpServers"]
for user in deploy_config.get("osUsers"):
user["password"] = config[user["userName"] + "DefaultPwd"]
user["newPassword"] = config["ppdmAdminPwd"]
deploy_config["applicationUserPassword"] = config["ppdmAdminPwd"]
if config["autoSupport"]:
deploy_config["autoSupport"] = True
deploy_config["gettingStartedCompleted"] = True
return deploy_config
def bootstrap_ppdm_deployment(ppdm_uri, token, deploy_config):
"""Initiates the PPDM deployment"""
deploy_uri = f"{ppdm_uri}/configurations/{deploy_config['id']}"
deploy_response = init_rest_call("PUT", deploy_uri, token, deploy_config)
return deploy_response
def monitor_deploy_activity(
ppdm_uri, token, deploy_config_id, ppdm_deploy_timeout, admin_pwd
):
"""Monitors deployment operation"""
monitor_uri = f"{ppdm_uri}/configurations/{deploy_config_id}/config-status"
poll_interval = 5
retry_interval = 30
start = time.monotonic()
requires_auth = False
username = "admin"
print(f"---> Deploying configuration {deploy_config_id}")
logger.info("Deploying configuration %s", deploy_config_id)
while True:
if (time.monotonic() - start) > ppdm_deploy_timeout:
break
response = init_rest_call("GET", monitor_uri, token)
if not response:
if not requires_auth:
response = init_rest_call(
"GET", monitor_uri, token, None, None, True
)
else:
token = authenticate(ppdm_uri, username, admin_pwd)
try:
response = init_rest_call(
"GET", monitor_uri, token, None, None, True
)
except requests.exceptions.RequestException:
time.sleep(retry_interval)
response = init_rest_call(
"GET", monitor_uri, token, None, None, True
)
requires_auth = True
status = response.get("status")
percent_complete = response.get("percentageCompleted", 0)
if status == "SUCCESS":
print(f"---> Deployment status {status} {percent_complete}%")
logger.info(
"Deployment status %s %s%%", status, percent_complete
)
return True
if status == "ERROR":
print(f"->Action failed: {json.dumps(response)}")
logger.error("Action failed: %s", json.dumps(response))
break
print(f"---> Deployment status {status} {percent_complete}%")
logger.info(
"Deployment status %s %s%%", status, percent_complete
)
time.sleep(poll_interval)
return False
def authenticate(ppdm_uri, username, password):
"""Login to PowerProtect Data Manager"""
login_uri = f"{ppdm_uri}/login"
login_payload = {"username": username, "password": password}
token = init_rest_call("POST", login_uri, None, login_payload)
return token
def accept_certificate(asset_type, config, ppdm_uri, token):
"""Accept host certificate"""
certs_uri = f"{ppdm_uri}/certificates"
params = {
"host": config[asset_type + "FQDNorIP"],
"port": config[asset_type + "Port"],
"type": "HOST",
}
certs = init_rest_call("GET", certs_uri, token, None, params)
if not certs or not isinstance(certs, list):
print(f"-> No certificate response for {asset_type.upper()}")
logger.warning("No certificate response for %s", asset_type.upper())
return False
cert = certs[0]
cert_id = cert.get("id")
if not cert_id:
print(f"-> Certificate response missing 'id' for {asset_type.upper()}")
logger.warning(
"Certificate response missing 'id' for %s", asset_type.upper()
)
return False
if cert.get("state") == "ACCEPTED":
return True
cert["state"] = "ACCEPTED"
cert_upd_uri = f"{certs_uri}/{cert_id}"
cert = init_rest_call("PUT", cert_upd_uri, token, cert)
if isinstance(cert, dict):
if cert.get("state") == "ACCEPTED":
return True
elif bool(cert):
return True
print(f"Cannot add {asset_type.upper()}. "
f"Could not accept certificate")
logger.error(
f"Cannot add {asset_type.upper()}. Could not accept certificate"
)
return False
def add_credentials(asset_type, config, ppdm_uri, token):
"""Add credentials for a given asset source type"""
asset_type_alt = None
creds_uri = f"{ppdm_uri}/credentials"
if asset_type == "VCENTER":
asset_type_alt = "vc"
elif asset_type == "DATADOMAIN":
asset_type_alt = "dd"
elif asset_type == "POWERPROTECT":
asset_type_alt = "peerPpdm"
payload = {"type": asset_type,
"name": config[asset_type_alt + "NiceName"],
"username": config[asset_type_alt + "User"],
"password": config[asset_type_alt + "Password"]}
response = init_rest_call("POST", creds_uri, token, payload)
if "id" in response:
return response["id"]
return False
def config_auto_support(ppdm_uri, token):
"""Configure AutoSupport"""
support_uri = f"{ppdm_uri}/common-settings/TELEMETRY_SETTING"
response = init_rest_call("GET", support_uri, token)
if not isinstance(response, dict):
print("-> AutoSupport could not be configured")
logger.error("AutoSupport could not be configured")
return False
properties = response.get("properties")
if not isinstance(properties, list) or not response.get("id"):
print("-> AutoSupport could not be configured")
logger.error("AutoSupport could not be configured")
return False
for element in properties:
if element.get("name") == "transportType":
element["value"] = "EMAIL"
response.pop("_links", None)
payload = response
response = init_rest_call("PUT", support_uri, token, payload)
if response.get("id"):
print("-> AutoSupport configured successfully")
logger.info("AutoSupport configured successfully")
return True
print("-> AutoSupport could not be configured")
logger.error("AutoSupport could not be configured")
return False