Ejemplo n.º 1
0
    def list_subs(self, line):
        from azure.cli.core._profile import Profile
        try:
            from azure.cli.core.util import CLIError
        except ImportError:
            from azure.cli.core._util import CLIError
        self._redirect_logging('az.azure.cli.core._profile')

        # load cached credentials in Profile() object
        profile = Profile()
        try:
            # if user is logged in, get their current subscription
            profile.get_subscription()
        except CLIError:
            # otherwise interactively prompy user to log in
            profile.find_subscriptions_on_login(True, None, None, None, None)

        # list all subscriptions
        subs = profile.load_cached_subscriptions()
        if not subs:
            print('No subscriptions available.')
            print('Please run `az login` from the console then try again')
            return
        print("Available subscriptions:\n  {}".format('\n  '.join(
            [sub['name'] for sub in subs])))
Ejemplo n.º 2
0
    def select_sub(self, line):
        from azure.cli.core._profile import Profile
        try:
            from azure.cli.core.util import CLIError
        except ImportError:
            from azure.cli.core._util import CLIError
        self._redirect_logging('az.azure.cli.core._profile')
        p = argparse.ArgumentParser()
        p.add_argument('subscription')
        parsed_args = p.parse_args(shlex.split(line))

        # if user is already logged in, get their subscriptions
        profile = Profile()
        subs = profile.load_cached_subscriptions()
        if not subs:
            # user not logged in--prompt them
            profile.find_subscriptions_on_login(True, None, None, None, None)

        try:
            profile.set_active_subscription(parsed_args.subscription)
            print('Active subscription set to {}'.format(
                profile.get_subscription()['name']))
        except CLIError as exc:
            print(exc)
            print('Active subscription remains {}'.format(
                profile.get_subscription()['name']))
Ejemplo n.º 3
0
def _get_subscription_id_from_subscription(cli_ctx, subscription):  # pylint: disable=inconsistent-return-statements
    profile = Profile(cli_ctx=cli_ctx)
    subscriptions_list = profile.load_cached_subscriptions()
    for sub in subscriptions_list:
        if sub['id'] == subscription or sub['name'] == subscription:
            return sub['id']
    from azure.cli.core.util import CLIError
    raise CLIError("Subscription not found in the current context.")
Ejemplo n.º 4
0
def _get_subscription_id_from_subscription(cli_ctx, subscription):  # pylint: disable=inconsistent-return-statements
    profile = Profile(cli_ctx=cli_ctx)
    subscriptions_list = profile.load_cached_subscriptions()
    for sub in subscriptions_list:
        if sub['id'] == subscription or sub['name'] == subscription:
            return sub['id']
    from azure.cli.core.util import CLIError
    raise CLIError("Subscription not found in the current context.")
def get_default_subscription_info():
    from azure.cli.core._profile import Profile
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions(False)
    for subscription in subscriptions:
        if subscription['isDefault']:
            return subscription['id'], subscription['name'], subscription['tenantId'], subscription['environmentName']
    logger.debug('Your account does not have a default Azure subscription. Please run "az login" to setup account.')
    return None, None, None, None
Ejemplo n.º 6
0
def get_default_subscription_info():
    """
    Returns the Id, name, tenantID and environmentName of the default subscription
    None if no default is set or no subscription is found
    """
    profile = Profile()
    dummy_user = profile.get_current_account_user()     # noqa: F841
    subscriptions = profile.load_cached_subscriptions(False)
    for subscription in subscriptions:
        if subscription['isDefault']:
            return subscription['id'], subscription['name'], subscription['tenantId'], subscription['environmentName']
    logger.debug('Your account does not have a default Azure subscription. Please run \'az login\' to setup account.')
    return None, None, None, None
