Skip to content

Commit

Permalink
Merge pull request #19 from QualiSystemsLab/feature_8.0
Browse files Browse the repository at this point in the history
Latest updates from the 8.0 setup/teardown + cleanups and tests
  • Loading branch information
kalsky committed Mar 3, 2017
2 parents 03b69e2 + 6a00016 commit 165d811
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 155 deletions.
16 changes: 12 additions & 4 deletions sandbox_scripts/QualiEnvironmentUtils/Sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,37 @@ 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, 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):
"""
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
"""
if self._logger:
self._logger.error(error_message)
if log_message:
self._logger.error(log_message)
else:
self._logger.error(error_message)
if write_to_output_window:
self._write_message_to_output(error_message, SEVERITY_ERROR)
if raise_error:
raise QualiError(self.id, error_message)

# ----------------------------------
def report_info(self, message, write_to_output_window=False):
def report_info(self, message, log_message=None, write_to_output_window=False):
"""
Report information to the log file, output window is optional.
:param str message: The 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 write_to_output_window: Would you like to write the message to the output window?
"""
if self._logger:
self._logger.info(message)
if log_message:
self._logger.info(log_message)
else:
self._logger.info(message)
if write_to_output_window:
self._write_message_to_output(message, SEVERITY_INFO)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ def test_clear_all_resources_live_status_two_devices(self,mock_resourcebase):
self.sandbox.get_root_resources = Mock(return_value=rr)

self.sandbox.clear_all_resources_live_status()
#self.mock_api_session.return_value.SetResourceLiveStatus.assert_called()
calls = [call('r1', ''),
call('r2', '')]
self.mock_api_session.return_value.SetResourceLiveStatus.assert_has_calls(calls)
Expand Down
74 changes: 30 additions & 44 deletions sandbox_scripts/environment/setup/setup_VM.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,27 @@ def __init__(self):
log_group=self.reservation_id,
log_category='Setup')
self.is_snapshot = False
self.sandbox = None

#@profileit(scriptName='Setup')
def execute(self):
api = helpers.get_api_session()
self.sandbox = SandboxBase(self.reservation_id, self.logger)

api.WriteMessageToReservationOutput(reservationId=self.reservation_id,
message='Beginning resources power On')
self.sandbox.report_info('Beginning VMs power on')

reservation_details = api.GetReservationDetails(self.reservation_id)
reservation_details = self.sandbox.api_session.GetReservationDetails(self.reservation_id)

sandbox = SandboxBase(self.reservation_id, self.logger)
#TODO: don't use networking save and restore to figure if it's a snapshot setup
saveNRestoreTool = NetworkingSaveRestore(sandbox)
saveNRestoreTool = NetworkingSaveRestore(self.sandbox)
if saveNRestoreTool.get_storage_client():
if saveNRestoreTool.is_snapshot():
self.is_snapshot = True

self._run_async_power_on_refresh_ip_install(api,reservation_details)
self._run_async_power_on_refresh_ip(reservation_details)

# self._run_async_power_on_refresh_ip_install(api,reservation_details)

def _run_async_power_on_refresh_ip_install(self, api, reservation_details):
def _run_async_power_on_refresh_ip(self, reservation_details):

"""
:param CloudShellAPISession api:
:param GetReservationDescriptionResponseInfo reservation_details:
:param BulkAppDeploymentyInfo deploy_results:
:param (dict of str: ResourceInfo) resource_details_cache:
Expand All @@ -45,20 +41,17 @@ def _run_async_power_on_refresh_ip_install(self, api, reservation_details):
resources = reservation_details.ReservationDescription.Resources

if len(resources) == 0:
api.WriteMessageToReservationOutput(
reservationId=self.reservation_id,
message='No resources to power on or install')
self.sandbox.report_info(message='No resources to power on ', write_to_output_window=True)
return

pool = ThreadPool(len(resources))
lock = Lock()
message_status = {
"power_on": False,
"wait_for_ip": False,
"install": False
"wait_for_ip": False
}
async_results = [pool.apply_async(self._power_on_refresh_ip,
(api,lock, message_status, resource))
(lock, message_status, resource))
for resource in resources]

pool.close()
Expand All @@ -69,11 +62,8 @@ def _run_async_power_on_refresh_ip_install(self, api, reservation_details):
if not res[0]:
raise Exception("Reservation is Active with Errors - " + res[1])



def _power_on_refresh_ip(self, api, lock, message_status, resource):
def _power_on_refresh_ip(self, lock, message_status, resource):
"""
:param CloudShellAPISession api:
:param Lock lock:
:param (dict of str: Boolean) message_status:
:param ReservedResourceInfo resource:
Expand All @@ -83,7 +73,7 @@ def _power_on_refresh_ip(self, api, lock, message_status, resource):

deployed_app_name = resource.Name

