-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Teardowndebug #26
Teardowndebug #26
Changes from 25 commits
e4af557
a8bfa49
1a9829c
3b8733a
6498acc
35c71e5
487a17b
97fba27
11585ba
a002f36
1e63cec
a4b0728
c0413a4
e178657
30650ad
43585fb
56f6a10
9e0d91c
5a12d61
62b03b7
3c3a210
fa997dc
0312028
1c36302
c81fda8
50eeb9f
d1a20f2
3bd9812
fb30370
90860eb
25ade05
1000180
3818b05
3194c5a
6eabbf1
32b9e6d
4232efb
b471401
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,7 @@ def __init__(self, resource_name, resource_alias=''): | |
|
||
self.attributes = self.details.ResourceAttributes | ||
# If there is an attribute named 'model' take its value (exist in shells), otherwise take the family's model | ||
if self.attribute_exist('Model'): | ||
self.model = self.get_attribute('Model') | ||
else: | ||
self.model = self.details.ResourceModelName | ||
|
||
self.model = self.get_attrib_value_ending_in_model() | ||
self.alias = resource_alias | ||
|
||
# ----------------------------------------- | ||
|
@@ -45,17 +41,24 @@ def has_command(self, command_name): | |
def attribute_exist(self, attribute_name): | ||
attribute_name = attribute_name.lower() | ||
for attribute in self.attributes: | ||
if attribute.Name.lower() == attribute_name: | ||
if attribute.Name.lower() == attribute_name or attribute.Name.lower().endswith('.' + attribute_name): | ||
return True | ||
return False | ||
|
||
# ----------------------------------------- | ||
# ----------------------------------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function is not needed anymore |
||
def get_attrib_value_ending_in_model(self): | ||
for attribute in self.attributes: | ||
if attribute.Name.endswith('.Model'): | ||
return str(attribute.Value) | ||
return str(self.details.ResourceModelName) | ||
|
||
# ----------------------------------------- | ||
# ----------------------------------------- | ||
def get_attribute(self, attribute_name): | ||
attribute_name = attribute_name.lower() | ||
for attribute in self.attributes: | ||
if attribute.Name.lower() == attribute_name: | ||
if attribute.Name.lower() == attribute_name or attribute.Name.lower().endswith('.' + attribute_name): | ||
if attribute.Type == 'Password': | ||
decrypted = self.api_session.DecryptPassword(attribute.Value) | ||
return decrypted.Value | ||
|
@@ -66,11 +69,17 @@ def get_attribute(self, attribute_name): | |
# ----------------------------------------- | ||
# ----------------------------------------- | ||
def set_attribute_value(self, attribute_name, attribute_value): | ||
# if caller passes ending string of name, need to handle not knowing prefix | ||
try: | ||
self.api_session.SetAttributeValue(resourceFullPath=self.name, attributeName=attribute_name, | ||
attributeValue=attribute_value) | ||
attribute_name = attribute_name.lower() | ||
for attribute in self.attributes: | ||
if attribute.Name.lower() == attribute_name or attribute.Name.lower().endswith('.' + attribute_name): | ||
self.api_session.SetAttributeValue(resourceFullPath=self.name, | ||
attributeName=attribute.Name, | ||
attributeValue=attribute_value) | ||
return | ||
except CloudShellAPIError as error: | ||
raise QualiError(self.name, "Attribute: " + attribute_name + " not found. " + error.message) | ||
raise QualiError(self.name, "Attribute named or ending-with: " + attribute_name + " not found. " + error.message) | ||
|
||
# ----------------------------------------- | ||
# implement the command to get the neighbors and their ports | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ | |
from cloudshell.core.logger.qs_logger import * | ||
from cloudshell.helpers.scripts import cloudshell_scripts_helpers as helpers | ||
from os.path import * | ||
from time import gmtime, strftime | ||
import smtplib | ||
import socket | ||
from email.mime.text import MIMEText | ||
|
||
|
||
SEVERITY_INFO = 20 | ||
|
@@ -20,9 +24,10 @@ def __init__(self, reservation_id, logger): | |
""":type : logging.Logger""" | ||
self.api_session = helpers.get_api_session() | ||
self.id = reservation_id | ||
|
||
self.owner = helpers.get_reservation_context_details().owner_user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the get_reservation_context_details result should be saved into a variable and then from there, you can take the owner and environment instead of calling this function twice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, now that we have the environmentPath, i would save it as well (self.environmentPath =..) |
||
self.Blueprint_name = helpers.get_reservation_context_details().environment_name | ||
|
||
if self.Blueprint_name == '': | ||
raise QualiError("Blueprint name empty (from env name)") | ||
|
||
full_path = None | ||
tp = self.api_session.GetActiveTopologyNames() | ||
|
@@ -53,19 +58,41 @@ def _write_message_to_output(self, message, severity_level=SEVERITY_INFO): | |
self.api_session.WriteMessageToReservationOutput(self.id, '<font color="red">' + message + '</font>') | ||
|
||
# ---------------------------------- | ||
def report_error(self, error_message, log_message=None, raise_error=True, write_to_output_window=False): | ||
def report_error(self, error_message, log_message=None, raise_error=True, write_to_output_window=False, | ||
send_email=False): | ||
""" | ||
Report on an error to the log file, output window is optional.There is also an option to raise the error up | ||
:param str error_message: The error message you would like to present | ||
:param str log_message: The error message you would like to write to the log. Keep None to use the message param | ||
:param bool raise_error: Do you want to throw an exception | ||
:param bool write_to_output_window: Would you like to write the message to the output window | ||
""" | ||
|
||
emailresult = '' | ||
if raise_error: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these 2 ifs can be one |
||
if send_email: | ||
emailOwner = False | ||
try: | ||
emailSubject = str(self.Blueprint_name) + ' / ' + str(self.owner) | ||
emailBody = "Sandbox: " + str(self.Blueprint_name) + "\n" + \ | ||
"Owner: " + str(self.owner) + "\n\n" | ||
sb_owner = str(self.owner) | ||
except: | ||
emailSubject = "Catastrophic ERROR in sandbox." | ||
emailBody = str(os.environ) + "\n\n" | ||
sb_owner = "Owner Unknown" | ||
|
||
if log_message: | ||
emailBody += "LogMsg: " + log_message + "\n\n" | ||
if error_message: | ||
emailBody += "ErrMsg: " + error_message + "\n\n" | ||
emailresult = self.emailalert(emailSubject, emailBody, owner=sb_owner, ishtml=False, emailOwner=emailOwner) + "\n" | ||
|
||
if self._logger: | ||
if log_message: | ||
self._logger.error(log_message) | ||
self._logger.error(emailresult + log_message) | ||
else: | ||
self._logger.error(error_message) | ||
self._logger.error(emailresult + error_message) | ||
if write_to_output_window: | ||
self._write_message_to_output(error_message, SEVERITY_ERROR) | ||
if raise_error: | ||
|
@@ -87,6 +114,51 @@ def report_info(self, message, log_message=None, write_to_output_window=False): | |
if write_to_output_window: | ||
self._write_message_to_output(message, SEVERITY_INFO) | ||
|
||
# ---------------------------------- | ||
def emailalert(self, subject, body, owner, ishtml=False, emailOwner=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change it to private (_email_alert) |
||
try: | ||
globalsresource = self.get_config_set_pool_resource() | ||
host = str(globalsresource.get_attribute("ConfigPool_SMTP_Server")) | ||
port = str(globalsresource.get_attribute("ConfigPool_SMTP_port")) | ||
emailfrom = str(globalsresource.get_attribute("ConfigPool_SMTP_from")) | ||
emailto = emailfrom | ||
emailcc = '' | ||
emailbcc = '' | ||
|
||
if emailOwner: | ||
try: | ||
emailto = str(self.api_session.GetUserDetails(owner).Email) | ||
emailbcc = emailfrom | ||
body += "----\n A copy of this email was also sent to our support staff." | ||
except: | ||
emailbcc = '' | ||
emailto = emailfrom | ||
|
||
try: | ||
if ishtml: | ||
emsg = MIMEText(body + '\n\n', 'html') | ||
else: | ||
emsg = MIMEText(body + '\n\n', 'plain') | ||
|
||
emsg['Subject'] = subject | ||
emsg['From'] = emailfrom | ||
emsg['To'] = ",".join([emailto]) | ||
emsg['CC'] = "" | ||
emsg.preamble = subject | ||
tolist = emailto.split(",") + emailcc.split(",") + emailbcc.split(",") | ||
mailer = smtplib.SMTP(host=host, port=port) | ||
mailer.sendmail(emailfrom, tolist, emsg.as_string()) | ||
return "Emailed OK" | ||
|
||
except smtplib.SMTPException as e: | ||
# cannot post again as error or we could be in a loop! | ||
return ("ERROR Failed to send email, %s" % str(e)) | ||
except: | ||
# cannot post again as error or we could be in a loop! | ||
return "ERROR Failed to send email(1)" | ||
except: | ||
return "ERROR Failed to send email(2)" | ||
|
||
# ---------------------------------- | ||
def get_root_resources(self): | ||
""" | ||
|
@@ -173,7 +245,8 @@ def clear_all_resources_live_status(self): | |
""" | ||
root_resources = self.get_root_resources() | ||
for resource in root_resources: | ||
self.api_session.SetResourceLiveStatus(resource.name, '') | ||
self.api_session.SetResourceLiveStatus(resource.name, liveStatusName="Info", | ||
additionalInfo='status cleared ' + strftime("%H:%M:%S", gmtime())) | ||
|
||
# ---------------------------------- | ||
# ---------------------------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import cloudshell.helpers.scripts.cloudshell_dev_helpers as dev_helpers | ||
from sandbox_scripts.environment.setup.setup_resources import * | ||
|
||
dev_helpers.attach_to_cloudshell_as(user="admin", password="dev", domain="Global", | ||
reservation_id="d7be79c2-57d0-4063-9ada-e27cd3c608a7", | ||
dev_helpers.attach_to_cloudshell_as(user="admin", password="xxx", domain="Global", | ||
reservation_id="3a8cad5c-f197-4b1e-8739-70829957562f", | ||
server_address="svl-dev-quali") | ||
os.environ["environment_name"] = "Abstract-ALL" | ||
|
||
x = EnvironmentSetupResources() | ||
x.execute() | ||
x.execute() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import cloudshell.helpers.scripts.cloudshell_dev_helpers as dev_helpers | ||
from sandbox_scripts.environment.teardown.teardown_resources import * | ||
|
||
dev_helpers.attach_to_cloudshell_as(user="admin", password="xx", domain="Global", | ||
reservation_id="bc0517e5-7240-4184-b04d-19e755f9c9a7", | ||
server_address="svl-dev-quali") | ||
|
||
x = EnvironmentTeardownResources() | ||
x.execute() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use the previous logic now that you fixed the attribute_exist function