Ejemplo n.º 7
0
 def __call__(self, parser, namespace, value, option_string=None):
     from azure.cli.core._profile import Profile
     profile = Profile(cli_ctx=namespace._cmd.cli_ctx)  # pylint: disable=protected-access
     subscriptions_list = profile.load_cached_subscriptions()
     sub_id = None
     for sub in subscriptions_list:
         match_val = value.lower()
         if sub['id'].lower() == match_val or sub['name'].lower() == match_val:
             sub_id = sub['id']
             break
     if not sub_id:
         logger.warning("Subscription '%s' not recognized.", value)
         sub_id = value
     namespace._subscription = sub_id  # pylint: disable=protected-access
Ejemplo n.º 8
0
    def check_deployment(self, line):
        from azure.cli.core._profile import Profile
        from azure.cli.command_modules.ml.env import env_setup
        from azure.cli.command_modules.ml._util import JupyterContext
        self._redirect_logging('az.azure.cli.core._profile')

        # validate that user has selected a subscription
        profile = Profile()
        subs = profile.load_cached_subscriptions()
        if not subs:
            print('Please run %select_sub before attempting to query.')
            return

        env_setup(True, None, None, None, None, None, context=JupyterContext())
Ejemplo n.º 9
0
def _set_active_subscription(context):
    ''' Set the active subscription used by Profile '''
    from azure.cli.core._profile import Profile, _ENVIRONMENT_NAME, _SUBSCRIPTION_ID, _STATE
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions()
    context_file_path = get_context_file_path(context['name'])
    context_config = configparser.SafeConfigParser()
    context_config.read(context_file_path)
    subscription_to_use = None
    try:
        subscription_to_use = context_config.get('context', 'default_subscription')
    except (configparser.NoSectionError, configparser.NoOptionError):
        # Fallback to finding the first subscription that is for this cloud
        for sub in subscriptions:
            if sub[_ENVIRONMENT_NAME] == context['cloud'] and sub[_STATE] == 'Enabled':
                subscription_to_use = sub[_SUBSCRIPTION_ID]
                break
    if subscription_to_use:
        profile.set_active_subscription(subscription_to_use)
 def list_subs(self, line):
     from azure.cli.core._profile import Profile
     try:
         from azure.cli.core.util import CLIError
     except ImportError:
         from azure.cli.core._util import CLIError
     self._redirect_logging('az.azure.cli.core._profile')
     profile = Profile()
     try:
         profile.get_subscription()
     except CLIError:
         profile.find_subscriptions_on_login(True, None, None, None, None)
     subs = profile.load_cached_subscriptions()
     if not subs:
         print('No subscriptions available.')
         print('Please run `az login` from the console then try again')
         return
     print("Available subscriptions:\n  {}".format('\n  '.join(
         [sub['name'] for sub in subs])))
Ejemplo n.º 11
0
def get_token_from_az_logins(organization, pat_token_present):
    profile = Profile()
    dummy_user = profile.get_current_account_user()  # noqa: F841
    subscriptions = profile.load_cached_subscriptions(False)
    tenantsDict = OrderedDict()

    # first loop to make sure the first identity we try with is coming from selected subscription
    for subscription in subscriptions:
        if subscription['isDefault'] == "true":
            tenantsDict[(subscription['tenantId'],
                         subscription['user']['name'])] = ''

    for subscription in subscriptions:
        tenantsDict[(subscription['tenantId'],
                     subscription['user']['name'])] = ''

    skipValidateToken = False
    if pat_token_present is False and len(tenantsDict) == 1:
        skipValidateToken = True

    try:
        for key, dummy_value in tenantsDict.items():
            try:
                logger.debug(
                    'trying to get token (temp) for tenant %s and user %s ',
                    key[0], key[1])
                token = get_token_from_az_login(profile, key[1], key[0])
                credentials = BasicAuthentication('', token)

                if skipValidateToken is True:
                    return token
                if validate_token_for_instance(organization, credentials):
                    return token
                logger.debug('invalid token obtained for tenant %s', key[0])
            except BaseException as ex2:
                logger.debug(ex2)
                logger.debug('failed while trying to get token for tenant %s',
                             key[0])
    except BaseException as ex:
        logger.debug(ex)

    return ''