resource_details = api.GetResourceDetails(deployed_app_name)
resource_details = self.sandbox.api_session.GetResourceDetails(deployed_app_name)
vm_details = resource_details.VmDetails

if not hasattr(vm_details, "UID"):
Expand All @@ -102,15 +92,15 @@ def _power_on_refresh_ip(self, api, lock, message_status, resource):


try:
self._power_on(api, deployed_app_name, power_on, lock, message_status)
self._power_on(deployed_app_name, power_on, lock, message_status)
except Exception as exc:
self.logger.error("Error powering on deployed app {0} in reservation {1}. Error: {2}"
.format(deployed_app_name, self.reservation_id, str(exc)))
return False, "Error powering on deployed app {0}".format(deployed_app_name)

try:
if resource.ResourceModelName.lower() !="vCenter Static VM":
self._wait_for_ip(api, deployed_app_name, wait_for_ip, lock, message_status)
self._wait_for_ip(deployed_app_name, wait_for_ip, lock, message_status)
except Exception as exc:
self.logger.error("Error refreshing IP on deployed app {0} in reservation {1}. Error: {2}"
.format(deployed_app_name, self.reservation_id, str(exc)))
Expand All @@ -119,51 +109,47 @@ def _power_on_refresh_ip(self, api, lock, message_status, resource):
return True,""


def _wait_for_ip(self, api, deployed_app_name, wait_for_ip, lock, message_status):
def _wait_for_ip(self, deployed_app_name, wait_for_ip, lock, message_status):

if wait_for_ip.lower() == "true":

if not message_status['wait_for_ip']:
with lock:
if not message_status['wait_for_ip']:
message_status['wait_for_ip'] = True
api.WriteMessageToReservationOutput(
reservationId=self.reservation_id,
message='Waiting for apps IP addresses, this may take a while...')
self.sandbox.report_info(message='Waiting for apps IP addresses, this may take a while...',
write_to_output_window=True)

self.logger.info("Executing 'Refresh IP' on deployed app {0} in reservation {1}"
.format(deployed_app_name, self.reservation_id))

api.ExecuteResourceConnectedCommand(self.reservation_id, deployed_app_name,
with lock:
self.sandbox.api_session.ExecuteResourceConnectedCommand(self.reservation_id, deployed_app_name,
"remote_refresh_ip",
"remote_connectivity")
else:
self.logger.info("Wait For IP is off for deployed app {0} in reservation {1}"
.format(deployed_app_name, self.reservation_id))

def _power_on(self, api, deployed_app_name, power_on, lock, message_status):

api.WriteMessageToReservationOutput(
reservationId=self.reservation_id,
message="Executing 'Power On' on deployed app {0} "
.format(deployed_app_name))
self.logger.info("Executing 'Power On' on deployed app {0} in reservation {1}"
.format(deployed_app_name, self.reservation_id))
def _power_on(self, deployed_app_name, power_on, lock, message_status):

if power_on.lower() == "true":

self.logger.info("Executing 'Power On' on deployed app {0} in reservation {1}"
.format(deployed_app_name, self.reservation_id))
with lock:
if not message_status['power_on']:
message_status['power_on'] = True
api.WriteMessageToReservationOutput(reservationId=self.reservation_id,
message='Apps are powering on... ')
self.sandbox.report_info('Apps are powering on... ')

api.ExecuteResourceConnectedCommand(self.reservation_id, deployed_app_name, "PowerOn", "power")
self.sandbox.report_info(message="Executing 'Power On' on deployed app {0} "
.format(deployed_app_name),
log_message="Executing 'Power On' on deployed app {0} in reservation {1}"
.format(deployed_app_name, self.reservation_id),
write_to_output_window=True)

self.sandbox.api_session.ExecuteResourceConnectedCommand(self.reservation_id, deployed_app_name,
"PowerOn", "power")


else:
self.logger.info("Auto Power On is off for deployed app {0} in reservation {1}"
.format(deployed_app_name, self.reservation_id))
.format(deployed_app_name, self.reservation_id))

4 changes: 3 additions & 1 deletion sandbox_scripts/environment/setup/setup_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def execute(self):
sandbox = SandboxBase(self.reservation_id, self.logger)
saveNRestoreTool = NetworkingSaveRestore(sandbox)

sandbox.report_info('Beginning resources config load')
sandbox.report_info('Beginning load configuration for resources')

try:
sandbox.clear_all_resources_live_status()
Expand Down Expand Up @@ -50,6 +50,8 @@ def execute(self):
# call activate_all_routes_and_connectors
sandbox.activate_all_routes_and_connectors()

sandbox.report_info('Sandbox setup finished successfully')

# Call routes_validation
# sandbox.routes_validation()
except QualiError as qe:
Expand Down
Loading

0 comments on commit 165d811

Please sign in to comment.