forked from delphix/delphixpy-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_setup.py
executable file
·244 lines (205 loc) · 7.26 KB
/
engine_setup.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
#!/usr/bin/env python
"""
Adam Bowen - Jan 2016
This script configures the sysadmin user and configures domain0
Will come back and properly throw this with logging, etc
"""
from __future__ import print_function
import getopt
import logging
import signal
import sys
import time
import traceback
from os.path import basename
import untangle
from delphixpy.v1_6_0.delphix_engine import DelphixEngine
from delphixpy.v1_6_0.exceptions import HttpError
from delphixpy.v1_6_0.exceptions import JobError
from delphixpy.v1_6_0.web import domain
from delphixpy.v1_6_0.web import storage
from delphixpy.v1_6_0.web import user
from delphixpy.v1_6_0.web.vo import CredentialUpdateParameters
from delphixpy.v1_6_0.web.vo import DomainCreateParameters
from delphixpy.v1_6_0.web.vo import PasswordCredential
from delphixpy.v1_6_0.web.vo import User
from lib.GetSession import GetSession
VERSION = "v.2.3.005"
CONTENTDIR = "/u02/app/content"
def system_serversess(f_engine_address, f_engine_username, f_engine_password):
"""
Function to grab the server session
"""
server_session = DelphixEngine(
f_engine_address, f_engine_username, f_engine_password, "SYSTEM"
)
return server_session
def help():
print(
"\n"
+ basename(__file__)
+ " [-e <engine ip>] [-o <old sysadmin password] [-p <new sysadmin password]"
)
print(
"\n\nScript requires three parameters, the IP of the Delphix Engine, the initial sysadmin password to connect with, and the new sysadmin password you want to use"
)
print("-h - Prints this message")
print(
"-e <Delphix Engine IP> - Engine must be up, unconfigured, and console screen must be green"
)
print(
"-o <old sysadmin password> - will use this password to initially access the system"
)
print("-p <new sysadmin password> - will set the sysadmin user to this password")
print("-v - Print version information and exit")
sys.exit(2)
def logging_est():
"""
Establish Logging
"""
global debug
logging.basicConfig(
filename="landshark_setup.log",
format="%(levelname)s:%(asctime)s:%(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
print_info("Welcome to " + basename(__file__) + ", version " + VERSION)
global logger
debug = True
logger = logging.getLogger()
logger.setLevel(10)
print_info("Debug Logging is enabled.")
def on_exit(sig, func=None):
print_info("Shutdown Command Received")
print_info("Shutting down prime_setup.py")
sys.exit(0)
def print_debug(print_obj):
"""
DEBUG Log-level
"""
if debug == True:
print("DEBUG: " + str(print_obj))
logging.debug(str(print_obj))
def print_error(print_obj):
"""
ERROR Log-level
"""
print("ERROR: " + str(print_obj))
logging.error(str(print_obj))
def print_info(print_obj):
"""
INFO Log-level
"""
print("INFO: " + str(print_obj))
logging.info(str(print_obj))
def print_warning(print_obj):
"""
WARNING Log-level
"""
print("WARNING: " + str(print_obj))
logging.warning(str(print_obj))
def set_exit_handler(func):
signal.signal(signal.SIGTERM, func)
def time_elapsed():
elapsed_minutes = round((time.time() - time_start) / 60, +1)
return elapsed_minutes
def version():
print("Version: " + VERSION)
logging_est()
set_exit_handler(on_exit)
sys.exit(1)
def main(argv):
try:
logging_est()
global time_start
time_start = time.time()
dx_session_obj = GetSession()
engine_ip = ""
engine_pass = ""
old_engine_pass = ""
try:
opts, args = getopt.getopt(argv, "e:o:p:hv")
except getopt.GetoptError:
help()
for opt, arg in opts:
if opt == "-h":
help()
elif opt == "-e":
engine_ip = arg
elif opt == "-o":
old_engine_pass = arg
elif opt == "-p":
engine_pass = arg
elif opt == "-v":
version()
if engine_ip == "" or engine_pass == "" or old_engine_pass == "":
help()
dx_session_obj.serversess(engine_ip, "sysadmin", old_engine_pass, "SYSTEM")
dx_session_obj.server_wait()
sys_server = system_serversess(engine_ip, "sysadmin", old_engine_pass)
if user.get(sys_server, "USER-1").email_address == None:
print_info("Setting sysadmin's email address")
sysadmin_user = User()
sysadmin_user.email_address = "[email protected]"
user.update(sys_server, "USER-1", sysadmin_user)
print_info("Setting sysadmin's password")
sysadmin_credupdate = CredentialUpdateParameters()
sysadmin_credupdate.new_credential = PasswordCredential()
sysadmin_credupdate.new_credential.password = engine_pass
user.update_credential(sys_server, "USER-1", sysadmin_credupdate)
else:
print_info("sysadmin user has already been configured")
try:
sys_server = system_serversess(engine_ip, "sysadmin", engine_pass)
domain.get(sys_server)
print_info("domain0 already exists. Skipping domain0 creation.")
elapsed_minutes = time_elapsed()
print_info(
"Prime took " + str(elapsed_minutes) + " minutes to get this far."
)
sys.exit(7)
except HttpError as e:
device_list = storage.device.get_all(sys_server)
system_init_params = DomainCreateParameters()
system_init_params.devices = [
device.reference for device in device_list if not device.configured
]
print_info("Creating storage domain")
domain.set(sys_server, system_init_params)
while True:
try:
sys_server = system_serversess(engine_ip, "sysadmin", engine_pass)
domain.get(sys_server)
except:
break
print_info("Waiting for Delphix Engine to go down")
time.sleep(3)
dx_session_obj.serversess(engine_ip, "sysadmin", engine_pass, "SYSTEM")
dx_session_obj.server_wait()
except SystemExit as e:
sys.exit(e)
except HttpError as e:
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:
print_error("A job failed in the Delphix Engine")
print_error(e.job)
elapsed_minutes = time_elapsed()
print_info("Prime took " + str(elapsed_minutes) + " minutes to get this far.")
sys.exit(2)
except KeyboardInterrupt:
print_debug("You sent a CTRL+C to interrupt the process")
elapsed_minutes = time_elapsed()
print_info("Prime took " + str(elapsed_minutes) + " minutes to get this far.")
sys.exit(2)
except:
print_error(sys.exc_info()[0])
print_error(traceback.format_exc())
elapsed_minutes = time_elapsed()
print_info("Prime took " + str(elapsed_minutes) + " minutes to get this far.")
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])