Ejemplo n.º 12
0
def _set_active_subscription(context):
    ''' Set the active subscription used by Profile '''
    from azure.cli.core._profile import Profile, _ENVIRONMENT_NAME, _SUBSCRIPTION_ID, _STATE
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions()
    context_file_path = get_context_file_path(context['name'])
    context_config = configparser.SafeConfigParser()
    context_config.read(context_file_path)
    subscription_to_use = None
    try:
        subscription_to_use = context_config.get('context',
                                                 'default_subscription')
    except (configparser.NoSectionError, configparser.NoOptionError):
        # Fallback to finding the first subscription that is for this cloud
        for sub in subscriptions:
            if sub[_ENVIRONMENT_NAME] == context['cloud'] and sub[
                    _STATE] == 'Enabled':
                subscription_to_use = sub[_SUBSCRIPTION_ID]
                break
    if subscription_to_use:
        profile.set_active_subscription(subscription_to_use)
Ejemplo n.º 13
0
def _set_active_subscription(cloud_name):
    from azure.cli.core._profile import (Profile, _ENVIRONMENT_NAME, _SUBSCRIPTION_ID,
                                         _STATE, _SUBSCRIPTION_NAME)
    profile = Profile()
    subscription_to_use = get_cloud_subscription(cloud_name) or \
                          next((s[_SUBSCRIPTION_ID] for s in profile.load_cached_subscriptions()  # noqa
                                if s[_STATE] == 'Enabled'),
                               None)
    if subscription_to_use:
        try:
            profile.set_active_subscription(subscription_to_use)
            sub = profile.get_subscription(subscription_to_use)
            logger.warning("Active subscription switched to '%s (%s)'.",
                           sub[_SUBSCRIPTION_NAME], sub[_SUBSCRIPTION_ID])
        except CLIError as e:
            logger.warning(e)
            logger.warning("Unable to automatically switch the active subscription. "
                           "Use 'az account set'.")
    else:
        logger.warning("Use 'az login' to log in to this cloud.")
        logger.warning("Use 'az account set' to set the active subscription.")
Ejemplo n.º 14
0
def _load_subscriptions(cli_ctx, all_clouds=False, refresh=False):
    profile = Profile(cli_ctx=cli_ctx)
    if refresh:
        subscriptions = profile.refresh_accounts()
    subscriptions = profile.load_cached_subscriptions(all_clouds)
    return subscriptions
Ejemplo n.º 15
0
def load_subscriptions():
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions()
    return subscriptions
Ejemplo n.º 16
0
    def aml_env_setup(self, line):
        from azure.cli.core._profile import Profile
        self._redirect_logging('az.azure.cli.core._profile')
        p = argparse.ArgumentParser()
        p.add_argument('-n',
                       '--name',
                       help='base name for your environment',
                       required=True)
        p.add_argument('-k',
                       dest='kubernetes',
                       help='Flag to indicate kubernetes environment',
                       required=False,
                       action='store_true')
        p.add_argument('-l',
                       dest='local_only',
                       help='Flag to exclude ACS deployment',
                       required=False,
                       action='store_true')
        p.add_argument('-a',
                       dest='service_principal_app_id',
                       help='AppID of service principal',
                       required=False)
        p.add_argument('-p',
                       dest='service_principal_password',
                       help='Client Secret of service principal',
                       required=False)
        parsed_args = p.parse_args(line.split())
        # validate that user has selected a subscription
        profile = Profile()
        subs = profile.load_cached_subscriptions()
        if not subs:
            print(
                'Please run %select_sub before attempting to set up environment.'
            )
            return
        from azure.cli.command_modules.ml.env import env_setup
        from azure.cli.command_modules.ml._util import JupyterContext
        c = JupyterContext()
        c.set_input_response('Continue with this subscription (Y/n)? ', 'y')
        print(
            'Setting up AML environment. Feel free to continue exploring the rest '
            'of your notebook until this cell updates, though kernel will be busy...'
        )
        with Capturing() as output:
            env_setup(None, parsed_args.name, parsed_args.kubernetes,
                      parsed_args.local_only,
                      parsed_args.service_principal_app_id,
                      parsed_args.service_principal_password, c)
        acs_regex = r"az ml env setup -s (?P<deployment_id>[^']+)"
        env_regex = r'export (?P<k>[^=]+)=(?P<v>.+)'

        for line in output:
            s = re.search(acs_regex, line)
            if s:
                print(
                    'To check the status of the deployment, run line magic %check_deployment'
                )
            else:
                s = re.search(env_regex, line)
                if s:
                    self.print_and_update_env(s.group('k'), s.group('v'))
                else:
                    print(line)

        print('These values have also been added to the current environment.')
