def azcli(commands, *fileName):
    if len(fileName) != 0:
        with open(fileName[0], 'w') as fileObject:
            z = get_default_cli().invoke(commands, out_file=fileObject)
    else:
        z = get_default_cli().invoke(commands)
    return z
Beispiel #2
0
def pull_pol_def_list_from_az_to_file(
        fileName="az_policy_definition_list.json"):
    try:
        with open(fileName, 'w') as outfile:
            get_default_cli().invoke(['policy', 'definition', 'list'],
                                     out_file=outfile)
            print(
                'Azure Defintion Policies List written to az_policy_definition_list.json'
            )
    except:
        print('Something went wrong.')  #json.dump(data, outfile)
def exceptionhandler():

   passcode = 1111
   attempts = 0
   returnResp = ""
   ipAddressValue = ""
   while passcode == 1111:

      if attempts < 2:
         attempts += 1
         try:
            returnResp = "OK"
            #establishing the connection
            #conn = mysql.connector.connect
            conn = pymysql.connect(user='******', password='******', host='ansibledemoserver.mysql.database.azure.com', database='defaultdb', ssl= {'ssl': {'ca': '/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt'}})

            #Creating a cursor object using the cursor() method
            cursor = conn.cursor()

         except Exception as e :
            print("exception occured while connecting to Azure MySQL", str(e))
            returnResp = "exception occured while connecting to Azure MySQL" + " " + str(e)
            #cmd = 'echo '+str(str(str(e).split("(9000, ")[1]).split(")")[0])+' | awk -F"Client with IP address " \'{print $2}\' | awk -F" " \'{print $1}\' | sed \'s/^ *//;s/ *$//\' | sed "s/\'//g"'
            intmStr = str(str(str(e).split("(9000, ")[1]).split(")")[0]).strip("'").strip(".")
            print (intmStr)
            cmd = "echo "+intmStr+" | grep -o \\'[0-9]*.[0-9]*.[0-9]*.[0-9]*\\' |  sed \"s/'//g\""  
            #cmd = "ls -lrt"
            print (cmd)
            #ipAddrData = subprocess.Popen(subprocess.check_output(["bash", "-c", cmd], stderr=subprocess.STDOUT, shell=True)
            ipAddrData = subprocess.Popen(["bash", "-c", cmd],  stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            ipAddressValue = ipAddrData.stdout.read().strip().decode("utf-8")
            print ("ipadress pulled is ",ipAddressValue)
            #cmd2 = "az mysql server firewall-rule create --resource-group myresourcegroup --server ansibledemoserver --name AllowIP --start-ip-address "+str(ipAddrData.stdout.read().strip())+" --end-ip-address "+str(ipAddrData.stdout.read().strip())

            cmd2 = ['mysql', 'server', 'firewall-rule', 'create', '--resource-group', 'myresourcegroup', '--server', 'ansibledemoserver', '--name', 'AllowIP', '--start-ip-address', '--end-ip-address']

            cmd2.insert(11, ipAddressValue)
            cmd2.insert(13, ipAddressValue)
            print ("command is +++++++++++++++++++++++++++")
            print (cmd2)
            get_default_cli().invoke(cmd2)
            
            #p2 = subprocess.Popen(["bash", "-c", cmd2], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            #ipAddrData2 = p2.stdout.read().strip().decode("utf-8")
            #print ("Firewall rule creation output "+ str(response))
      
      else:
         if "OK" != returnResp.upper() :
            returnResp = "All attempts to connect to Azure MySQL DB are failed.."
         exit()

   return str(returnResp)
Beispiel #4
0
def create_databricks_workspace():

    # 0. Deploy Databricks workspace, see https://azure.microsoft.com/nl-nl/resources/templates/101-databricks-workspace/
    # Key commands are as follows, make sure that ARM does not prompt you in case it needs to run automated
    # az group create --name <resource-group-name> --location <resource-group-location> #use this command when you need to create a new resource group for your deployment
    # az group deployment create --resource-group <my-resource-group> --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-databricks-workspace/azuredeploy.json
    # Run Azure CLI in python
    response = get_default_cli().invoke(
        ['group', 'create', '-n', resource_group, '-l', dbricks_location])
    print(response)
    response = get_default_cli().invoke([
        'group', 'deployment', 'create', '-g', resource_group,
        '--template-file', 'arm/azuredeploy.json'
    ])
    print(response)
Beispiel #5
0
def az_cli(args_str):
    cli = get_default_cli()
    cli.invoke(args_str)
    if cli.result.result:
        return cli.result.result
    elif cli.result.error:
        return cli.result.error
Beispiel #6
0
def show_management_cluster(_cmd, yes=False):
    # TODO: check to see if a management cluster is specified in the config
    config = get_default_cli().config
    # Config can also be set by the AZURE_CAPI_KUBECONFIG environment variable.
    kubeconfig = config.get("capi", "kubeconfig",
                            fallback=os.environ.get("KUBECONFIG"))
    if not kubeconfig:
        raise InvalidArgumentValueError("no kubeconfig")
    # make a $HOME/.azure/capi directory for storing cluster configurations
    path = os.path.join(get_config_dir(), "capi")
    if not os.path.exists(path):
        os.makedirs(path)
    command = ["kubectl", "config", "get-contexts",
               "--no-headers", "--output", "name"]
    try:
        output = subprocess.check_output(command, universal_newlines=True)
        logger.info("%s returned:\n%s", " ".join(command), output)
        contexts = output.splitlines()
        logger.info(contexts)
    except subprocess.CalledProcessError as err:
        raise UnclassifiedUserFault from err

    msg = path + "ok"
    if not yes and prompt_y_n(msg, default="n"):
        logger.info("yes")
def azure_cli_run(cmd: str) -> Union[Dict, bool]:
    """Run Azure CLI command

    Returns
    -------
    cli.result
        stout of Azure CLI command

    Raises
    ------
    cli.result.error
        If sterror occurs due to azure CLI command
    """

    args = cmd.split()
    cli = get_default_cli()
    cli.invoke(args)
    if cli.result.result:
        print("az {0}...".format(cmd))
        return cli.result.result
    elif cli.result.error:
        if any(msg in cli.result.error.message for msg in all_messages):
            logger.error(
                f"Creation failed due to known reason: {cli.result.error.message}"
            )
        else:
            raise cli.result.error
    return True
Beispiel #8
0
def az_cli(args):
    cli = get_default_cli()
    cli.invoke(args, out_file=open(os.devnull, "w"))
    if cli.result.result:
        return cli.result.result
    elif cli.result.error:
        raise cli.result.error
def invoke_az_cli(args_str):
    """ 
    Invoke azure cli command
    """
    login_user = os.environ.get('AD_LOGIN_USER', None)
    login_password = os.environ.get('AD_LOGIN_PASSWORD', None)

    if not login_user or not login_password:
        logger.error(
            "`loginUser` or `loginPassword` field is not set in environment")
        return {
            "error":
            "`loginUser` or `loginPassword` field is not set in environment"
        }

    azexe = os.environ.get('AZEXE', 'az')
    os.system(azexe + " login -u " + login_user + " -p " + login_password)

    args = args_str.split()
    cli = get_default_cli()
    cli.invoke(args)
    logger.info('Invoked Azure CLI command :: az %s' % args)
    if cli.result.result:
        os.system(azexe + " logout")
        return cli.result.result
    elif cli.result.error:
        raise cli.result.error
    return True
Beispiel #10
0
def main(args):
    from azure.cli.core import get_default_cli
    from azure.cli.core.file_util import get_all_help, create_invoker_and_load_cmds_and_args

    print('Initializing linter with command table and help files...')
    # setup CLI to enable command loader
    az_cli = get_default_cli()

    # load commands, args, and help
    create_invoker_and_load_cmds_and_args(az_cli)
    loaded_help = get_all_help(az_cli)
    command_table = az_cli.invocation.commands_loader.command_table

    # format loaded help
    loaded_help = {data.command: data for data in loaded_help if data.command}

    # load yaml help
    help_file_entries = {}
    for entry_name, help_yaml in helps.items():
        help_entry = yaml.load(help_yaml)
        help_file_entries[entry_name] = help_entry

    if not args.rule_types_to_run:
        args.rule_types_to_run = [
            'params', 'commands', 'command_groups', 'help_entries'
        ]

    # find rule exclusions and pass to linter manager
    from ..utilities.path import get_command_modules_paths
    exclusions = {}
    command_modules_paths = get_command_modules_paths()
    for _, path in command_modules_paths:
        exclusion_path = os.path.join(path, 'linter_exclusions.yml')
        if os.path.isfile(exclusion_path):
            mod_exclusions = yaml.load(open(exclusion_path))
            exclusions.update(mod_exclusions)

    # only run linter on modules specified
    if args.modules:
        from .util import include_mods
        command_table, help_file_entries = include_mods(
            command_table, help_file_entries, args.modules)

    # Instantiate and run Linter
    linter_manager = LinterManager(command_table=command_table,
                                   help_file_entries=help_file_entries,
                                   loaded_help=loaded_help,
                                   exclusions=exclusions,
                                   rule_inclusions=args.rules)
    exit_code = linter_manager.run(run_params='params'
                                   in args.rule_types_to_run,
                                   run_commands='commands'
                                   in args.rule_types_to_run,
                                   run_command_groups='command_groups'
                                   in args.rule_types_to_run,
                                   run_help_files_entries='help_entries'
                                   in args.rule_types_to_run,
                                   ci=args.ci)

    sys.exit(exit_code)
    def __init__(self,
                 storage=None,
                 auth_ctx_factory=None,
                 use_global_creds_cache=True,
                 async_persist=True,
                 cli_ctx=None):
        from azure.cli.core import get_default_cli

        self.cli_ctx = cli_ctx or get_default_cli()
        self._storage = storage or ACCOUNT
        self.auth_ctx_factory = auth_ctx_factory or _AUTH_CTX_FACTORY

        if use_global_creds_cache:
            # for perf, use global cache
            if not Profile._global_creds_cache:
                Profile._global_creds_cache = CredsCache(
                    self.cli_ctx,
                    self.auth_ctx_factory,
                    async_persist=async_persist)
            self._creds_cache = Profile._global_creds_cache
        else:
            self._creds_cache = CredsCache(self.cli_ctx,
                                           self.auth_ctx_factory,
                                           async_persist=async_persist)

        self._management_resource_uri = self.cli_ctx.cloud.endpoints.management
        self._ad_resource_uri = self.cli_ctx.cloud.endpoints.active_directory_resource_id
        self._msi_creds = None
Beispiel #12
0
def az_cli(args: List[str]) -> Any:
    cli = get_default_cli()
    cli.logging_cls
    cli.invoke(args, out_file=open(os.devnull, "w"))
    if cli.result.result:
        return cli.result.result
    elif cli.result.error:
        raise cli.result.error
Beispiel #13
0
def _update_postgres_server(target, password):
    cli = get_default_cli()
    parameters = [
        'postgres', 'server', 'update', '--ids', target, '--admin-password',
        password
    ]
    rc = cli.invoke(parameters)
    return rc
Beispiel #14
0
def az_cli(args_str):
    temp = tempfile.TemporaryFile()
    args = args_str.split()
    logger.debug('Sending cli command {}'.format(args))
    code = get_default_cli().invoke(args, None, temp)
    # temp.seek(0)
    data = temp.read().strip()
    temp.close()
    return [code, data]
Beispiel #15
0
def az_cli(args_str):
    args = args_str.split()
    cli = get_default_cli()
    cli.invoke(args, out_file=open(os.devnull, 'w'))
    if cli.result.result:
        return cli.result.result
    elif cli.result.error:
        raise Exception(cli.result.error)
    return True
def _is_telemetry_enabled():
    from azure.cli.core import get_default_cli
    collect_telemetry = None
    # Read the telemetry flag from az cli config file not the az devops extension config file
    az_cli_ctx = get_default_cli()
    az_config = az_cli_ctx.config
    if az_config.has_option('core', 'collect_telemetry'):
        collect_telemetry = az_config.get('core', 'collect_telemetry')
    return bool(collect_telemetry is None or collect_telemetry != 'no')
Beispiel #17
0
def az_cli (args_str):
    args = args_str.split()
    cli = get_default_cli()
    cli.invoke(args)
    if cli.result.result:
        return cli.result.result
    elif cli.result.error:
        raise cli.result.error
    return True
Beispiel #18
0
def main(args):
    from azure.cli.core import get_default_cli
    from azure.cli.core.file_util import get_all_help, create_invoker_and_load_cmds_and_args

    print('Initializing linter with command table and help files...')
    # setup CLI to enable command loader
    az_cli = get_default_cli()

    # load commands, args, and help
    create_invoker_and_load_cmds_and_args(az_cli)
    loaded_help = get_all_help(az_cli)
    command_loader = az_cli.invocation.commands_loader

    # format loaded help
    loaded_help = {data.command: data for data in loaded_help if data.command}

    # load yaml help
    help_file_entries = {}
    for entry_name, help_yaml in helps.items():
        help_entry = yaml.load(help_yaml)
        help_file_entries[entry_name] = help_entry

    if not args.rule_types_to_run:
        args.rule_types_to_run = ['params', 'commands', 'command_groups', 'help_entries']

    # find rule exclusions and pass to linter manager
    from ..utilities.path import get_command_modules_paths, get_extensions_paths
    exclusions = {}
    command_modules_paths = get_command_modules_paths()
    extension_paths = get_extensions_paths()
    for gen in (command_modules_paths, extension_paths):
        for _, path in gen:
            exclusion_path = os.path.join(path, 'linter_exclusions.yml')
            if os.path.isfile(exclusion_path):
                mod_exclusions = yaml.load(open(exclusion_path))
                exclusions.update(mod_exclusions)

    # only run linter on modules and extensions specified
    if args.modules or args.extensions:
        from .util import include_commands
        command_loader, help_file_entries = include_commands(
            command_loader, help_file_entries, module_inclusions=args.modules, extensions=args.extensions)

    # Instantiate and run Linter
    linter_manager = LinterManager(command_loader=command_loader,
                                   help_file_entries=help_file_entries,
                                   loaded_help=loaded_help,
                                   exclusions=exclusions,
                                   rule_inclusions=args.rules)
    exit_code = linter_manager.run(run_params='params' in args.rule_types_to_run,
                                   run_commands='commands' in args.rule_types_to_run,
                                   run_command_groups='command_groups' in args.rule_types_to_run,
                                   run_help_files_entries='help_entries' in args.rule_types_to_run,
                                   ci=args.ci)

    sys.exit(exit_code)
Beispiel #19
0
 def _az_cli(self, args_str):
     #https://stackoverflow.com/questions/51546073/how-to-run-azure-cli-commands-using-python
     args = args_str.split()
     cli = get_default_cli()
     cli.invoke(args)
     if cli.result.result:
         return cli.result.result
     elif cli.result.error:
         raise cli.result.error
     return True
Beispiel #20
0
 def authenticate(self):
     # authenticate to azure
     self.cli = get_default_cli()
     self.cli.invoke([
         'login',
         '--service-principal',
         '-u', self.servicePrincipal,
         '-p', self.clientSecret,
         '--tenant', self.tenant
     ])
Beispiel #21
0
def get_active_cloud(cli_ctx=None):
    if not cli_ctx:
        from azure.cli.core import get_default_cli
        cli_ctx = get_default_cli()
    try:
        return get_cloud(cli_ctx, get_active_cloud_name(cli_ctx))
    except CloudNotRegisteredException as err:
        logger.warning(err)
        logger.warning("Resetting active cloud to'%s'.", AZURE_PUBLIC_CLOUD.name)
        _set_active_cloud(cli_ctx, AZURE_PUBLIC_CLOUD.name)
        return get_cloud(cli_ctx, AZURE_PUBLIC_CLOUD.name)
def az_cli(args_str):
    args = args_str.split()
    cli = get_default_cli()
    cli.invoke(args)
    if cli.result.result:
        response = cli.result.result
        return response
    elif cli.result.error:
        err_response = cli.result.error
        raise err_response
    return True
Beispiel #23
0
def get_active_cloud(cli_ctx=None):
    if not cli_ctx:
        from azure.cli.core import get_default_cli
        cli_ctx = get_default_cli()
    try:
        return get_cloud(cli_ctx, get_active_cloud_name(cli_ctx))
    except CloudNotRegisteredException as err:
        logger.warning(err)
        logger.warning("Resetting active cloud to'%s'.", AZURE_PUBLIC_CLOUD.name)
        _set_active_cloud(cli_ctx, AZURE_PUBLIC_CLOUD.name)
        return get_cloud(cli_ctx, AZURE_PUBLIC_CLOUD.name)
Beispiel #24
0
def az_cli(args):
    cli = get_default_cli()
    cli.invoke(args)

    if cli.result.result:
        return cli.result.result

    elif cli.result.error:
        raise cli.result.error

    return True
def verify_load_all(_):
    from azure.cli.core import get_default_cli
    from azure.cli.core.file_util import get_all_help, create_invoker_and_load_cmds_and_args

    print('Loading all commands, arguments, and help...')
    # setup CLI to enable command loader
    az_cli = get_default_cli()

    # load commands, args, and help
    create_invoker_and_load_cmds_and_args(az_cli)
    loaded_help = get_all_help(az_cli)
    print('Everything loaded successfully.')
Beispiel #26
0
    def __init__(self, cli_ctx=None, storage=None):
        """Class to manage CLI's accounts (profiles) and identities (credentials).

        :param cli_ctx: The CLI context
        :param storage: A dict to store accounts, by default persisted to ~/.azure/azureProfile.json as JSON
        """
        from azure.cli.core import get_default_cli

        self.cli_ctx = cli_ctx or get_default_cli()
        self._storage = storage or ACCOUNT
        self._authority = self.cli_ctx.cloud.endpoints.active_directory

        from .auth.util import resource_to_scopes
        self._arm_scope = resource_to_scopes(
            self.cli_ctx.cloud.endpoints.active_directory_resource_id)
def _benchmark_load_all_commands():
    try:
        from azure.cli.core import get_default_cli
        from azure.cli.core.file_util import create_invoker_and_load_cmds_and_args
    except ImportError:
        raise CLIError("Azure CLI is not installed")

    az_cli = get_default_cli()

    create_invoker_and_load_cmds_and_args(az_cli)

    commands = list(az_cli.invocation.commands_loader.command_table.keys())

    commands = [cmd + " --help" for cmd in commands]

    return sorted(commands)
def az_cli(args_str: str, *non_split_args):
    '''
        Executes Azure CLI command as logged user
    '''
    from azure.cli.core import get_default_cli
    args = args_str.split()
    cli = get_default_cli()
    if DEBUG_ENABLED:
        print("az " + args_str + " " + " ".join(non_split_args))
    with open('/dev/null', 'w') as out_file:
        cli.invoke(args + list(non_split_args), out_file=out_file)
        if cli.result.result is not None:
            return cli.result.result
        elif cli.result.error:
            raise cli.result.error
        return True
def az_cli(args):
    """
    Call azure CLI using the given arguments.

    For example "vm list". When having extension installed they are
    also callable, for example "query -q 'Resources'"
    """
    temp = tempfile.TemporaryFile(mode="r+")
    code = get_default_cli().invoke(args, out_file=temp)
    temp.seek(0)
    data = temp.read().strip()
    temp.close()
    if code == SUCCESS_CODE:
        return code, json.loads(data.replace('null', '""'))
    else:
        return code, data
    def __init__(self,
                 test_file,
                 test_name,
                 run_live=False,
                 debug=False,
                 debug_vcr=False,
                 skip_setup=False,
                 skip_teardown=False):
        super(VCRTestBase, self).__init__(test_name)
        self.cli = get_default_cli()
        self.test_name = test_name
        self.recording_dir = os.path.join(os.path.dirname(test_file),
                                          'recordings')
        self.cassette_path = os.path.join(self.recording_dir,
                                          '{}.yaml'.format(test_name))
        self.playback = os.path.isfile(self.cassette_path)

        if os.environ.get(LIVE_TEST_CONTROL_ENV, None) == 'True':
            self.run_live = True
        else:
            self.run_live = run_live

        self.skip_setup = skip_setup
        self.skip_teardown = skip_teardown
        self.success = False
        self.exception = None
        self.track_commands = os.environ.get(COMMAND_COVERAGE_CONTROL_ENV,
                                             None)
        self._debug = debug

        if not self.playback and ('--buffer' in sys.argv) and not run_live:
            self.exception = CLIError(
                'No recorded result provided for {}.'.format(self.test_name))

        if debug_vcr:
            import logging
            logging.basicConfig()
            vcr_log = logging.getLogger('vcr')
            vcr_log.setLevel(logging.INFO)
        self.my_vcr = vcr.VCR(
            cassette_library_dir=self.recording_dir,
            before_record_request=self._before_record_request,
            before_record_response=self._before_record_response,
            decode_compressed_response=True,
            serializer='json')
        self.my_vcr.register_matcher('custom', _custom_request_matcher)
        self.my_vcr.match_on = ['custom']
Beispiel #31
0
def verify_load_all(_):
    from azure.cli.core import get_default_cli, EVENT_FAILED_EXTENSION_LOAD
    from azure.cli.core.file_util import get_all_help, create_invoker_and_load_cmds_and_args

    print('Loading all commands, arguments, and help...')
    # setup CLI to enable command loader and register event
    az_cli = get_default_cli()
    az_cli.register_event(EVENT_FAILED_EXTENSION_LOAD, extension_failed_load_handler)

    # load commands, args, and help
    create_invoker_and_load_cmds_and_args(az_cli)
    loaded_help = get_all_help(az_cli)

    # verify each installed extension is properly loaded
    if not FAILED_TO_LOAD or set(FAILED_TO_LOAD).issubset(set(EXTENSION_FAILURE_EXCLUSIONS)):
        print('Everything loaded successfully.')
    else:
        raise Exception('Exceptions failed to load: {}'.format(', '.join(FAILED_TO_LOAD)))
Beispiel #32
0
    def test_connection(self):
        if not cfg.AZ_CONFIG_PATH.is_file():
            self.logger.error('Azure connection has not been configured.')
            self.logger.error('Run: cmx az 1 --config')
            return False

        # Grab our user/domain and re-init logger.
        # Config should have stored this in the config file.
        f = open(cfg.AZ_CONFIG_PATH, "r")
        data = f.read()
        f.close()
        self.username = data.split()[0].split('@')[0]
        self.domain = data.split()[0].split('@')[1]
        self.proto_logger()

        self.az_cli = get_default_cli()

        return True
Beispiel #33
0
    def __init__(self, storage=None, auth_ctx_factory=None, use_global_creds_cache=True,
                 async_persist=True, cli_ctx=None):
        from azure.cli.core import get_default_cli

        self.cli_ctx = cli_ctx or get_default_cli()
        self._storage = storage or ACCOUNT
        self.auth_ctx_factory = auth_ctx_factory or _AUTH_CTX_FACTORY

        if use_global_creds_cache:
            # for perf, use global cache
            if not Profile._global_creds_cache:
                Profile._global_creds_cache = CredsCache(self.cli_ctx, self.auth_ctx_factory,
                                                         async_persist=async_persist)
            self._creds_cache = Profile._global_creds_cache
        else:
            self._creds_cache = CredsCache(self.cli_ctx, self.auth_ctx_factory, async_persist=async_persist)

        self._management_resource_uri = self.cli_ctx.cloud.endpoints.management
        self._ad_resource_uri = self.cli_ctx.cloud.endpoints.active_directory_resource_id
        self._msi_creds = None
Beispiel #34
0
# A workaround for https://bugs.python.org/issue32502 (https://github.com/Azure/azure-cli/issues/5184)
# If uuid1 raises ValueError, use uuid4 instead.
try:
    uuid.uuid1()
except ValueError:
    uuid.uuid1 = uuid.uuid4


logger = get_logger(__name__)


def cli_main(cli, args):
    return cli.invoke(args)


az_cli = get_default_cli()

telemetry.set_application(az_cli, ARGCOMPLETE_ENV_NAME)

try:
    telemetry.start()

    exit_code = cli_main(az_cli, sys.argv[1:])

    if exit_code and exit_code != 0:
        telemetry.set_failure()
    else:
        telemetry.set_success()

    sys.exit(exit_code)
except KeyboardInterrupt: