def test_submission_start(self, mock_maya):
     session = mock.Mock(batch="batch_client", storage="storage_client")
     self.mock_self.ui = mock.create_autospec(SubmissionUI)
     self.mock_self.ui.render_module = "module"
     self.mock_self.renderer = mock.Mock()
     AzureBatchSubmission.start(self.mock_self, session, "assets", "pools",
                                "env")
     self.mock_self.renderer.delete.assert_called_with()
     self.mock_self._configure_renderer.assert_called_with()
     self.mock_self.renderer.display.assert_called_with("module")
     self.mock_self.ui.is_logged_in.assert_called_with()
Esempio n. 2
0
class AzureBatchSettings(object):

    tab_index = {
        'AUTH': 1,
        'SUBMIT': 2,
        'ASSETS': 3,
        'POOLS': 4,
        'JOBHISTORY': 5,
        'ENV': 6
    }

    @staticmethod
    def starter():
        """Called by the mel script when the shelf button is clicked."""
        AzureBatchSettings()

    def __init__(self):
        """Initialize all the tabs and attempt to authenticate using cached
        credentials if available.
        """
        self._log = logging.getLogger('AzureBatchMaya')
        try:
            self.frame = AzureBatchUI(self)
            self.config = AzureBatchConfig(self.tab_index['AUTH'], self.frame, self.start)
            self.submission = AzureBatchSubmission(self.tab_index['SUBMIT'], self.frame, self.call)
            self.assets = AzureBatchAssets(self.tab_index['ASSETS'], self.frame, self.call)
            self.pools = AzureBatchPools(self.tab_index['POOLS'], self.frame, self.call)
            self.jobhistory =  AzureBatchJobHistory(self.tab_index['JOBHISTORY'], self.frame, self.call)
            self.env =  AzureBatchEnvironment(self.tab_index['ENV'], self.frame, self.call)
            self.start()
        except Exception as exp:
            if (maya.window("AzureBatch", q=1, exists=1)):
                maya.delete_ui("AzureBatch")
            message = "Batch Plugin Failed to Start: {0}".format(exp)
            maya.error(message)
            raise

    def start(self):
        """Start the plugin UI. Depending on whether auto-authentication was
        successful, the plugin will start by displaying the submission tab.
        Otherwise the UI will be disabled, and the login tab will be displayed.
        """
        try:
            self._log.debug("Starting AzureBatchShared...")
            if self.config.auth:
                self.frame.is_logged_in()
                self.env.configure(self.config)
                self.jobhistory.configure(self.config)
                self.assets.configure(self.config)
                self.pools.configure(self.config, self.env)
                self.submission.start(self.config, self.assets, self.pools, self.env)
            else:
                self.frame.is_logged_out()
        except Exception as exp:
            self._log.warning(exp)
            if (maya.window("AzureBatch", q=1, exists=1)):
                maya.delete_ui("AzureBatch")
            maya.error("Batch Plugin UI failed to load:\n{0}".format(exp))

    def call(self, command, *args, **kwargs):
        """Wrap all Batch and Storage API calls in order to handle errors.
        Some errors we anticipate and raise without a dialog (e.g. PoolNotFound).
        Others we raise and display to the user.
        """
        try:
            return command(*args, **kwargs)
        except BatchErrorException as exp:
            if exp.error.code in ACCEPTED_ERRORS:
                self._log.info("Call failed: {}".format(exp.error.code))
                raise
            else:
                message = exp.error.message.value
                if exp.error.values:
                    message += "Details:\n"
                    for detail in exp.error.values:
                        message += "{}: {}".format(detail.key, detail.value)
                raise ValueError(message)
        except Exception as exp:
            if (maya.window("AzureBatch", q=1, exists=1)):
                maya.delete_ui("AzureBatch")
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self._log.error("".join(traceback.format_exception(exc_type, exc_value, exc_traceback)))
            raise ValueError("Error: {0}".format(exp))