Ejemplo n.º 17
0
def load_subscriptions(all_clouds=False, refresh=False):
    profile = Profile()
    if refresh:
        subscriptions = profile.refresh_accounts()
    subscriptions = profile.load_cached_subscriptions(all_clouds)
    return subscriptions
Ejemplo n.º 18
0
def load_subscriptions(all_clouds=False):
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions(all_clouds)
    return subscriptions
Ejemplo n.º 19
0
def load_subscriptions():
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions()
    return subscriptions
    def check_deployment(self, line):
        from azure.cli.core._profile import Profile
        from azure.cli.core.commands import client_factory
        from azure.mgmt.resource.resources import ResourceManagementClient
        from azure.cli.command_modules.ml._az_util import az_get_app_insights_account
        self._redirect_logging('az.azure.cli.core._profile')
        p = argparse.ArgumentParser()
        p.add_argument('-d',
                       '--deployment',
                       help='Long running deployment to check',
                       required=True)
        parsed_args = p.parse_args(line.split())
        deployment_name = parsed_args.deployment

        # validate that user has selected a subscription
        profile = Profile()
        subs = profile.load_cached_subscriptions()
        if not subs:
            print('Please run %%select_sub before attempting to query.')
            return

        if 'deployment' not in deployment_name:
            print("Not a valid AML deployment name.")

        resource_group = deployment_name.split('deployment')[0]
        client = client_factory.get_mgmt_service_client(
            ResourceManagementClient).deployments
        result = client.get(resource_group, deployment_name)
        if result.properties.provisioning_state != 'Succeeded':
            print('Deployment status: {}'.format(
                result.properties.provisioning_state))
            return

        completed_deployment = result

        if 'appinsights' in completed_deployment.name:
            (app_insights_account_name, app_insights_account_key
             ) = az_get_app_insights_account(completed_deployment)
            if app_insights_account_name and app_insights_account_key:

                print("Environment updated with AppInsight information.")
                print(
                    "This notebook will keep this environment available, though kernel restarts will clear it."
                )
                print("To reset the environment, use the following commands:")
                print(' import os')
                result_str = '\n'.join([
                    self.print_and_update_env('AML_APP_INSIGHTS_NAME',
                                              app_insights_account_name),
                    self.print_and_update_env('AML_APP_INSIGHTS_KEY',
                                              app_insights_account_key)
                ])
                try:
                    with open(
                            os.path.join(os.path.expanduser('~'), '.amlenvrc'),
                            'a') as env_file:
                        env_file.write(result_str)
                        print('{} has also been updated.'.format(
                            env_file.name))
                except IOError:
                    pass
        else:
            acs_master = completed_deployment.properties.outputs['masterFQDN'][
                'value']
            acs_agent = completed_deployment.properties.outputs[
                'agentpublicFQDN']['value']
            if acs_master and acs_agent:
                print('ACS deployment succeeded.')
                print("Environment updated with ACS information.")
                print(
                    "This notebook will keep this environment available, though kernel restarts will clear it."
                )
                print("To reset the environment, use the following commands:")
                print(' import os')

                result_str = '\n'.join([
                    self.print_and_update_env('AML_ACS_MASTER', acs_master),
                    self.print_and_update_env('AML_ACS_AGENT', acs_agent)
                ])
                try:
                    with open(
                            os.path.join(os.path.expanduser('~'), '.amlenvrc'),
                            'a') as env_file:
                        env_file.write(result_str)
                        print('{} has also been updated.'.format(
                            env_file.name))
                except IOError:
                    pass