Esempio n. 3
0
class AzureBatchSettings(object):

    tab_index = {
        'AUTH': 1,
        'SUBMIT': 2,
        'ASSETS': 3,
        'POOLS': 4,
        'JOBHISTORY': 5,
        'ENV': 6
    }

    @staticmethod
    def starter():
        """Called by the mel script when the shelf button is clicked."""
        AzureBatchSettings()

    def __init__(self):
        """Initialize all the tabs and attempt to authenticate using cached
        credentials if available.
        """
        self._log = logging.getLogger('AzureBatchMaya')
        try:
            self.frame = AzureBatchUI(self)
            self.config = AzureBatchConfig(self.tab_index['AUTH'], self,
                                           self.frame, self.start, self.call)

            if (self.config.can_init_from_config):
                self.init_after_account_selected()

        except Exception as exp:
            if (maya.window("AzureBatch", q=1, exists=1)):
                maya.delete_ui("AzureBatch")
            message = "Batch Plugin Failed to Start: {0}".format(exp)
            maya.error(message)
            raise

    def init_after_account_selected(self):
        try:
            if not hasattr(self, "submission"):
                self.submission = AzureBatchSubmission(
                    self.tab_index['SUBMIT'], self.frame, self.call)
            if not hasattr(self, "assets"):
                self.assets = AzureBatchAssets(self.tab_index['ASSETS'],
                                               self.frame, self.call)
            if not hasattr(self, "pools"):
                self.pools = AzureBatchPools(self.tab_index['POOLS'],
                                             self.frame, self.call)
            if not hasattr(self, "jobhistory"):
                self.jobhistory = AzureBatchJobHistory(
                    self.tab_index['JOBHISTORY'], self.frame, self.call)
            if not hasattr(self, "env"):
                self.env = AzureBatchEnvironment(self.tab_index['ENV'],
                                                 self.frame, self.call)

            self.config.auth = True
            self.start()
        except Exception as exp:
            if (maya.window("AzureBatch", q=1, exists=1)):
                maya.delete_ui("AzureBatch")
            message = "Batch Plugin Failed to Start: {0}".format(exp)
            maya.error(message)
            raise

    def start(self):
        """Start the plugin UI. Depending on whether auto-authentication was
        successful, the plugin will start by displaying the submission tab.
        Otherwise the UI will be disabled, and the login tab will be displayed.
        """
        try:
            self._log.debug("Starting AzureBatchShared...")
            if self.config.auth:
                self.frame.is_logged_in()
                self.env.configure(self.config, self.submission, self.assets)
                self.jobhistory.configure(self.config)
                self.assets.configure(self.config, self.submission, self.env)
                self.pools.configure(self.config, self.env)
                self.submission.start(self.config, self.assets, self.pools,
                                      self.env)
            else:
                self.frame.is_logged_out()
        except Exception as exp:
            self._log.warning(exp)
            if (maya.window("AzureBatch", q=1, exists=1)):
                maya.delete_ui("AzureBatch")
            maya.error("Batch Plugin UI failed to load:\n{0}".format(exp))

    def call(self, command, *args, **kwargs):
        """Wrap all Batch and Storage API calls in order to handle errors.
        Some errors we anticipate and raise without a dialog (e.g. PoolNotFound).
        Others we raise and display to the user.
        """
        try:
            result = command(*args, **kwargs)
            return self.ensure_iter_called(result)
        except BatchErrorException as exp:
            #if auth error (401) refresh tokens and recreate clients, before repeating the call
            if exp.response.status_code in [401]:
                self.config.refresh_auth_tokens(self.config.batch_auth_token,
                                                self.config.mgmt_auth_token)
                self.config.update_batch_and_storage_client_creds(
                    self.config.batch_auth_token, self.config.mgmt_auth_token)

                result = command(*args, **kwargs)
                try:
                    return self.ensure_iter_called(result)
                except BatchErrorException as exp:
                    self.handle_batch_exception(exp)
            else:
                self.handle_batch_exception(exp)

        except Exception as exp:
            if (maya.window("AzureBatch", q=1, exists=1)):
                maya.delete_ui("AzureBatch")
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self._log.error("".join(
                traceback.format_exception(exc_type, exc_value,
                                           exc_traceback)))
            raise ValueError("Error: {0}".format(exp))

    def handle_batch_exception(self, exp):
        if exp.error.code in ACCEPTED_ERRORS:
            self._log.info("Call failed: {}".format(exp.error.code))
            raise
        else:
            message = exp.error.message.value
            if exp.error.values:
                message += "Details:\n"
                for detail in exp.error.values:
                    message += "{}: {}".format(detail.key, detail.value)
            raise ValueError(message)

    def ensure_iter_called(self, result):
        if isinstance(result, collections.Iterator):
            #peek at the first result to force the first call and make sure any auth errors are raised here
            try:
                peek = result.next()
                result.reset()
            except StopIteration:
                pass
        return result