Ejemplo n.º 21
0
def load_subscriptions(all_clouds=False):
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions(all_clouds)
    return subscriptions
    def aml_env_setup(self, line):
        from azure.cli.core._profile import Profile
        self._redirect_logging('az.azure.cli.core._profile')
        p = argparse.ArgumentParser()
        p.add_argument('-n',
                       '--name',
                       help='base name for your environment',
                       required=True)
        parsed_args = p.parse_args(line.split())

        # validate that user has selected a subscription
        profile = Profile()
        subs = profile.load_cached_subscriptions()
        if not subs:
            print(
                'Please run %%select_sub before attempting to set up environment.'
            )
            return
        from azure.cli.command_modules.ml._util import create_ssh_key_if_not_exists
        from azure.cli.command_modules.ml._util import JupyterContext
        from azure.cli.command_modules.ml._az_util import az_create_resource_group
        from azure.cli.command_modules.ml._az_util import az_create_app_insights_account
        from azure.cli.command_modules.ml._az_util import az_create_storage_and_acr
        from azure.cli.command_modules.ml._az_util import az_create_acs
        from azure.cli.command_modules.ml._az_util import query_deployment_status
        from azure.cli.command_modules.ml._az_util import az_get_app_insights_account
        from azure.cli.command_modules.ml._az_util import AzureCliError
        import time
        print(
            'Setting up your Azure ML environment with a storage account, App Insights account, ACR registry and ACS cluster.'
        )
        c = JupyterContext()
        try:
            ssh_public_key = create_ssh_key_if_not_exists()
        except:
            return
        resource_group = az_create_resource_group(c, parsed_args.name)
        app_insights_deployment_id = az_create_app_insights_account(
            parsed_args.name, resource_group)
        (acr_login_server, c.acr_username, acr_password, storage_account_name, storage_account_key) = \
            az_create_storage_and_acr(parsed_args.name, resource_group)

        with Capturing() as output:
            az_create_acs(parsed_args.name, resource_group, acr_login_server,
                          c.acr_username, acr_password, ssh_public_key)

        acs_regex = r"az ml env setup -s (?P<deployment_id>[^']+)"
        for line in output:
            s = re.search(acs_regex, line)
            if s:
                print(
                    'To check the status of the deployment, run line magic %check_deployment -d {}'
                    .format(s.group('deployment_id')))
            else:
                print(line)

        completed_deployment = None
        while not completed_deployment:
            try:
                print('Querying App Insights deployment...')
                completed_deployment = query_deployment_status(
                    resource_group, app_insights_deployment_id)
                time.sleep(5)
            except AzureCliError as exc:
                print(exc.message)
                break

        print("Environment configured, pending ACS deployment completion.")
        print(
            "This notebook will keep this environment available, though kernel restarts will clear it."
        )
        print("To reset the environment, use the following commands:")
        print(' import os')

        result_str = ''
        if completed_deployment:
            app_insights_account_name, app_insights_account_key = az_get_app_insights_account(
                completed_deployment)
            result_str = '\n'.join([
                self.print_and_update_env('AML_APP_INSIGHTS_NAME',
                                          app_insights_account_name),
                self.print_and_update_env('AML_APP_INSIGHTS_KEY',
                                          app_insights_account_key)
            ])
        result_str += '\n'.join([
            self.print_and_update_env('AML_STORAGE_ACCT_NAME',
                                      storage_account_name),
            self.print_and_update_env('AML_STORAGE_ACCT_KEY',
                                      storage_account_name),
            self.print_and_update_env('AML_ACR_HOME', acr_login_server),
            self.print_and_update_env('AML_ACR_USER', c.acr_username),
            self.print_and_update_env('AML_ACR_PW', acr_password)
        ])
        try:
            with open(os.path.expanduser('~/.amlenvrc'), 'w+') as env_file:
                env_file.write(result_str)
                print('You can also find these settings saved in {}'.format(
                    env_file.name))
        except IOError:
            pass