{"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndeleting all the keys in keyname", "response": "def delete_(*keyname):\n '''\n Delete metadata\n\n prop : string\n name of property\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mdata.get salt:role\n salt '*' mdata.get user-script salt:role\n '''\n mdata = _check_mdata_delete()\n valid_keynames = list_()\n ret = {}\n\n for k in keyname:\n if mdata and k in valid_keynames:\n cmd = '{0} {1}'.format(mdata, k)\n ret[k] = __salt__['cmd.run_all'](cmd, ignore_retcode=True)['retcode'] == 0\n else:\n ret[k] = True\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nshow the details of a single node", "response": "def show_instance(name, call=None):\n '''\n List the a single node, return dict of grains.\n '''\n local = salt.client.LocalClient()\n ret = local.cmd(name, 'grains.items')\n ret.update(_build_required_items(ret))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a new salt master", "response": "def create(vm_):\n '''\n if configuration parameter ``deploy`` is ``True``,\n\n Provision a single machine, adding its keys to the salt master\n\n else,\n\n Test ssh connections to the machine\n\n Configuration parameters:\n\n - deploy: (see above)\n - provider: name of entry in ``salt/cloud.providers.d/???`` file\n - ssh_host: IP address or DNS name of the new machine\n - ssh_username: name used to log in to the new machine\n - ssh_password: password to log in (unless key_filename is used)\n - key_filename: (optional) SSH private key for passwordless login\n - ssh_port: (default=22) TCP port for SSH connection\n - wake_on_lan_mac: (optional) hardware (MAC) address for wake on lan\n - wol_sender_node: (optional) salt minion to send wake on lan command\n - wol_boot_wait: (default=30) seconds to delay while client boots\n - force_minion_config: (optional) replace the minion configuration files on the new machine\n\n See also\n :ref:`Miscellaneous Salt Cloud Options `\n and\n :ref:`Getting Started with Saltify `\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -p mymachine my_new_id\n '''\n deploy_config = config.get_cloud_config_value(\n 'deploy', vm_, __opts__, default=False)\n\n # If ssh_host is not set, default to the minion name\n if not config.get_cloud_config_value('ssh_host', vm_, __opts__, default=''):\n vm_['ssh_host'] = vm_['name']\n\n if deploy_config:\n wol_mac = config.get_cloud_config_value(\n 'wake_on_lan_mac', vm_, __opts__, default='')\n wol_host = config.get_cloud_config_value(\n 'wol_sender_node', vm_, __opts__, default='')\n if wol_mac and wol_host:\n good_ping = False\n local = salt.client.LocalClient()\n ssh_host = config.get_cloud_config_value(\n 'ssh_host', vm_, __opts__, default='')\n if ssh_host:\n log.info('trying to ping %s', ssh_host)\n count = 'n' if salt.utils.platform.is_windows() else 'c'\n cmd = 'ping -{} 1 {}'.format(count, ssh_host)\n good_ping = local.cmd(wol_host, 'cmd.retcode', [cmd]) == 0\n if good_ping:\n log.info('successful ping.')\n else:\n log.info('sending wake-on-lan to %s using node %s',\n wol_mac, wol_host)\n\n if isinstance(wol_mac, six.string_types):\n wol_mac = [wol_mac] # a smart user may have passed more params\n ret = local.cmd(wol_host, 'network.wol', wol_mac)\n log.info('network.wol returned value %s', ret)\n if ret and ret[wol_host]:\n sleep_time = config.get_cloud_config_value(\n 'wol_boot_wait', vm_, __opts__, default=30)\n if sleep_time > 0.0:\n log.info('delaying %d seconds for boot', sleep_time)\n time.sleep(sleep_time)\n log.info('Provisioning existing machine %s', vm_['name'])\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n else:\n ret = _verify(vm_)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nverify that the credentials for an existing system is correct.", "response": "def _verify(vm_):\n '''\n Verify credentials for an existing system\n '''\n log.info('Verifying credentials for %s', vm_['name'])\n\n win_installer = config.get_cloud_config_value(\n 'win_installer', vm_, __opts__)\n\n if win_installer:\n\n log.debug('Testing Windows authentication method for %s', vm_['name'])\n\n if not HAS_IMPACKET:\n log.error('Impacket library not found')\n return False\n\n # Test Windows connection\n kwargs = {\n 'host': vm_['ssh_host'],\n 'username': config.get_cloud_config_value(\n 'win_username', vm_, __opts__, default='Administrator'),\n 'password': config.get_cloud_config_value(\n 'win_password', vm_, __opts__, default='')\n }\n\n # Test SMB connection\n try:\n log.debug('Testing SMB protocol for %s', vm_['name'])\n if __utils__['smb.get_conn'](**kwargs) is False:\n return False\n except (smbSessionError, smb3SessionError) as exc:\n log.error('Exception: %s', exc)\n return False\n\n # Test WinRM connection\n use_winrm = config.get_cloud_config_value(\n 'use_winrm', vm_, __opts__, default=False)\n\n if use_winrm:\n log.debug('WinRM protocol requested for %s', vm_['name'])\n if not HAS_WINRM:\n log.error('WinRM library not found')\n return False\n\n kwargs['port'] = config.get_cloud_config_value(\n 'winrm_port', vm_, __opts__, default=5986)\n kwargs['timeout'] = 10\n\n try:\n log.debug('Testing WinRM protocol for %s', vm_['name'])\n return __utils__['cloud.wait_for_winrm'](**kwargs) is not None\n except (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,\n ProxyError, RetryError, InvalidSchema, WinRMTransportError) as exc:\n log.error('Exception: %s', exc)\n return False\n\n return True\n\n else:\n\n log.debug('Testing SSH authentication method for %s', vm_['name'])\n\n # Test SSH connection\n kwargs = {\n 'host': vm_['ssh_host'],\n 'port': config.get_cloud_config_value(\n 'ssh_port', vm_, __opts__, default=22\n ),\n 'username': config.get_cloud_config_value(\n 'ssh_username', vm_, __opts__, default='root'\n ),\n 'password': config.get_cloud_config_value(\n 'password', vm_, __opts__, search_global=False\n ),\n 'key_filename': config.get_cloud_config_value(\n 'key_filename', vm_, __opts__, search_global=False,\n default=config.get_cloud_config_value(\n 'ssh_keyfile', vm_, __opts__, search_global=False,\n default=None\n )\n ),\n 'gateway': vm_.get('gateway', None),\n 'maxtries': 1\n }\n\n log.debug('Testing SSH protocol for %s', vm_['name'])\n try:\n return __utils__['cloud.wait_for_passwd'](**kwargs) is True\n except SaltCloudException as exc:\n log.error('Exception: %s', exc)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndestroy a node. .. versionadded:: 2018.3.0 Disconnect a minion from the master, and remove its keys. Optionally, (if ``remove_config_on_destroy`` is ``True``), disables salt-minion from running on the minion, and erases the Salt configuration files from it. Optionally, (if ``shutdown_on_destroy`` is ``True``), orders the minion to halt. CLI Example: .. code-block:: bash salt-cloud --destroy mymachine", "response": "def destroy(name, call=None):\n ''' Destroy a node.\n\n .. versionadded:: 2018.3.0\n\n Disconnect a minion from the master, and remove its keys.\n\n Optionally, (if ``remove_config_on_destroy`` is ``True``),\n disables salt-minion from running on the minion, and\n erases the Salt configuration files from it.\n\n Optionally, (if ``shutdown_on_destroy`` is ``True``),\n orders the minion to halt.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud --destroy mymachine\n\n '''\n if call == 'function':\n raise SaltCloudSystemExit(\n 'The destroy action must be called with -d, --destroy, '\n '-a, or --action.'\n )\n\n opts = __opts__\n\n __utils__['cloud.fire_event'](\n 'event',\n 'destroying instance',\n 'salt/cloud/{0}/destroying'.format(name),\n args={'name': name},\n sock_dir=opts['sock_dir'],\n transport=opts['transport']\n )\n\n vm_ = get_configured_provider()\n local = salt.client.LocalClient()\n my_info = local.cmd(name, 'grains.get', ['salt-cloud'])\n try:\n vm_.update(my_info[name]) # get profile name to get config value\n except (IndexError, TypeError):\n pass\n if config.get_cloud_config_value(\n 'remove_config_on_destroy', vm_, opts, default=True\n ):\n ret = local.cmd(name, # prevent generating new keys on restart\n 'service.disable',\n ['salt-minion'])\n if ret and ret[name]:\n log.info('disabled salt-minion service on %s', name)\n ret = local.cmd(name, 'config.get', ['conf_file'])\n if ret and ret[name]:\n confile = ret[name]\n ret = local.cmd(name, 'file.remove', [confile])\n if ret and ret[name]:\n log.info('removed minion %s configuration file %s',\n name, confile)\n ret = local.cmd(name, 'config.get', ['pki_dir'])\n if ret and ret[name]:\n pki_dir = ret[name]\n ret = local.cmd(name, 'file.remove', [pki_dir])\n if ret and ret[name]:\n log.info(\n 'removed minion %s key files in %s',\n name,\n pki_dir)\n\n if config.get_cloud_config_value(\n 'shutdown_on_destroy', vm_, opts, default=False\n ):\n ret = local.cmd(name, 'system.shutdown')\n if ret and ret[name]:\n log.info('system.shutdown for minion %s successful', name)\n\n __utils__['cloud.fire_event'](\n 'event',\n 'destroyed instance',\n 'salt/cloud/{0}/destroyed'.format(name),\n args={'name': name},\n sock_dir=opts['sock_dir'],\n transport=opts['transport']\n )\n\n return {'Destroyed': '{0} was destroyed.'.format(name)}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef present(name,\n config=None,\n **kwargs):\n '''\n Ensure the job is present in the Jenkins configured jobs\n\n name\n The unique name for the Jenkins job\n\n config\n The Salt URL for the file to use for configuring the job\n '''\n\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ['Job {0} is up to date.'.format(name)]}\n\n if __salt__['jenkins.job_exists'](name):\n _current_job_config = __salt__['jenkins.get_job_config'](name)\n buf = six.moves.StringIO(_current_job_config)\n oldXML = ET.fromstring(buf.read())\n\n cached_source_path = __salt__['cp.cache_file'](config, __env__)\n with salt.utils.files.fopen(cached_source_path) as _fp:\n newXML = ET.fromstring(salt.utils.stringutils.to_unicode(_fp.read()))\n if not _elements_equal(oldXML, newXML):\n diff = difflib.unified_diff(\n ET.tostringlist(oldXML, encoding='utf8', method='xml'),\n ET.tostringlist(newXML, encoding='utf8', method='xml'), lineterm='')\n try:\n __salt__['jenkins.update_job'](name, config, __env__)\n except CommandExecutionError as exc:\n return _fail(ret, exc.strerror)\n else:\n ret['changes'] = ''.join(diff)\n ret['comment'].append('Job \\'{0}\\' updated.'.format(name))\n\n else:\n cached_source_path = __salt__['cp.cache_file'](config, __env__)\n with salt.utils.files.fopen(cached_source_path) as _fp:\n new_config_xml = salt.utils.stringutils.to_unicode(_fp.read())\n\n try:\n __salt__['jenkins.create_job'](name, config, __env__)\n except CommandExecutionError as exc:\n return _fail(ret, exc.strerror)\n\n buf = six.moves.StringIO(new_config_xml)\n diff = difflib.unified_diff('', buf.readlines(), lineterm='')\n ret['changes'][name] = ''.join(diff)\n ret['comment'].append('Job \\'{0}\\' added.'.format(name))\n\n ret['comment'] = '\\n'.join(ret['comment'])\n return ret", "response": "Ensure the Jenkins job is present in Jenkins configured jobs"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef absent(name,\n **kwargs):\n '''\n Ensure the job is absent from the Jenkins configured jobs\n\n name\n The name of the Jenkins job to remove\n '''\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': []}\n\n if __salt__['jenkins.job_exists'](name):\n try:\n __salt__['jenkins.delete_job'](name)\n except CommandExecutionError as exc:\n return _fail(ret, exc.strerror)\n else:\n ret['comment'] = 'Job \\'{0}\\' deleted.'.format(name)\n else:\n ret['comment'] = 'Job \\'{0}\\' already absent.'.format(name)\n return ret", "response": "Ensure the Jenkins job is absent from the Jenkins configured jobs\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\niterating over a list of proxynames and restart any that aren t running", "response": "def _run_proxy_processes(proxies):\n '''\n Iterate over a list of proxy\n names and restart any that\n aren't running\n '''\n ret = []\n for proxy in proxies:\n result = {}\n if not __salt__['salt_proxy.is_running'](proxy)['result']:\n __salt__['salt_proxy.configure_proxy'](proxy, start=True)\n result[proxy] = 'Proxy {0} was started'.format(proxy)\n else:\n msg = 'Proxy {0} is already running'.format(proxy)\n result[proxy] = msg\n log.debug(msg)\n ret.append(result)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef beacon(config):\n '''\n Handle configured proxies\n\n .. code-block:: yaml\n\n beacons:\n salt_proxy:\n - proxies:\n p8000: {}\n p8001: {}\n '''\n log.trace('salt proxy beacon called')\n\n _config = {}\n list(map(_config.update, config))\n\n return _run_proxy_processes(_config['proxies'])", "response": "Handle configured proxies\n and return a list of the processed proxies"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_licenses():\n '''\n returns a list of applied powerpath license keys\n '''\n KEY_PATTERN = re.compile('Key (.*)')\n\n keys = []\n out = __salt__['cmd.run']('/sbin/emcpreg -list')\n for line in out.splitlines():\n match = KEY_PATTERN.match(line)\n\n if not match:\n continue\n\n keys.append({'key': match.group(1)})\n\n return keys", "response": "returns a list of applied powerpath license keys\n returns a list of applied powerpath license keys\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd a license to the specified key", "response": "def add_license(key):\n '''\n Add a license\n '''\n result = {\n 'result': False,\n 'retcode': -1,\n 'output': ''\n }\n\n if not has_powerpath():\n result['output'] = 'PowerPath is not installed'\n return result\n\n cmd = '/sbin/emcpreg -add {0}'.format(key)\n ret = __salt__['cmd.run_all'](cmd, python_shell=True)\n\n result['retcode'] = ret['retcode']\n\n if ret['retcode'] != 0:\n result['output'] = ret['stderr']\n else:\n result['output'] = ret['stdout']\n result['result'] = True\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef managed(name, table, data, record=None):\n '''\n Ensures that the specified columns of the named record have the specified\n values.\n\n Args:\n name: The name of the record.\n table: The name of the table to which the record belongs.\n data: Dictionary containing a mapping from column names to the desired\n values. Columns that exist, but are not specified in this\n dictionary are not touched.\n record: The name of the record (optional). Replaces name if specified.\n '''\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n if record is None:\n record = name\n current_data = {\n column: __salt__['openvswitch.db_get'](table, record, column)\n for column in data\n }\n\n # Comment and change messages\n comment_changes = 'Columns have been updated.'\n comment_no_changes = 'All columns are already up to date.'\n comment_error = 'Error while updating column {0}: {1}'\n\n # Dry run, test=true mode\n if __opts__['test']:\n for column in data:\n if data[column] != current_data[column]:\n ret['changes'][column] = {'old': current_data[column],\n 'new': data[column]}\n if ret['changes']:\n ret['result'] = None\n ret['comment'] = comment_changes\n else:\n ret['result'] = True\n ret['comment'] = comment_no_changes\n return ret\n\n for column in data:\n if data[column] != current_data[column]:\n result = __salt__['openvswitch.db_set'](table, record, column,\n data[column])\n if result is not None:\n ret['comment'] = comment_error.format(column, result)\n ret['result'] = False\n return ret\n ret['changes'][column] = {'old': current_data[column],\n 'new': data[column]}\n ret['result'] = True\n ret['comment'] = comment_no_changes\n return ret", "response": "Ensures that the specified columns of the named record are set to the specified values."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _fix_ctx(m2_ctx, issuer=None):\n '''\n This is part of an ugly hack to fix an ancient bug in M2Crypto\n https://bugzilla.osafoundation.org/show_bug.cgi?id=7530#c13\n '''\n ctx = _Ctx.from_address(int(m2_ctx)) # pylint: disable=no-member\n\n ctx.flags = 0\n ctx.subject_cert = None\n ctx.subject_req = None\n ctx.crl = None\n if issuer is None:\n ctx.issuer_cert = None\n else:\n ctx.issuer_cert = int(issuer.x509)", "response": "Fixes the context of a M2Crypto\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _new_extension(name, value, critical=0, issuer=None, _pyfree=1):\n '''\n Create new X509_Extension, This is required because M2Crypto\n doesn't support getting the publickeyidentifier from the issuer\n to create the authoritykeyidentifier extension.\n '''\n if name == 'subjectKeyIdentifier' and value.strip('0123456789abcdefABCDEF:') is not '':\n raise salt.exceptions.SaltInvocationError('value must be precomputed hash')\n\n # ensure name and value are bytes\n name = salt.utils.stringutils.to_str(name)\n value = salt.utils.stringutils.to_str(value)\n\n try:\n ctx = M2Crypto.m2.x509v3_set_nconf()\n _fix_ctx(ctx, issuer)\n if ctx is None:\n raise MemoryError(\n 'Not enough memory when creating a new X509 extension')\n x509_ext_ptr = M2Crypto.m2.x509v3_ext_conf(None, ctx, name, value)\n lhash = None\n except AttributeError:\n lhash = M2Crypto.m2.x509v3_lhash() # pylint: disable=no-member\n ctx = M2Crypto.m2.x509v3_set_conf_lhash(\n lhash) # pylint: disable=no-member\n # ctx not zeroed\n _fix_ctx(ctx, issuer)\n x509_ext_ptr = M2Crypto.m2.x509v3_ext_conf(\n lhash, ctx, name, value) # pylint: disable=no-member\n # ctx,lhash freed\n\n if x509_ext_ptr is None:\n raise M2Crypto.X509.X509Error(\n \"Cannot create X509_Extension with name '{0}' and value '{1}'\".format(name, value))\n x509_ext = M2Crypto.X509.X509_Extension(x509_ext_ptr, _pyfree)\n x509_ext.set_critical(critical)\n return x509_ext", "response": "Create a new X509 extension."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nparses openssl command line output and returns a dict of the resulting CSR objects.", "response": "def _parse_openssl_req(csr_filename):\n '''\n Parses openssl command line output, this is a workaround for M2Crypto's\n inability to get them from CSR objects.\n '''\n if not salt.utils.path.which('openssl'):\n raise salt.exceptions.SaltInvocationError(\n 'openssl binary not found in path'\n )\n cmd = ('openssl req -text -noout -in {0}'.format(csr_filename))\n\n output = __salt__['cmd.run_stdout'](cmd)\n\n output = re.sub(r': rsaEncryption', ':', output)\n output = re.sub(r'[0-9a-f]{2}:', '', output)\n\n return salt.utils.data.decode(salt.utils.yaml.safe_load(output))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_csr_extensions(csr):\n '''\n Returns a list of dicts containing the name, value and critical value of\n any extension contained in a csr object.\n '''\n ret = OrderedDict()\n\n csrtempfile = tempfile.NamedTemporaryFile()\n csrtempfile.write(csr.as_pem())\n csrtempfile.flush()\n csryaml = _parse_openssl_req(csrtempfile.name)\n csrtempfile.close()\n if csryaml and 'Requested Extensions' in csryaml['Certificate Request']['Data']:\n csrexts = csryaml['Certificate Request']['Data']['Requested Extensions']\n\n if not csrexts:\n return ret\n\n for short_name, long_name in six.iteritems(EXT_NAME_MAPPINGS):\n if long_name in csrexts:\n csrexts[short_name] = csrexts[long_name]\n del csrexts[long_name]\n ret = csrexts\n return ret", "response": "Returns a list of dicts containing the name value and critical value of any extension contained in a CSR object."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nparses the contents of a openssl - text file and returns a dict of the keys to be used in the CSR objects.", "response": "def _parse_openssl_crl(crl_filename):\n '''\n Parses openssl command line output, this is a workaround for M2Crypto's\n inability to get them from CSR objects.\n '''\n if not salt.utils.path.which('openssl'):\n raise salt.exceptions.SaltInvocationError(\n 'openssl binary not found in path'\n )\n cmd = ('openssl crl -text -noout -in {0}'.format(crl_filename))\n\n output = __salt__['cmd.run_stdout'](cmd)\n\n crl = {}\n for line in output.split('\\n'):\n line = line.strip()\n if line.startswith('Version '):\n crl['Version'] = line.replace('Version ', '')\n if line.startswith('Signature Algorithm: '):\n crl['Signature Algorithm'] = line.replace(\n 'Signature Algorithm: ', '')\n if line.startswith('Issuer: '):\n line = line.replace('Issuer: ', '')\n subject = {}\n for sub_entry in line.split('/'):\n if '=' in sub_entry:\n sub_entry = sub_entry.split('=')\n subject[sub_entry[0]] = sub_entry[1]\n crl['Issuer'] = subject\n if line.startswith('Last Update: '):\n crl['Last Update'] = line.replace('Last Update: ', '')\n last_update = datetime.datetime.strptime(\n crl['Last Update'], \"%b %d %H:%M:%S %Y %Z\")\n crl['Last Update'] = last_update.strftime(\"%Y-%m-%d %H:%M:%S\")\n if line.startswith('Next Update: '):\n crl['Next Update'] = line.replace('Next Update: ', '')\n next_update = datetime.datetime.strptime(\n crl['Next Update'], \"%b %d %H:%M:%S %Y %Z\")\n crl['Next Update'] = next_update.strftime(\"%Y-%m-%d %H:%M:%S\")\n if line.startswith('Revoked Certificates:'):\n break\n\n if 'No Revoked Certificates.' in output:\n crl['Revoked Certificates'] = []\n return crl\n\n output = output.split('Revoked Certificates:')[1]\n output = output.split('Signature Algorithm:')[0]\n\n rev = []\n for revoked in output.split('Serial Number: '):\n if not revoked.strip():\n continue\n\n rev_sn = revoked.split('\\n')[0].strip()\n revoked = rev_sn + ':\\n' + '\\n'.join(revoked.split('\\n')[1:])\n rev_yaml = salt.utils.data.decode(salt.utils.yaml.safe_load(revoked))\n # pylint: disable=unused-variable\n for rev_item, rev_values in six.iteritems(rev_yaml):\n # pylint: enable=unused-variable\n if 'Revocation Date' in rev_values:\n rev_date = datetime.datetime.strptime(\n rev_values['Revocation Date'], \"%b %d %H:%M:%S %Y %Z\")\n rev_values['Revocation Date'] = rev_date.strftime(\n \"%Y-%m-%d %H:%M:%S\")\n\n rev.append(rev_yaml)\n\n crl['Revoked Certificates'] = rev\n\n return crl"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _pretty_hex(hex_str):\n '''\n Nicely formats hex strings\n '''\n if len(hex_str) % 2 != 0:\n hex_str = '0' + hex_str\n return ':'.join(\n [hex_str[i:i + 2] for i in range(0, len(hex_str), 2)]).upper()", "response": "Nicely formats hex strings\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndetermine if input is a file or a string with the Taxonomy content to be parsed.", "response": "def _text_or_file(input_):\n '''\n Determines if input is a path to a file, or a string with the\n content to be parsed.\n '''\n if _isfile(input_):\n with salt.utils.files.fopen(input_) as fp_:\n out = salt.utils.stringutils.to_str(fp_.read())\n else:\n out = salt.utils.stringutils.to_str(input_)\n\n return out"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a dict containing all values in an X509 Subject", "response": "def _parse_subject(subject):\n '''\n Returns a dict containing all values in an X509 Subject\n '''\n ret = {}\n nids = []\n for nid_name, nid_num in six.iteritems(subject.nid):\n if nid_num in nids:\n continue\n try:\n val = getattr(subject, nid_name)\n if val:\n ret[nid_name] = val\n nids.append(nid_num)\n except TypeError as err:\n log.debug(\"Missing attribute '%s'. Error: %s\", nid_name, err)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_certificate_obj(cert):\n '''\n Returns a certificate object based on PEM text.\n '''\n if isinstance(cert, M2Crypto.X509.X509):\n return cert\n\n text = _text_or_file(cert)\n text = get_pem_entry(text, pem_type='CERTIFICATE')\n return M2Crypto.X509.load_cert_string(text)", "response": "Returns a certificate object based on PEM text."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a private key object based on PEM text.", "response": "def _get_private_key_obj(private_key, passphrase=None):\n '''\n Returns a private key object based on PEM text.\n '''\n private_key = _text_or_file(private_key)\n private_key = get_pem_entry(private_key, pem_type='(?:RSA )?PRIVATE KEY')\n rsaprivkey = M2Crypto.RSA.load_key_string(\n private_key, callback=_passphrase_callback(passphrase))\n evpprivkey = M2Crypto.EVP.PKey()\n evpprivkey.assign_rsa(rsaprivkey)\n return evpprivkey"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a callback function used to supply a passphrase for private keys", "response": "def _passphrase_callback(passphrase):\n '''\n Returns a callback function used to supply a passphrase for private keys\n '''\n def f(*args):\n return salt.utils.stringutils.to_bytes(passphrase)\n return f"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_request_obj(csr):\n '''\n Returns a CSR object based on PEM text.\n '''\n text = _text_or_file(csr)\n text = get_pem_entry(text, pem_type='CERTIFICATE REQUEST')\n return M2Crypto.X509.load_request_string(text)", "response": "Returns a CSR object based on PEM text."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the sha1 hash of the modulus of a public key in a cert Used for generating subject key identifiers", "response": "def _get_pubkey_hash(cert):\n '''\n Returns the sha1 hash of the modulus of a public key in a cert\n Used for generating subject key identifiers\n '''\n sha_hash = hashlib.sha1(cert.get_pubkey().get_modulus()).hexdigest()\n return _pretty_hex(sha_hash)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a properly formatted PEM string from the input text removing any whitespace or line - break issues and adding a new entry to the output.", "response": "def get_pem_entry(text, pem_type=None):\n '''\n Returns a properly formatted PEM string from the input text fixing\n any whitespace or line-break issues\n\n text:\n Text containing the X509 PEM entry to be returned or path to\n a file containing the text.\n\n pem_type:\n If specified, this function will only return a pem of a certain type,\n for example 'CERTIFICATE' or 'CERTIFICATE REQUEST'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.get_pem_entry \"-----BEGIN CERTIFICATE REQUEST-----MIICyzCC Ar8CAQI...-----END CERTIFICATE REQUEST\"\n '''\n text = _text_or_file(text)\n # Replace encoded newlines\n text = text.replace('\\\\n', '\\n')\n\n _match = None\n\n if len(text.splitlines()) == 1 and text.startswith(\n '-----') and text.endswith('-----'):\n # mine.get returns the PEM on a single line, we fix this\n pem_fixed = []\n pem_temp = text\n while pem_temp:\n if pem_temp.startswith('-----'):\n # Grab ----(.*)---- blocks\n pem_fixed.append(pem_temp[:pem_temp.index('-----', 5) + 5])\n pem_temp = pem_temp[pem_temp.index('-----', 5) + 5:]\n else:\n # grab base64 chunks\n if pem_temp[:64].count('-') == 0:\n pem_fixed.append(pem_temp[:64])\n pem_temp = pem_temp[64:]\n else:\n pem_fixed.append(pem_temp[:pem_temp.index('-')])\n pem_temp = pem_temp[pem_temp.index('-'):]\n text = \"\\n\".join(pem_fixed)\n\n _dregex = _make_regex('[0-9A-Z ]+')\n errmsg = 'PEM text not valid:\\n{0}'.format(text)\n if pem_type:\n _dregex = _make_regex(pem_type)\n errmsg = ('PEM does not contain a single entry of type {0}:\\n'\n '{1}'.format(pem_type, text))\n\n for _match in _dregex.finditer(text):\n if _match:\n break\n if not _match:\n raise salt.exceptions.SaltInvocationError(errmsg)\n _match_dict = _match.groupdict()\n pem_header = _match_dict['pem_header']\n proc_type = _match_dict['proc_type']\n dek_info = _match_dict['dek_info']\n pem_footer = _match_dict['pem_footer']\n pem_body = _match_dict['pem_body']\n\n # Remove all whitespace from body\n pem_body = ''.join(pem_body.split())\n\n # Generate correctly formatted pem\n ret = pem_header + '\\n'\n if proc_type:\n ret += proc_type + '\\n'\n if dek_info:\n ret += dek_info + '\\n' + '\\n'\n for i in range(0, len(pem_body), 64):\n ret += pem_body[i:i + 64] + '\\n'\n ret += pem_footer + '\\n'\n\n return salt.utils.stringutils.to_bytes(ret, encoding='ascii')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a dict containing PEM entries in files matching a glob_path", "response": "def get_pem_entries(glob_path):\n '''\n Returns a dict containing PEM entries in files matching a glob\n\n glob_path:\n A path to certificates to be read and returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.get_pem_entries \"/etc/pki/*.crt\"\n '''\n ret = {}\n\n for path in glob.glob(glob_path):\n if os.path.isfile(path):\n try:\n ret[path] = get_pem_entry(text=path)\n except ValueError as err:\n log.debug('Unable to get PEM entries from %s: %s', path, err)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef read_certificate(certificate):\n '''\n Returns a dict containing details of a certificate. Input can be a PEM\n string or file path.\n\n certificate:\n The certificate to be read. Can be a path to a certificate file, or\n a string containing the PEM formatted text of the certificate.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.read_certificate /etc/pki/mycert.crt\n '''\n cert = _get_certificate_obj(certificate)\n\n ret = {\n # X509 Version 3 has a value of 2 in the field.\n # Version 2 has a value of 1.\n # https://tools.ietf.org/html/rfc5280#section-4.1.2.1\n 'Version': cert.get_version() + 1,\n # Get size returns in bytes. The world thinks of key sizes in bits.\n 'Key Size': cert.get_pubkey().size() * 8,\n 'Serial Number': _dec2hex(cert.get_serial_number()),\n 'SHA-256 Finger Print': _pretty_hex(cert.get_fingerprint(md='sha256')),\n 'MD5 Finger Print': _pretty_hex(cert.get_fingerprint(md='md5')),\n 'SHA1 Finger Print': _pretty_hex(cert.get_fingerprint(md='sha1')),\n 'Subject': _parse_subject(cert.get_subject()),\n 'Subject Hash': _dec2hex(cert.get_subject().as_hash()),\n 'Issuer': _parse_subject(cert.get_issuer()),\n 'Issuer Hash': _dec2hex(cert.get_issuer().as_hash()),\n 'Not Before':\n cert.get_not_before().get_datetime().strftime('%Y-%m-%d %H:%M:%S'),\n 'Not After':\n cert.get_not_after().get_datetime().strftime('%Y-%m-%d %H:%M:%S'),\n 'Public Key': get_public_key(cert)\n }\n\n exts = OrderedDict()\n for ext_index in range(0, cert.get_ext_count()):\n ext = cert.get_ext_at(ext_index)\n name = ext.get_name()\n val = ext.get_value()\n if ext.get_critical():\n val = 'critical ' + val\n exts[name] = val\n\n if exts:\n ret['X509v3 Extensions'] = exts\n\n return ret", "response": "Read a certificate file and return a dict containing details of the certificate."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef read_certificates(glob_path):\n '''\n Returns a dict containing details of a all certificates matching a glob\n\n glob_path:\n A path to certificates to be read and returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.read_certificates \"/etc/pki/*.crt\"\n '''\n ret = {}\n\n for path in glob.glob(glob_path):\n if os.path.isfile(path):\n try:\n ret[path] = read_certificate(certificate=path)\n except ValueError as err:\n log.debug('Unable to read certificate %s: %s', path, err)\n\n return ret", "response": "Reads all certificates matching a glob_path and returns a dict containing details of a all certificates matching a glob_path"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a dict containing details of a certificate request.", "response": "def read_csr(csr):\n '''\n Returns a dict containing details of a certificate request.\n\n :depends: - OpenSSL command line tool\n\n csr:\n A path or PEM encoded string containing the CSR to read.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.read_csr /etc/pki/mycert.csr\n '''\n csr = _get_request_obj(csr)\n ret = {\n # X509 Version 3 has a value of 2 in the field.\n # Version 2 has a value of 1.\n # https://tools.ietf.org/html/rfc5280#section-4.1.2.1\n 'Version': csr.get_version() + 1,\n # Get size returns in bytes. The world thinks of key sizes in bits.\n 'Subject': _parse_subject(csr.get_subject()),\n 'Subject Hash': _dec2hex(csr.get_subject().as_hash()),\n 'Public Key Hash': hashlib.sha1(csr.get_pubkey().get_modulus()).hexdigest(),\n 'X509v3 Extensions': _get_csr_extensions(csr),\n }\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread a PEM encoded X509 CRL and returns a dict containing details of a certificate revocation list.", "response": "def read_crl(crl):\n '''\n Returns a dict containing details of a certificate revocation list.\n Input can be a PEM string or file path.\n\n :depends: - OpenSSL command line tool\n\n csl:\n A path or PEM encoded string containing the CSL to read.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.read_crl /etc/pki/mycrl.crl\n '''\n text = _text_or_file(crl)\n text = get_pem_entry(text, pem_type='X509 CRL')\n\n crltempfile = tempfile.NamedTemporaryFile()\n crltempfile.write(salt.utils.stringutils.to_str(text))\n crltempfile.flush()\n crlparsed = _parse_openssl_crl(crltempfile.name)\n crltempfile.close()\n\n return crlparsed"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a string containing the public key in PEM format.", "response": "def get_public_key(key, passphrase=None, asObj=False):\n '''\n Returns a string containing the public key in PEM format.\n\n key:\n A path or PEM encoded string containing a CSR, Certificate or\n Private Key from which a public key can be retrieved.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.get_public_key /etc/pki/mycert.cer\n '''\n\n if isinstance(key, M2Crypto.X509.X509):\n rsa = key.get_pubkey().get_rsa()\n text = b''\n else:\n text = _text_or_file(key)\n text = get_pem_entry(text)\n\n if text.startswith(b'-----BEGIN PUBLIC KEY-----'):\n if not asObj:\n return text\n bio = M2Crypto.BIO.MemoryBuffer()\n bio.write(text)\n rsa = M2Crypto.RSA.load_pub_key_bio(bio)\n\n bio = M2Crypto.BIO.MemoryBuffer()\n if text.startswith(b'-----BEGIN CERTIFICATE-----'):\n cert = M2Crypto.X509.load_cert_string(text)\n rsa = cert.get_pubkey().get_rsa()\n if text.startswith(b'-----BEGIN CERTIFICATE REQUEST-----'):\n csr = M2Crypto.X509.load_request_string(text)\n rsa = csr.get_pubkey().get_rsa()\n if (text.startswith(b'-----BEGIN PRIVATE KEY-----') or\n text.startswith(b'-----BEGIN RSA PRIVATE KEY-----')):\n rsa = M2Crypto.RSA.load_key_string(\n text, callback=_passphrase_callback(passphrase))\n\n if asObj:\n evppubkey = M2Crypto.EVP.PKey()\n evppubkey.assign_rsa(rsa)\n return evppubkey\n\n rsa.save_pub_key_bio(bio)\n return bio.read_all()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef write_pem(text, path, overwrite=True, pem_type=None):\n '''\n Writes out a PEM string fixing any formatting or whitespace\n issues before writing.\n\n text:\n PEM string input to be written out.\n\n path:\n Path of the file to write the pem out to.\n\n overwrite:\n If True(default), write_pem will overwrite the entire pem file.\n Set False to preserve existing private keys and dh params that may\n exist in the pem file.\n\n pem_type:\n The PEM type to be saved, for example ``CERTIFICATE`` or\n ``PUBLIC KEY``. Adding this will allow the function to take\n input that may contain multiple pem types.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.write_pem \"-----BEGIN CERTIFICATE-----MIIGMzCCBBugA...\" path=/etc/pki/mycert.crt\n '''\n with salt.utils.files.set_umask(0o077):\n text = get_pem_entry(text, pem_type=pem_type)\n _dhparams = ''\n _private_key = ''\n if pem_type and pem_type == 'CERTIFICATE' and os.path.isfile(path) and not overwrite:\n _filecontents = _text_or_file(path)\n try:\n _dhparams = get_pem_entry(_filecontents, 'DH PARAMETERS')\n except salt.exceptions.SaltInvocationError as err:\n log.debug(\"Error when getting DH PARAMETERS: %s\", err)\n log.trace(err, exc_info=err)\n try:\n _private_key = get_pem_entry(_filecontents, '(?:RSA )?PRIVATE KEY')\n except salt.exceptions.SaltInvocationError as err:\n log.debug(\"Error when getting PRIVATE KEY: %s\", err)\n log.trace(err, exc_info=err)\n with salt.utils.files.fopen(path, 'w') as _fp:\n if pem_type and pem_type == 'CERTIFICATE' and _private_key:\n _fp.write(salt.utils.stringutils.to_str(_private_key))\n _fp.write(salt.utils.stringutils.to_str(text))\n if pem_type and pem_type == 'CERTIFICATE' and _dhparams:\n _fp.write(salt.utils.stringutils.to_str(_dhparams))\n return 'PEM written to {0}'.format(path)", "response": "Writes out a PEM string to a file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_private_key(path=None,\n text=False,\n bits=2048,\n passphrase=None,\n cipher='aes_128_cbc',\n verbose=True):\n '''\n Creates a private key in PEM format.\n\n path:\n The path to write the file to, either ``path`` or ``text``\n are required.\n\n text:\n If ``True``, return the PEM text without writing to a file.\n Default ``False``.\n\n bits:\n Length of the private key in bits. Default 2048\n\n passphrase:\n Passphrase for encryting the private key\n\n cipher:\n Cipher for encrypting the private key. Has no effect if passhprase is None.\n\n verbose:\n Provide visual feedback on stdout. Default True\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.create_private_key path=/etc/pki/mykey.key\n '''\n if not path and not text:\n raise salt.exceptions.SaltInvocationError(\n 'Either path or text must be specified.')\n if path and text:\n raise salt.exceptions.SaltInvocationError(\n 'Either path or text must be specified, not both.')\n\n if verbose:\n _callback_func = M2Crypto.RSA.keygen_callback\n else:\n _callback_func = _keygen_callback\n\n # pylint: disable=no-member\n rsa = M2Crypto.RSA.gen_key(bits, M2Crypto.m2.RSA_F4, _callback_func)\n # pylint: enable=no-member\n bio = M2Crypto.BIO.MemoryBuffer()\n if passphrase is None:\n cipher = None\n rsa.save_key_bio(\n bio,\n cipher=cipher,\n callback=_passphrase_callback(passphrase))\n\n if path:\n return write_pem(\n text=bio.read_all(),\n path=path,\n pem_type='(?:RSA )?PRIVATE KEY'\n )\n else:\n return salt.utils.stringutils.to_str(bio.read_all())", "response": "Create a private key in PEM format."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a CRL for the given key - set of certificates.", "response": "def create_crl( # pylint: disable=too-many-arguments,too-many-locals\n path=None, text=False, signing_private_key=None,\n signing_private_key_passphrase=None,\n signing_cert=None, revoked=None, include_expired=False,\n days_valid=100, digest=''):\n '''\n Create a CRL\n\n :depends: - PyOpenSSL Python module\n\n path:\n Path to write the crl to.\n\n text:\n If ``True``, return the PEM text without writing to a file.\n Default ``False``.\n\n signing_private_key:\n A path or string of the private key in PEM format that will be used\n to sign this crl. This is required.\n\n signing_private_key_passphrase:\n Passphrase to decrypt the private key.\n\n signing_cert:\n A certificate matching the private key that will be used to sign\n this crl. This is required.\n\n revoked:\n A list of dicts containing all the certificates to revoke. Each dict\n represents one certificate. A dict must contain either the key\n ``serial_number`` with the value of the serial number to revoke, or\n ``certificate`` with either the PEM encoded text of the certificate,\n or a path to the certificate to revoke.\n\n The dict can optionally contain the ``revocation_date`` key. If this\n key is omitted the revocation date will be set to now. If should be a\n string in the format \"%Y-%m-%d %H:%M:%S\".\n\n The dict can also optionally contain the ``not_after`` key. This is\n redundant if the ``certificate`` key is included. If the\n ``Certificate`` key is not included, this can be used for the logic\n behind the ``include_expired`` parameter. If should be a string in\n the format \"%Y-%m-%d %H:%M:%S\".\n\n The dict can also optionally contain the ``reason`` key. This is the\n reason code for the revocation. Available choices are ``unspecified``,\n ``keyCompromise``, ``CACompromise``, ``affiliationChanged``,\n ``superseded``, ``cessationOfOperation`` and ``certificateHold``.\n\n include_expired:\n Include expired certificates in the CRL. Default is ``False``.\n\n days_valid:\n The number of days that the CRL should be valid. This sets the Next\n Update field in the CRL.\n\n digest:\n The digest to use for signing the CRL.\n This has no effect on versions of pyOpenSSL less than 0.14\n\n .. note\n\n At this time the pyOpenSSL library does not allow choosing a signing\n algorithm for CRLs See https://github.com/pyca/pyopenssl/issues/159\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.create_crl path=/etc/pki/mykey.key signing_private_key=/etc/pki/ca.key signing_cert=/etc/pki/ca.crt revoked=\"{'compromized-web-key': {'certificate': '/etc/pki/certs/www1.crt', 'revocation_date': '2015-03-01 00:00:00'}}\"\n '''\n # pyOpenSSL is required for dealing with CSLs. Importing inside these\n # functions because Client operations like creating CRLs shouldn't require\n # pyOpenSSL Note due to current limitations in pyOpenSSL it is impossible\n # to specify a digest For signing the CRL. This will hopefully be fixed\n # soon: https://github.com/pyca/pyopenssl/pull/161\n if OpenSSL is None:\n raise salt.exceptions.SaltInvocationError(\n 'Could not load OpenSSL module, OpenSSL unavailable'\n )\n crl = OpenSSL.crypto.CRL()\n\n if revoked is None:\n revoked = []\n\n for rev_item in revoked:\n if 'certificate' in rev_item:\n rev_cert = read_certificate(rev_item['certificate'])\n rev_item['serial_number'] = rev_cert['Serial Number']\n rev_item['not_after'] = rev_cert['Not After']\n\n serial_number = rev_item['serial_number'].replace(':', '')\n # OpenSSL bindings requires this to be a non-unicode string\n serial_number = salt.utils.stringutils.to_bytes(serial_number)\n\n if 'not_after' in rev_item and not include_expired:\n not_after = datetime.datetime.strptime(\n rev_item['not_after'], '%Y-%m-%d %H:%M:%S')\n if datetime.datetime.now() > not_after:\n continue\n\n if 'revocation_date' not in rev_item:\n rev_item['revocation_date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')\n\n rev_date = datetime.datetime.strptime(\n rev_item['revocation_date'], '%Y-%m-%d %H:%M:%S')\n rev_date = rev_date.strftime('%Y%m%d%H%M%SZ')\n rev_date = salt.utils.stringutils.to_bytes(rev_date)\n\n rev = OpenSSL.crypto.Revoked()\n rev.set_serial(salt.utils.stringutils.to_bytes(serial_number))\n rev.set_rev_date(salt.utils.stringutils.to_bytes(rev_date))\n\n if 'reason' in rev_item:\n # Same here for OpenSSL bindings and non-unicode strings\n reason = salt.utils.stringutils.to_str(rev_item['reason'])\n rev.set_reason(reason)\n\n crl.add_revoked(rev)\n\n signing_cert = _text_or_file(signing_cert)\n cert = OpenSSL.crypto.load_certificate(\n OpenSSL.crypto.FILETYPE_PEM,\n get_pem_entry(signing_cert, pem_type='CERTIFICATE'))\n signing_private_key = _get_private_key_obj(signing_private_key,\n passphrase=signing_private_key_passphrase).as_pem(cipher=None)\n key = OpenSSL.crypto.load_privatekey(\n OpenSSL.crypto.FILETYPE_PEM,\n get_pem_entry(signing_private_key))\n\n export_kwargs = {\n 'cert': cert,\n 'key': key,\n 'type': OpenSSL.crypto.FILETYPE_PEM,\n 'days': days_valid\n }\n if digest:\n export_kwargs['digest'] = salt.utils.stringutils.to_bytes(digest)\n else:\n log.warning('No digest specified. The default md5 digest will be used.')\n\n try:\n crltext = crl.export(**export_kwargs)\n except (TypeError, ValueError):\n log.warning('Error signing crl with specified digest. '\n 'Are you using pyopenssl 0.15 or newer? '\n 'The default md5 digest will be used.')\n export_kwargs.pop('digest', None)\n crltext = crl.export(**export_kwargs)\n\n if text:\n return crltext\n\n return write_pem(text=crltext, path=path, pem_type='X509 CRL')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nrequests a certificate to be remotely signed according to a signing policy. argdic: A dict containing all the arguments to be passed into the create_certificate function. This will become kwargs when passed to create_certificate. kwargs: kwargs delivered from publish.publish CLI Example: .. code-block:: bash salt '*' x509.sign_remote_certificate argdic=\"{'public_key': '/etc/pki/www.key', 'signing_policy': 'www'}\" __pub_id='www1'", "response": "def sign_remote_certificate(argdic, **kwargs):\n '''\n Request a certificate to be remotely signed according to a signing policy.\n\n argdic:\n A dict containing all the arguments to be passed into the\n create_certificate function. This will become kwargs when passed\n to create_certificate.\n\n kwargs:\n kwargs delivered from publish.publish\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.sign_remote_certificate argdic=\"{'public_key': '/etc/pki/www.key', 'signing_policy': 'www'}\" __pub_id='www1'\n '''\n if 'signing_policy' not in argdic:\n return 'signing_policy must be specified'\n\n if not isinstance(argdic, dict):\n argdic = ast.literal_eval(argdic)\n\n signing_policy = {}\n if 'signing_policy' in argdic:\n signing_policy = _get_signing_policy(argdic['signing_policy'])\n if not signing_policy:\n return 'Signing policy {0} does not exist.'.format(argdic['signing_policy'])\n\n if isinstance(signing_policy, list):\n dict_ = {}\n for item in signing_policy:\n dict_.update(item)\n signing_policy = dict_\n\n if 'minions' in signing_policy:\n if '__pub_id' not in kwargs:\n return 'minion sending this request could not be identified'\n matcher = 'match.glob'\n if '@' in signing_policy['minions']:\n matcher = 'match.compound'\n if not __salt__[matcher](\n signing_policy['minions'], kwargs['__pub_id']):\n return '{0} not permitted to use signing policy {1}'.format(\n kwargs['__pub_id'], argdic['signing_policy'])\n\n try:\n return create_certificate(path=None, text=True, **argdic)\n except Exception as except_: # pylint: disable=broad-except\n return six.text_type(except_)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the details of a names signing policy. Does not return the private key.", "response": "def get_signing_policy(signing_policy_name):\n '''\n Returns the details of a names signing policy, including the text of\n the public key that will be used to sign it. Does not return the\n private key.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.get_signing_policy www\n '''\n signing_policy = _get_signing_policy(signing_policy_name)\n if not signing_policy:\n return 'Signing policy {0} does not exist.'.format(signing_policy_name)\n\n if isinstance(signing_policy, list):\n dict_ = {}\n for item in signing_policy:\n dict_.update(item)\n signing_policy = dict_\n\n try:\n del signing_policy['signing_private_key']\n except KeyError:\n pass\n\n try:\n signing_policy['signing_cert'] = get_pem_entry(signing_policy['signing_cert'], 'CERTIFICATE')\n except KeyError:\n log.debug('Unable to get \"certificate\" PEM entry')\n\n return signing_policy"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_certificate(\n path=None, text=False, overwrite=True, ca_server=None, **kwargs):\n '''\n Create an X509 certificate.\n\n path:\n Path to write the certificate to.\n\n text:\n If ``True``, return the PEM text without writing to a file.\n Default ``False``.\n\n overwrite:\n If True(default), create_certificate will overwrite the entire pem\n file. Set False to preserve existing private keys and dh params that\n may exist in the pem file.\n\n kwargs:\n Any of the properties below can be included as additional\n keyword arguments.\n\n ca_server:\n Request a remotely signed certificate from ca_server. For this to\n work, a ``signing_policy`` must be specified, and that same policy\n must be configured on the ca_server (name or list of ca server). See ``signing_policy`` for\n details. Also the salt master must permit peers to call the\n ``sign_remote_certificate`` function.\n\n Example:\n\n /etc/salt/master.d/peer.conf\n\n .. code-block:: yaml\n\n peer:\n .*:\n - x509.sign_remote_certificate\n\n subject properties:\n Any of the values below can be included to set subject properties\n Any other subject properties supported by OpenSSL should also work.\n\n C:\n 2 letter Country code\n CN:\n Certificate common name, typically the FQDN.\n\n Email:\n Email address\n\n GN:\n Given Name\n\n L:\n Locality\n\n O:\n Organization\n\n OU:\n Organization Unit\n\n SN:\n SurName\n\n ST:\n State or Province\n\n signing_private_key:\n A path or string of the private key in PEM format that will be used\n to sign this certificate. If neither ``signing_cert``, ``public_key``,\n or ``csr`` are included, it will be assumed that this is a self-signed\n certificate, and the public key matching ``signing_private_key`` will\n be used to create the certificate.\n\n signing_private_key_passphrase:\n Passphrase used to decrypt the signing_private_key.\n\n signing_cert:\n A certificate matching the private key that will be used to sign this\n certificate. This is used to populate the issuer values in the\n resulting certificate. Do not include this value for\n self-signed certificates.\n\n public_key:\n The public key to be included in this certificate. This can be sourced\n from a public key, certificate, csr or private key. If a private key\n is used, the matching public key from the private key will be\n generated before any processing is done. This means you can request a\n certificate from a remote CA using a private key file as your\n public_key and only the public key will be sent across the network to\n the CA. If neither ``public_key`` or ``csr`` are specified, it will be\n assumed that this is a self-signed certificate, and the public key\n derived from ``signing_private_key`` will be used. Specify either\n ``public_key`` or ``csr``, not both. Because you can input a CSR as a\n public key or as a CSR, it is important to understand the difference.\n If you import a CSR as a public key, only the public key will be added\n to the certificate, subject or extension information in the CSR will\n be lost.\n\n public_key_passphrase:\n If the public key is supplied as a private key, this is the passphrase\n used to decrypt it.\n\n csr:\n A file or PEM string containing a certificate signing request. This\n will be used to supply the subject, extensions and public key of a\n certificate. Any subject or extensions specified explicitly will\n overwrite any in the CSR.\n\n basicConstraints:\n X509v3 Basic Constraints extension.\n\n extensions:\n The following arguments set X509v3 Extension values. If the value\n starts with ``critical``, the extension will be marked as critical.\n\n Some special extensions are ``subjectKeyIdentifier`` and\n ``authorityKeyIdentifier``.\n\n ``subjectKeyIdentifier`` can be an explicit value or it can be the\n special string ``hash``. ``hash`` will set the subjectKeyIdentifier\n equal to the SHA1 hash of the modulus of the public key in this\n certificate. Note that this is not the exact same hashing method used\n by OpenSSL when using the hash value.\n\n ``authorityKeyIdentifier`` Use values acceptable to the openssl CLI\n tools. This will automatically populate ``authorityKeyIdentifier``\n with the ``subjectKeyIdentifier`` of ``signing_cert``. If this is a\n self-signed cert these values will be the same.\n\n basicConstraints:\n X509v3 Basic Constraints\n\n keyUsage:\n X509v3 Key Usage\n\n extendedKeyUsage:\n X509v3 Extended Key Usage\n\n subjectKeyIdentifier:\n X509v3 Subject Key Identifier\n\n issuerAltName:\n X509v3 Issuer Alternative Name\n\n subjectAltName:\n X509v3 Subject Alternative Name\n\n crlDistributionPoints:\n X509v3 CRL distribution points\n\n issuingDistributionPoint:\n X509v3 Issuing Distribution Point\n\n certificatePolicies:\n X509v3 Certificate Policies\n\n policyConstraints:\n X509v3 Policy Constraints\n\n inhibitAnyPolicy:\n X509v3 Inhibit Any Policy\n\n nameConstraints:\n X509v3 Name Constraints\n\n noCheck:\n X509v3 OCSP No Check\n\n nsComment:\n Netscape Comment\n\n nsCertType:\n Netscape Certificate Type\n\n days_valid:\n The number of days this certificate should be valid. This sets the\n ``notAfter`` property of the certificate. Defaults to 365.\n\n version:\n The version of the X509 certificate. Defaults to 3. This is\n automatically converted to the version value, so ``version=3``\n sets the certificate version field to 0x2.\n\n serial_number:\n The serial number to assign to this certificate. If omitted a random\n serial number of size ``serial_bits`` is generated.\n\n serial_bits:\n The number of bits to use when randomly generating a serial number.\n Defaults to 64.\n\n algorithm:\n The hashing algorithm to be used for signing this certificate.\n Defaults to sha256.\n\n copypath:\n An additional path to copy the resulting certificate to. Can be used\n to maintain a copy of all certificates issued for revocation purposes.\n\n prepend_cn:\n If set to True, the CN and a dash will be prepended to the copypath's filename.\n\n Example:\n /etc/pki/issued_certs/www.example.com-DE:CA:FB:AD:00:00:00:00.crt\n\n signing_policy:\n A signing policy that should be used to create this certificate.\n Signing policies should be defined in the minion configuration, or in\n a minion pillar. It should be a yaml formatted list of arguments\n which will override any arguments passed to this function. If the\n ``minions`` key is included in the signing policy, only minions\n matching that pattern (see match.glob and match.compound) will be\n permitted to remotely request certificates from that policy.\n\n Example:\n\n .. code-block:: yaml\n\n x509_signing_policies:\n www:\n - minions: 'www*'\n - signing_private_key: /etc/pki/ca.key\n - signing_cert: /etc/pki/ca.crt\n - C: US\n - ST: Utah\n - L: Salt Lake City\n - basicConstraints: \"critical CA:false\"\n - keyUsage: \"critical cRLSign, keyCertSign\"\n - subjectKeyIdentifier: hash\n - authorityKeyIdentifier: keyid,issuer:always\n - days_valid: 90\n - copypath: /etc/pki/issued_certs/\n\n The above signing policy can be invoked with ``signing_policy=www``\n\n ext_mapping:\n Provide additional X509v3 extension mappings. This argument should be\n in the form of a dictionary and should include both the OID and the\n friendly name for the extension.\n\n .. versionadded:: Neon\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.create_certificate path=/etc/pki/myca.crt signing_private_key='/etc/pki/myca.key' csr='/etc/pki/myca.csr'}\n '''\n\n if not path and not text and ('testrun' not in kwargs or kwargs['testrun'] is False):\n raise salt.exceptions.SaltInvocationError(\n 'Either path or text must be specified.')\n if path and text:\n raise salt.exceptions.SaltInvocationError(\n 'Either path or text must be specified, not both.')\n\n if 'public_key_passphrase' not in kwargs:\n kwargs['public_key_passphrase'] = None\n if ca_server:\n if 'signing_policy' not in kwargs:\n raise salt.exceptions.SaltInvocationError(\n 'signing_policy must be specified'\n 'if requesting remote certificate from ca_server {0}.'\n .format(ca_server))\n if 'csr' in kwargs:\n kwargs['csr'] = get_pem_entry(\n kwargs['csr'],\n pem_type='CERTIFICATE REQUEST').replace('\\n', '')\n if 'public_key' in kwargs:\n # Strip newlines to make passing through as cli functions easier\n kwargs['public_key'] = salt.utils.stringutils.to_str(get_public_key(\n kwargs['public_key'],\n passphrase=kwargs['public_key_passphrase'])).replace('\\n', '')\n\n # Remove system entries in kwargs\n # Including listen_in and preqreuired because they are not included\n # in STATE_INTERNAL_KEYWORDS\n # for salt 2014.7.2\n for ignore in list(_STATE_INTERNAL_KEYWORDS) + ['listen_in', 'preqrequired', '__prerequired__']:\n kwargs.pop(ignore, None)\n\n if not isinstance(ca_server, list):\n ca_server = [ca_server]\n random.shuffle(ca_server)\n for server in ca_server:\n certs = __salt__['publish.publish'](\n tgt=server,\n fun='x509.sign_remote_certificate',\n arg=six.text_type(kwargs))\n if certs is None or not any(certs):\n continue\n else:\n cert_txt = certs[server]\n break\n\n if not any(certs):\n raise salt.exceptions.SaltInvocationError(\n 'ca_server did not respond'\n ' salt master must permit peers to'\n ' call the sign_remote_certificate function.')\n\n if path:\n return write_pem(\n text=cert_txt,\n overwrite=overwrite,\n path=path,\n pem_type='CERTIFICATE'\n )\n else:\n return cert_txt\n\n signing_policy = {}\n if 'signing_policy' in kwargs:\n signing_policy = _get_signing_policy(kwargs['signing_policy'])\n if isinstance(signing_policy, list):\n dict_ = {}\n for item in signing_policy:\n dict_.update(item)\n signing_policy = dict_\n\n # Overwrite any arguments in kwargs with signing_policy\n kwargs.update(signing_policy)\n\n for prop, default in six.iteritems(CERT_DEFAULTS):\n if prop not in kwargs:\n kwargs[prop] = default\n\n cert = M2Crypto.X509.X509()\n\n # X509 Version 3 has a value of 2 in the field.\n # Version 2 has a value of 1.\n # https://tools.ietf.org/html/rfc5280#section-4.1.2.1\n cert.set_version(kwargs['version'] - 1)\n\n # Random serial number if not specified\n if 'serial_number' not in kwargs:\n kwargs['serial_number'] = _dec2hex(\n random.getrandbits(kwargs['serial_bits']))\n serial_number = int(kwargs['serial_number'].replace(':', ''), 16)\n # With Python3 we occasionally end up with an INT that is greater than a C\n # long max_value. This causes an overflow error due to a bug in M2Crypto.\n # See issue: https://gitlab.com/m2crypto/m2crypto/issues/232\n # Remove this after M2Crypto fixes the bug.\n if six.PY3:\n if salt.utils.platform.is_windows():\n INT_MAX = 2147483647\n if serial_number >= INT_MAX:\n serial_number -= int(serial_number / INT_MAX) * INT_MAX\n else:\n if serial_number >= sys.maxsize:\n serial_number -= int(serial_number / sys.maxsize) * sys.maxsize\n cert.set_serial_number(serial_number)\n\n # Set validity dates\n # pylint: disable=no-member\n not_before = M2Crypto.m2.x509_get_not_before(cert.x509)\n not_after = M2Crypto.m2.x509_get_not_after(cert.x509)\n M2Crypto.m2.x509_gmtime_adj(not_before, 0)\n M2Crypto.m2.x509_gmtime_adj(not_after, 60 * 60 * 24 * kwargs['days_valid'])\n # pylint: enable=no-member\n\n # If neither public_key or csr are included, this cert is self-signed\n if 'public_key' not in kwargs and 'csr' not in kwargs:\n kwargs['public_key'] = kwargs['signing_private_key']\n if 'signing_private_key_passphrase' in kwargs:\n kwargs['public_key_passphrase'] = kwargs[\n 'signing_private_key_passphrase']\n\n csrexts = {}\n if 'csr' in kwargs:\n kwargs['public_key'] = kwargs['csr']\n csr = _get_request_obj(kwargs['csr'])\n cert.set_subject(csr.get_subject())\n csrexts = read_csr(kwargs['csr'])['X509v3 Extensions']\n\n cert.set_pubkey(get_public_key(kwargs['public_key'],\n passphrase=kwargs['public_key_passphrase'], asObj=True))\n\n subject = cert.get_subject()\n\n # pylint: disable=unused-variable\n for entry, num in six.iteritems(subject.nid):\n if entry in kwargs:\n setattr(subject, entry, kwargs[entry])\n # pylint: enable=unused-variable\n\n if 'signing_cert' in kwargs:\n signing_cert = _get_certificate_obj(kwargs['signing_cert'])\n else:\n signing_cert = cert\n cert.set_issuer(signing_cert.get_subject())\n\n if 'ext_mapping' in kwargs:\n EXT_NAME_MAPPINGS.update(kwargs['ext_mapping'])\n\n for extname, extlongname in six.iteritems(EXT_NAME_MAPPINGS):\n if (extname in kwargs or extlongname in kwargs or\n extname in csrexts or extlongname in csrexts) is False:\n continue\n\n # Use explicitly set values first, fall back to CSR values.\n extval = kwargs.get(extname) or kwargs.get(extlongname) or csrexts.get(extname) or csrexts.get(extlongname)\n\n critical = False\n if extval.startswith('critical '):\n critical = True\n extval = extval[9:]\n\n if extname == 'subjectKeyIdentifier' and 'hash' in extval:\n extval = extval.replace('hash', _get_pubkey_hash(cert))\n\n issuer = None\n if extname == 'authorityKeyIdentifier':\n issuer = signing_cert\n\n if extname == 'subjectAltName':\n extval = extval.replace('IP Address', 'IP')\n\n ext = _new_extension(\n name=extname, value=extval, critical=critical, issuer=issuer)\n if not ext.x509_ext:\n log.info('Invalid X509v3 Extension. %s: %s', extname, extval)\n continue\n\n cert.add_ext(ext)\n\n if 'signing_private_key_passphrase' not in kwargs:\n kwargs['signing_private_key_passphrase'] = None\n if 'testrun' in kwargs and kwargs['testrun'] is True:\n cert_props = read_certificate(cert)\n cert_props['Issuer Public Key'] = get_public_key(\n kwargs['signing_private_key'],\n passphrase=kwargs['signing_private_key_passphrase'])\n return cert_props\n\n if not verify_private_key(private_key=kwargs['signing_private_key'],\n passphrase=kwargs[\n 'signing_private_key_passphrase'],\n public_key=signing_cert):\n raise salt.exceptions.SaltInvocationError(\n 'signing_private_key: {0} '\n 'does no match signing_cert: {1}'.format(\n kwargs['signing_private_key'],\n kwargs.get('signing_cert', '')\n )\n )\n\n cert.sign(\n _get_private_key_obj(kwargs['signing_private_key'],\n passphrase=kwargs['signing_private_key_passphrase']),\n kwargs['algorithm']\n )\n\n if not verify_signature(cert, signing_pub_key=signing_cert):\n raise salt.exceptions.SaltInvocationError(\n 'failed to verify certificate signature')\n\n if 'copypath' in kwargs:\n if 'prepend_cn' in kwargs and kwargs['prepend_cn'] is True:\n prepend = six.text_type(kwargs['CN']) + '-'\n else:\n prepend = ''\n write_pem(text=cert.as_pem(), path=os.path.join(kwargs['copypath'],\n prepend + kwargs['serial_number'] + '.crt'),\n pem_type='CERTIFICATE')\n\n if path:\n return write_pem(\n text=cert.as_pem(),\n overwrite=overwrite,\n path=path,\n pem_type='CERTIFICATE'\n )\n else:\n return salt.utils.stringutils.to_str(cert.as_pem())", "response": "Create a new X509 certificate."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_csr(path=None, text=False, **kwargs):\n '''\n Create a certificate signing request.\n\n path:\n Path to write the certificate to.\n\n text:\n If ``True``, return the PEM text without writing to a file.\n Default ``False``.\n\n algorithm:\n The hashing algorithm to be used for signing this request. Defaults to sha256.\n\n kwargs:\n The subject, extension and version arguments from\n :mod:`x509.create_certificate `\n can be used.\n\n ext_mapping:\n Provide additional X509v3 extension mappings. This argument should be\n in the form of a dictionary and should include both the OID and the\n friendly name for the extension.\n\n .. versionadded:: Neon\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.create_csr path=/etc/pki/myca.csr public_key='/etc/pki/myca.key' CN='My Cert'\n '''\n\n if not path and not text:\n raise salt.exceptions.SaltInvocationError(\n 'Either path or text must be specified.')\n if path and text:\n raise salt.exceptions.SaltInvocationError(\n 'Either path or text must be specified, not both.')\n\n csr = M2Crypto.X509.Request()\n subject = csr.get_subject()\n\n for prop, default in six.iteritems(CERT_DEFAULTS):\n if prop not in kwargs:\n kwargs[prop] = default\n\n csr.set_version(kwargs['version'] - 1)\n\n if 'private_key' not in kwargs and 'public_key' in kwargs:\n kwargs['private_key'] = kwargs['public_key']\n log.warning(\"OpenSSL no longer allows working with non-signed CSRs. \"\n \"A private_key must be specified. Attempting to use public_key as private_key\")\n\n if 'private_key' not in kwargs:\n raise salt.exceptions.SaltInvocationError('private_key is required')\n\n if 'public_key' not in kwargs:\n kwargs['public_key'] = kwargs['private_key']\n\n if 'private_key_passphrase' not in kwargs:\n kwargs['private_key_passphrase'] = None\n if 'public_key_passphrase' not in kwargs:\n kwargs['public_key_passphrase'] = None\n if kwargs['public_key_passphrase'] and not kwargs['private_key_passphrase']:\n kwargs['private_key_passphrase'] = kwargs['public_key_passphrase']\n if kwargs['private_key_passphrase'] and not kwargs['public_key_passphrase']:\n kwargs['public_key_passphrase'] = kwargs['private_key_passphrase']\n\n csr.set_pubkey(get_public_key(kwargs['public_key'],\n passphrase=kwargs['public_key_passphrase'], asObj=True))\n\n # pylint: disable=unused-variable\n for entry, num in six.iteritems(subject.nid):\n if entry in kwargs:\n setattr(subject, entry, kwargs[entry])\n # pylint: enable=unused-variable\n\n if 'ext_mapping' in kwargs:\n EXT_NAME_MAPPINGS.update(kwargs['ext_mapping'])\n\n extstack = M2Crypto.X509.X509_Extension_Stack()\n for extname, extlongname in six.iteritems(EXT_NAME_MAPPINGS):\n if extname not in kwargs and extlongname not in kwargs:\n continue\n\n extval = kwargs.get(extname, None) or kwargs.get(extlongname, None)\n\n critical = False\n if extval.startswith('critical '):\n critical = True\n extval = extval[9:]\n\n if extname == 'subjectKeyIdentifier' and 'hash' in extval:\n extval = extval.replace('hash', _get_pubkey_hash(csr))\n\n if extname == 'subjectAltName':\n extval = extval.replace('IP Address', 'IP')\n\n if extname == 'authorityKeyIdentifier':\n continue\n\n issuer = None\n ext = _new_extension(\n name=extname, value=extval, critical=critical, issuer=issuer)\n if not ext.x509_ext:\n log.info('Invalid X509v3 Extension. %s: %s', extname, extval)\n continue\n\n extstack.push(ext)\n\n csr.add_extensions(extstack)\n csr.sign(_get_private_key_obj(kwargs['private_key'],\n passphrase=kwargs['private_key_passphrase']), kwargs['algorithm'])\n\n return write_pem(text=csr.as_pem(), path=path, pem_type='CERTIFICATE REQUEST') if path else csr.as_pem()", "response": "Create a certificate signing request."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef verify_signature(certificate, signing_pub_key=None,\n signing_pub_key_passphrase=None):\n '''\n Verify that ``certificate`` has been signed by ``signing_pub_key``\n\n certificate:\n The certificate to verify. Can be a path or string containing a\n PEM formatted certificate.\n\n signing_pub_key:\n The public key to verify, can be a string or path to a PEM formatted\n certificate, csr, or private key.\n\n signing_pub_key_passphrase:\n Passphrase to the signing_pub_key if it is an encrypted private key.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.verify_signature /etc/pki/mycert.pem \\\\\n signing_pub_key=/etc/pki/myca.crt\n '''\n cert = _get_certificate_obj(certificate)\n\n if signing_pub_key:\n signing_pub_key = get_public_key(signing_pub_key,\n passphrase=signing_pub_key_passphrase, asObj=True)\n\n return bool(cert.verify(pkey=signing_pub_key) == 1)", "response": "Verify that a certificate has been signed by a specific public key."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nvalidate a CRL against a certificate.", "response": "def verify_crl(crl, cert):\n '''\n Validate a CRL against a certificate.\n Parses openssl command line output, this is a workaround for M2Crypto's\n inability to get them from CSR objects.\n\n crl:\n The CRL to verify\n\n cert:\n The certificate to verify the CRL against\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.verify_crl crl=/etc/pki/myca.crl cert=/etc/pki/myca.crt\n '''\n if not salt.utils.path.which('openssl'):\n raise salt.exceptions.SaltInvocationError('External command \"openssl\" not found')\n\n crltext = _text_or_file(crl)\n crltext = get_pem_entry(crltext, pem_type='X509 CRL')\n crltempfile = tempfile.NamedTemporaryFile()\n crltempfile.write(salt.utils.stringutils.to_str(crltext))\n crltempfile.flush()\n\n certtext = _text_or_file(cert)\n certtext = get_pem_entry(certtext, pem_type='CERTIFICATE')\n certtempfile = tempfile.NamedTemporaryFile()\n certtempfile.write(salt.utils.stringutils.to_str(certtext))\n certtempfile.flush()\n\n cmd = ('openssl crl -noout -in {0} -CAfile {1}'.format(\n crltempfile.name, certtempfile.name))\n\n output = __salt__['cmd.run_stderr'](cmd)\n\n crltempfile.close()\n certtempfile.close()\n\n return 'verify OK' in output"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a dict containing limited details of a certificate and whether the certificate has expired.", "response": "def expired(certificate):\n '''\n Returns a dict containing limited details of a\n certificate and whether the certificate has expired.\n\n .. versionadded:: 2016.11.0\n\n certificate:\n The certificate to be read. Can be a path to a certificate file,\n or a string containing the PEM formatted text of the certificate.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.expired \"/etc/pki/mycert.crt\"\n '''\n ret = {}\n\n if os.path.isfile(certificate):\n try:\n ret['path'] = certificate\n cert = _get_certificate_obj(certificate)\n\n _now = datetime.datetime.utcnow()\n _expiration_date = cert.get_not_after().get_datetime()\n\n ret['cn'] = _parse_subject(cert.get_subject())['CN']\n\n if _expiration_date.strftime(\"%Y-%m-%d %H:%M:%S\") <= \\\n _now.strftime(\"%Y-%m-%d %H:%M:%S\"):\n ret['expired'] = True\n else:\n ret['expired'] = False\n except ValueError as err:\n log.debug('Failed to get data of expired certificate: %s', err)\n log.trace(err, exc_info=True)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef will_expire(certificate, days):\n '''\n Returns a dict containing details of a certificate and whether\n the certificate will expire in the specified number of days.\n Input can be a PEM string or file path.\n\n .. versionadded:: 2016.11.0\n\n certificate:\n The certificate to be read. Can be a path to a certificate file,\n or a string containing the PEM formatted text of the certificate.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' x509.will_expire \"/etc/pki/mycert.crt\" days=30\n '''\n ts_pt = \"%Y-%m-%d %H:%M:%S\"\n ret = {}\n\n if os.path.isfile(certificate):\n try:\n ret['path'] = certificate\n ret['check_days'] = days\n\n cert = _get_certificate_obj(certificate)\n\n _check_time = datetime.datetime.utcnow() + datetime.timedelta(days=days)\n _expiration_date = cert.get_not_after().get_datetime()\n\n ret['cn'] = _parse_subject(cert.get_subject())['CN']\n ret['will_expire'] = _expiration_date.strftime(ts_pt) <= _check_time.strftime(ts_pt)\n except ValueError as err:\n log.debug('Unable to return details of a sertificate expiration: %s', err)\n log.trace(err, exc_info=True)\n\n return ret", "response": "Returns a dict containing details of a certificate and whether it will expire in the specified number of days."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconnect to a Django database through the ORM and retrieve model fields :type pillar_name: str :param pillar_name: The name of the pillar to be returned :type project_path: str :param project_path: The full path to your Django project (the directory manage.py is in) :type settings_module: str :param settings_module: The settings module for your project. This can be found in your manage.py file :type django_app: str :param django_app: A dictionary containing your apps, models, and fields :type env: str :param env: The full path to the virtualenv for your Django project :type env_file: str :param env_file: An optional bash file that sets up your environment. The file is run in a subprocess and the changed variables are then added", "response": "def ext_pillar(minion_id, # pylint: disable=W0613\n pillar, # pylint: disable=W0613\n pillar_name,\n project_path,\n settings_module,\n django_app,\n env=None,\n env_file=None,\n *args, # pylint: disable=W0613\n **kwargs): # pylint: disable=W0613\n '''\n Connect to a Django database through the ORM and retrieve model fields\n\n :type pillar_name: str\n :param pillar_name: The name of the pillar to be returned\n\n :type project_path: str\n :param project_path: The full path to your Django project (the directory\n manage.py is in)\n\n :type settings_module: str\n :param settings_module: The settings module for your project. This can be\n found in your manage.py file\n\n :type django_app: str\n :param django_app: A dictionary containing your apps, models, and fields\n\n :type env: str\n :param env: The full path to the virtualenv for your Django project\n\n :type env_file: str\n :param env_file: An optional bash file that sets up your environment. The\n file is run in a subprocess and the changed variables are then added\n '''\n\n if not os.path.isdir(project_path):\n log.error('Django project dir: \\'%s\\' not a directory!', project_path)\n return {}\n if HAS_VIRTUALENV and env is not None and os.path.isdir(env):\n for path in virtualenv.path_locations(env):\n if not os.path.isdir(path):\n log.error('Virtualenv %s not a directory!', path)\n return {}\n # load the virtualenv first\n sys.path.insert(0,\n os.path.join(\n virtualenv.path_locations(env)[1],\n 'site-packages'))\n\n # load the django project\n sys.path.append(project_path)\n\n os.environ['DJANGO_SETTINGS_MODULE'] = settings_module\n\n if env_file is not None:\n import subprocess\n\n base_env = {}\n proc = subprocess.Popen(['bash', '-c', 'env'], stdout=subprocess.PIPE)\n for line in proc.stdout:\n (key, _, value) = salt.utils.stringutils.to_str(line).partition('=')\n base_env[key] = value\n\n command = ['bash', '-c', 'source {0} && env'.format(env_file)]\n proc = subprocess.Popen(command, stdout=subprocess.PIPE)\n\n for line in proc.stdout:\n (key, _, value) = salt.utils.stringutils.to_str(line).partition('=')\n # only add a key if it is different or doesn't already exist\n if key not in base_env or base_env[key] != value:\n os.environ[key] = value.rstrip('\\n')\n log.debug('Adding %s = %s to Django environment', key, value.rstrip('\\n'))\n\n try:\n from django.db.models.loading import get_model\n\n django_pillar = {}\n\n for proj_app, models in six.iteritems(django_app):\n _, _, app = proj_app.rpartition('.')\n django_pillar[app] = {}\n for model_name, model_meta in six.iteritems(models):\n model_orm = get_model(app, model_name)\n if model_orm is None:\n raise salt.exceptions.SaltException(\n \"Django model '{0}' not found in app '{1}'.\"\n .format(app, model_name))\n\n pillar_for_model = django_pillar[app][model_orm.__name__] = {}\n\n name_field = model_meta['name']\n fields = model_meta['fields']\n\n if 'filter' in model_meta:\n qs = (model_orm.objects\n .filter(**model_meta['filter'])\n .values(*fields))\n else:\n qs = model_orm.objects.values(*fields)\n\n for model in qs:\n # Check that the human-friendly name given is valid (will\n # be able to pick up a value from the query) and unique\n # (since we're using it as the key in a dictionary)\n if name_field not in model:\n raise salt.exceptions.SaltException(\n \"Name '{0}' not found in returned fields.\".format(\n name_field))\n\n if model[name_field] in pillar_for_model:\n raise salt.exceptions.SaltException(\n \"Value for '{0}' is not unique: {0}\".format(\n model[name_field]))\n\n pillar_for_model[model[name_field]] = model\n\n return {pillar_name: django_pillar}\n except ImportError as e:\n log.error('Failed to import library: %s', e)\n return {}\n except Exception as e:\n log.error('Failed on Error: %s', e)\n log.debug('django_orm traceback', exc_info=True)\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of virtual machine names on the minion", "response": "def list_domains():\n '''\n Return a list of virtual machine names on the minion\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.list_domains\n '''\n data = __salt__['vmadm.list'](keyed=True)\n vms = [\"UUID TYPE RAM STATE ALIAS\"]\n for vm in data:\n vms.append(\"{vmuuid}{vmtype}{vmram}{vmstate}{vmalias}\".format(\n vmuuid=vm.ljust(38),\n vmtype=data[vm]['type'].ljust(6),\n vmram=data[vm]['ram'].ljust(9),\n vmstate=data[vm]['state'].ljust(18),\n vmalias=data[vm]['alias'],\n ))\n return vms"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef vm_virt_type(domain):\n '''\n Return VM virtualization type : OS or KVM\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.vm_virt_type \n '''\n ret = __salt__['vmadm.lookup'](search=\"uuid={uuid}\".format(uuid=domain), order='type')\n if not ret:\n raise CommandExecutionError(\"We can't determine the type of this VM\")\n\n return ret[0]['type']", "response": "Return the virtualization type of this VM"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nchange the amount of memory allocated to VM.", "response": "def setmem(domain, memory):\n '''\n Change the amount of memory allocated to VM.\n is to be specified in MB.\n\n Note for KVM : this would require a restart of the VM.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.setmem 512\n '''\n vmtype = vm_virt_type(domain)\n if vmtype == 'OS':\n return __salt__['vmadm.update'](vm=domain, max_physical_memory=memory)\n elif vmtype == 'LX':\n return __salt__['vmadm.update'](vm=domain, max_physical_memory=memory)\n elif vmtype == 'KVM':\n log.warning('Changes will be applied after the VM restart.')\n return __salt__['vmadm.update'](vm=domain, ram=memory)\n else:\n raise CommandExecutionError('Unknown VM type')\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list off MAC addresses from the named VM CLI Example : bash salt '*' virt. get_macs ", "response": "def get_macs(domain):\n '''\n Return a list off MAC addresses from the named VM\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_macs \n '''\n macs = []\n ret = __salt__['vmadm.lookup'](search=\"uuid={uuid}\".format(uuid=domain), order='nics')\n if not ret:\n raise CommandExecutionError('We can\\'t find the MAC address of this VM')\n else:\n for nic in ret[0]['nics']:\n macs.append(nic['mac'])\n return macs"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a map of FACLs on specified file", "response": "def getfacl(*args, **kwargs):\n '''\n Return (extremely verbose) map of FACLs on specified file(s)\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' acl.getfacl /tmp/house/kitchen\n salt '*' acl.getfacl /tmp/house/kitchen /tmp/house/livingroom\n salt '*' acl.getfacl /tmp/house/kitchen /tmp/house/livingroom recursive=True\n '''\n recursive = kwargs.pop('recursive', False)\n\n _raise_on_no_files(*args)\n\n ret = {}\n cmd = 'getfacl --absolute-names'\n if recursive:\n cmd += ' -R'\n for dentry in args:\n cmd += ' \"{0}\"'.format(dentry)\n out = __salt__['cmd.run'](cmd, python_shell=False).splitlines()\n dentry = ''\n for line in out:\n if not line:\n continue\n elif line.startswith('getfacl'):\n continue\n elif line.startswith('#'):\n comps = line.replace('# ', '').split(': ')\n if comps[0] == 'file':\n dentry = comps[1]\n ret[dentry] = {'comment': {},\n 'user': [],\n 'group': []}\n ret[dentry]['comment'][comps[0]] = comps[1]\n if comps[0] == 'flags':\n flags = list(comps[1])\n if flags[0] == 's':\n ret[dentry]['suid'] = True\n if flags[1] == 's':\n ret[dentry]['sgid'] = True\n if flags[2] == 't':\n ret[dentry]['sticky'] = True\n else:\n vals = _parse_acl(acl=line,\n user=ret[dentry]['comment']['owner'],\n group=ret[dentry]['comment']['group'])\n acl_type = vals['type']\n del vals['type']\n for entity in ('user', 'group'):\n if entity in vals:\n usergroup = vals[entity]\n del vals[entity]\n if acl_type == 'acl':\n ret[dentry][entity].append({usergroup: vals})\n elif acl_type == 'default':\n if 'defaults' not in ret[dentry]:\n ret[dentry]['defaults'] = {}\n if entity not in ret[dentry]['defaults']:\n ret[dentry]['defaults'][entity] = []\n ret[dentry]['defaults'][entity].append({usergroup: vals})\n for entity in ('other', 'mask'):\n if entity in vals:\n del vals[entity]\n if acl_type == 'acl':\n ret[dentry][entity] = [{\"\": vals}]\n elif acl_type == 'default':\n if 'defaults' not in ret[dentry]:\n ret[dentry]['defaults'] = {}\n ret[dentry]['defaults'][entity] = [{\"\": vals}]\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _parse_acl(acl, user, group):\n '''\n Parse a single ACL rule\n '''\n comps = acl.split(':')\n vals = {}\n\n # What type of rule is this?\n vals['type'] = 'acl'\n if comps[0] == 'default':\n vals['type'] = 'default'\n comps.pop(0)\n\n # If a user is not specified, use the owner of the file\n if comps[0] == 'user' and not comps[1]:\n comps[1] = user\n elif comps[0] == 'group' and not comps[1]:\n comps[1] = group\n vals[comps[0]] = comps[1]\n\n # Set the permissions fields\n octal = 0\n vals['permissions'] = {}\n if 'r' in comps[-1]:\n octal += 4\n vals['permissions']['read'] = True\n else:\n vals['permissions']['read'] = False\n if 'w' in comps[-1]:\n octal += 2\n vals['permissions']['write'] = True\n else:\n vals['permissions']['write'] = False\n if 'x' in comps[-1]:\n octal += 1\n vals['permissions']['execute'] = True\n else:\n vals['permissions']['execute'] = False\n vals['octal'] = octal\n\n return vals", "response": "Parse a single ACL rule into a dictionary."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves all FACLs from the specified file", "response": "def wipefacls(*args, **kwargs):\n '''\n Remove all FACLs from the specified file(s)\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' acl.wipefacls /tmp/house/kitchen\n salt '*' acl.wipefacls /tmp/house/kitchen /tmp/house/livingroom\n salt '*' acl.wipefacls /tmp/house/kitchen /tmp/house/livingroom recursive=True\n '''\n recursive = kwargs.pop('recursive', False)\n\n _raise_on_no_files(*args)\n cmd = 'setfacl -b'\n if recursive:\n cmd += ' -R'\n for dentry in args:\n cmd += ' \"{0}\"'.format(dentry)\n __salt__['cmd.run'](cmd, python_shell=False)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef modfacl(acl_type, acl_name='', perms='', *args, **kwargs):\n '''\n Add or modify a FACL for the specified file(s)\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' acl.modfacl user myuser rwx /tmp/house/kitchen\n salt '*' acl.modfacl default:group mygroup rx /tmp/house/kitchen\n salt '*' acl.modfacl d:u myuser 7 /tmp/house/kitchen\n salt '*' acl.modfacl g mygroup 0 /tmp/house/kitchen /tmp/house/livingroom\n salt '*' acl.modfacl user myuser rwx /tmp/house/kitchen recursive=True\n salt '*' acl.modfacl user myuser rwx /tmp/house/kitchen raise_err=True\n '''\n recursive = kwargs.pop('recursive', False)\n raise_err = kwargs.pop('raise_err', False)\n\n _raise_on_no_files(*args)\n\n cmd = 'setfacl'\n if recursive:\n cmd += ' -R' # -R must come first as -m needs the acl_* arguments that come later\n\n cmd += ' -m'\n\n cmd = '{0} {1}:{2}:{3}'.format(cmd, _acl_prefix(acl_type), acl_name, perms)\n\n for dentry in args:\n cmd += ' \"{0}\"'.format(dentry)\n __salt__['cmd.run'](cmd, python_shell=False, raise_err=raise_err)\n return True", "response": "Add or modify a FACL for the specified file"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nremove specific FACL from the specified file", "response": "def delfacl(acl_type, acl_name='', *args, **kwargs):\n '''\n Remove specific FACL from the specified file(s)\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' acl.delfacl user myuser /tmp/house/kitchen\n salt '*' acl.delfacl default:group mygroup /tmp/house/kitchen\n salt '*' acl.delfacl d:u myuser /tmp/house/kitchen\n salt '*' acl.delfacl g myuser /tmp/house/kitchen /tmp/house/livingroom\n salt '*' acl.delfacl user myuser /tmp/house/kitchen recursive=True\n '''\n recursive = kwargs.pop('recursive', False)\n\n _raise_on_no_files(*args)\n\n cmd = 'setfacl'\n if recursive:\n cmd += ' -R'\n\n cmd += ' -x'\n\n cmd = '{0} {1}:{2}'.format(cmd, _acl_prefix(acl_type), acl_name)\n\n for dentry in args:\n cmd += ' \"{0}\"'.format(dentry)\n __salt__['cmd.run'](cmd, python_shell=False)\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure that given user is present in the user database.", "response": "def present(name,\n passwd,\n admin=False,\n grants=None,\n **client_args):\n '''\n Ensure that given user is present.\n\n name\n Name of the user to manage\n\n passwd\n Password of the user\n\n admin : False\n Whether the user should have cluster administration\n privileges or not.\n\n grants\n Optional - Dict of database:privilege items associated with\n the user. Example:\n\n grants:\n foo_db: read\n bar_db: all\n\n **Example:**\n\n .. code-block:: yaml\n\n example user present in influxdb:\n influxdb_user.present:\n - name: example\n - passwd: somepassword\n - admin: False\n - grants:\n foo_db: read\n bar_db: all\n '''\n create = False\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'User {0} is present and up to date'.format(name)}\n\n if not __salt__['influxdb.user_exists'](name, **client_args):\n create = True\n if __opts__['test']:\n ret['comment'] = 'User {0} will be created'.format(name)\n ret['result'] = None\n return ret\n else:\n if not __salt__['influxdb.create_user'](\n name, passwd, admin=admin, **client_args):\n ret['comment'] = 'Failed to create user {0}'.format(name)\n ret['result'] = False\n return ret\n else:\n user = __salt__['influxdb.user_info'](name, **client_args)\n\n if user['admin'] != admin:\n if not __opts__['test']:\n if admin:\n __salt__['influxdb.grant_admin_privileges'](\n name, **client_args)\n else:\n __salt__['influxdb.revoke_admin_privileges'](\n name, **client_args)\n\n if admin != __salt__['influxdb.user_info'](\n name, **client_args)['admin']:\n ret['comment'] = 'Failed to set admin privilege to ' \\\n 'user {0}'.format(name)\n ret['result'] = False\n return ret\n ret['changes']['Admin privileges'] = admin\n\n if grants:\n db_privileges = __salt__['influxdb.list_privileges'](\n name, **client_args)\n for database, privilege in grants.items():\n privilege = privilege.lower()\n if privilege != db_privileges.get(database, privilege):\n if not __opts__['test']:\n __salt__['influxdb.revoke_privilege'](\n database, 'all', name, **client_args)\n del db_privileges[database]\n if database not in db_privileges:\n ret['changes']['Grant on database {0} to user {1}'.format(\n database, name)] = privilege\n if not __opts__['test']:\n __salt__['influxdb.grant_privilege'](\n database, privilege, name, **client_args)\n\n if ret['changes']:\n if create:\n ret['comment'] = 'Created user {0}'.format(name)\n ret['changes'][name] = 'User created'\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'User {0} will be updated with the ' \\\n 'following changes:'.format(name)\n for k, v in ret['changes'].items():\n ret['comment'] += '\\n{0} => {1}'.format(k, v)\n ret['changes'] = {}\n else:\n ret['comment'] = 'Updated user {0}'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nactivating the firmware backup image.", "response": "def activate_backup_image(reset=False):\n '''\n Activates the firmware backup image.\n\n CLI Example:\n\n Args:\n reset(bool): Reset the CIMC device on activate.\n\n .. code-block:: bash\n\n salt '*' cimc.activate_backup_image\n salt '*' cimc.activate_backup_image reset=True\n\n '''\n\n dn = \"sys/rack-unit-1/mgmt/fw-boot-def/bootunit-combined\"\n\n r = \"no\"\n\n if reset is True:\n r = \"yes\"\n\n inconfig = \"\"\"\"\"\".format(r)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a user in the system.", "response": "def create_user(uid=None, username=None, password=None, priv=None):\n '''\n Create a CIMC user with username and password.\n\n Args:\n uid(int): The user ID slot to create the user account in.\n\n username(str): The name of the user.\n\n password(str): The clear text password of the user.\n\n priv(str): The privilege level of the user.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.create_user 11 username=admin password=foobar priv=admin\n\n '''\n\n if not uid:\n raise salt.exceptions.CommandExecutionError(\"The user ID must be specified.\")\n\n if not username:\n raise salt.exceptions.CommandExecutionError(\"The username must be specified.\")\n\n if not password:\n raise salt.exceptions.CommandExecutionError(\"The password must be specified.\")\n\n if not priv:\n raise salt.exceptions.CommandExecutionError(\"The privilege level must be specified.\")\n\n dn = \"sys/user-ext/user-{0}\".format(uid)\n\n inconfig = \"\"\"\"\"\".format(uid,\n username,\n priv,\n password)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nmount a remote file through a remote share.", "response": "def mount_share(name=None,\n remote_share=None,\n remote_file=None,\n mount_type=\"nfs\",\n username=None,\n password=None):\n '''\n Mounts a remote file through a remote share. Currently, this feature is supported in version 1.5 or greater.\n The remote share can be either NFS, CIFS, or WWW.\n\n Some of the advantages of CIMC Mounted vMedia include:\n Communication between mounted media and target stays local (inside datacenter)\n Media mounts can be scripted/automated\n No vKVM requirements for media connection\n Multiple share types supported\n Connections supported through all CIMC interfaces\n\n Note: CIMC Mounted vMedia is enabled through BIOS configuration.\n\n Args:\n name(str): The name of the volume on the CIMC device.\n\n remote_share(str): The file share link that will be used to mount the share. This can be NFS, CIFS, or WWW. This\n must be the directory path and not the full path to the remote file.\n\n remote_file(str): The name of the remote file to mount. It must reside within remote_share.\n\n mount_type(str): The type of share to mount. Valid options are nfs, cifs, and www.\n\n username(str): An optional requirement to pass credentials to the remote share. If not provided, an\n unauthenticated connection attempt will be made.\n\n password(str): An optional requirement to pass a password to the remote share. If not provided, an\n unauthenticated connection attempt will be made.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.mount_share name=WIN7 remote_share=10.xxx.27.xxx:/nfs remote_file=sl1huu.iso\n\n salt '*' cimc.mount_share name=WIN7 remote_share=10.xxx.27.xxx:/nfs remote_file=sl1huu.iso username=bob password=badpassword\n\n '''\n\n if not name:\n raise salt.exceptions.CommandExecutionError(\"The share name must be specified.\")\n\n if not remote_share:\n raise salt.exceptions.CommandExecutionError(\"The remote share path must be specified.\")\n\n if not remote_file:\n raise salt.exceptions.CommandExecutionError(\"The remote file name must be specified.\")\n\n if username and password:\n mount_options = \" mountOptions='username={0},password={1}'\".format(username, password)\n else:\n mount_options = \"\"\n\n dn = 'sys/svc-ext/vmedia-svc/vmmap-{0}'.format(name)\n inconfig = \"\"\"\"\"\".format(name, mount_type, mount_options, remote_file, remote_share)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_hostname(hostname=None):\n '''\n Sets the hostname on the server.\n\n .. versionadded:: 2019.2.0\n\n Args:\n hostname(str): The new hostname to set.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.set_hostname foobar\n\n '''\n if not hostname:\n raise salt.exceptions.CommandExecutionError(\"Hostname option must be provided.\")\n\n dn = \"sys/rack-unit-1/mgmt/if-1\"\n inconfig = \"\"\"\"\"\".format(hostname)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n try:\n if ret['outConfig']['mgmtIf'][0]['status'] == 'modified':\n return True\n else:\n return False\n except Exception as err:\n return False", "response": "Sets the hostname on the server."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_logging_levels(remote=None, local=None):\n '''\n Sets the logging levels of the CIMC devices. The logging levels must match\n the following options: emergency, alert, critical, error, warning, notice,\n informational, debug.\n\n .. versionadded:: 2019.2.0\n\n Args:\n remote(str): The logging level for SYSLOG logs.\n\n local(str): The logging level for the local device.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.set_logging_levels remote=error local=notice\n\n '''\n\n logging_options = ['emergency',\n 'alert',\n 'critical',\n 'error',\n 'warning',\n 'notice',\n 'informational',\n 'debug']\n\n query = \"\"\n\n if remote:\n if remote in logging_options:\n query += ' remoteSeverity=\"{0}\"'.format(remote)\n else:\n raise salt.exceptions.CommandExecutionError(\"Remote Severity option is not valid.\")\n\n if local:\n if local in logging_options:\n query += ' localSeverity=\"{0}\"'.format(local)\n else:\n raise salt.exceptions.CommandExecutionError(\"Local Severity option is not valid.\")\n\n dn = \"sys/svc-ext/syslog\"\n inconfig = \"\"\"\"\"\".format(query)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret", "response": "Sets the logging levels for the remote and local devices."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_ntp_server(server1='', server2='', server3='', server4=''):\n '''\n Sets the NTP servers configuration. This will also enable the client NTP service.\n\n Args:\n server1(str): The first IP address or FQDN of the NTP servers.\n\n server2(str): The second IP address or FQDN of the NTP servers.\n\n server3(str): The third IP address or FQDN of the NTP servers.\n\n server4(str): The fourth IP address or FQDN of the NTP servers.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.set_ntp_server 10.10.10.1\n\n salt '*' cimc.set_ntp_server 10.10.10.1 foo.bar.com\n\n '''\n\n dn = \"sys/svc-ext/ntp-svc\"\n inconfig = \"\"\"\"\"\".format(server1, server2, server3, server4)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret", "response": "Sets the NTP servers configuration."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_power_configuration(policy=None, delayType=None, delayValue=None):\n '''\n Sets the power configuration on the device. This is only available for some\n C-Series servers.\n\n .. versionadded:: 2019.2.0\n\n Args:\n policy(str): The action to be taken when chassis power is restored after\n an unexpected power loss. This can be one of the following:\n\n reset: The server is allowed to boot up normally when power is\n restored. The server can restart immediately or, optionally, after a\n fixed or random delay.\n\n stay-off: The server remains off until it is manually restarted.\n\n last-state: The server restarts and the system attempts to restore\n any processes that were running before power was lost.\n\n delayType(str): If the selected policy is reset, the restart can be\n delayed with this option. This can be one of the following:\n\n fixed: The server restarts after a fixed delay.\n\n random: The server restarts after a random delay.\n\n delayValue(int): If a fixed delay is selected, once chassis power is\n restored and the Cisco IMC has finished rebooting, the system waits for\n the specified number of seconds before restarting the server. Enter an\n integer between 0 and 240.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.set_power_configuration stay-off\n\n salt '*' cimc.set_power_configuration reset fixed 0\n\n '''\n\n query = \"\"\n if policy == \"reset\":\n query = ' vpResumeOnACPowerLoss=\"reset\"'\n if delayType:\n if delayType == \"fixed\":\n query += ' delayType=\"fixed\"'\n if delayValue:\n query += ' delay=\"{0}\"'.format(delayValue)\n elif delayType == \"random\":\n query += ' delayType=\"random\"'\n else:\n raise salt.exceptions.CommandExecutionError(\"Invalid delay type entered.\")\n elif policy == \"stay-off\":\n query = ' vpResumeOnACPowerLoss=\"reset\"'\n elif policy == \"last-state\":\n query = ' vpResumeOnACPowerLoss=\"last-state\"'\n else:\n raise salt.exceptions.CommandExecutionError(\"The power state must be specified.\")\n\n dn = \"sys/rack-unit-1/board/Resume-on-AC-power-loss\"\n inconfig = \"\"\"\n \"\"\".format(query)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret", "response": "Sets the power configuration on the specified chassis."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting the SYSLOG server on the host.", "response": "def set_syslog_server(server=None, type=\"primary\"):\n '''\n Set the SYSLOG server on the host.\n\n Args:\n server(str): The hostname or IP address of the SYSLOG server.\n\n type(str): Specifies the type of SYSLOG server. This can either be primary (default) or secondary.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.set_syslog_server foo.bar.com\n\n salt '*' cimc.set_syslog_server foo.bar.com primary\n\n salt '*' cimc.set_syslog_server foo.bar.com secondary\n\n '''\n\n if not server:\n raise salt.exceptions.CommandExecutionError(\"The SYSLOG server must be specified.\")\n\n if type == \"primary\":\n dn = \"sys/svc-ext/syslog/client-primary\"\n inconfig = \"\"\" \"\"\".format(server)\n elif type == \"secondary\":\n dn = \"sys/svc-ext/syslog/client-secondary\"\n inconfig = \"\"\" \"\"\".format(server)\n else:\n raise salt.exceptions.CommandExecutionError(\"The SYSLOG type must be either primary or secondary.\")\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsetting a user in the current directory.", "response": "def set_user(uid=None, username=None, password=None, priv=None, status=None):\n '''\n Sets a CIMC user with specified configurations.\n\n .. versionadded:: 2019.2.0\n\n Args:\n uid(int): The user ID slot to create the user account in.\n\n username(str): The name of the user.\n\n password(str): The clear text password of the user.\n\n priv(str): The privilege level of the user.\n\n status(str): The account status of the user.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.set_user 11 username=admin password=foobar priv=admin active\n\n '''\n\n conf = \"\"\n if not uid:\n raise salt.exceptions.CommandExecutionError(\"The user ID must be specified.\")\n\n if status:\n conf += ' accountStatus=\"{0}\"'.format(status)\n\n if username:\n conf += ' name=\"{0}\"'.format(username)\n\n if priv:\n conf += ' priv=\"{0}\"'.format(priv)\n\n if password:\n conf += ' pwd=\"{0}\"'.format(password)\n\n dn = \"sys/user-ext/user-{0}\".format(uid)\n\n inconfig = \"\"\"\"\"\".format(uid,\n conf)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef tftp_update_bios(server=None, path=None):\n '''\n Update the BIOS firmware through TFTP.\n\n Args:\n server(str): The IP address or hostname of the TFTP server.\n\n path(str): The TFTP path and filename for the BIOS image.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cimc.tftp_update_bios foo.bar.com HP-SL2.cap\n\n '''\n\n if not server:\n raise salt.exceptions.CommandExecutionError(\"The server name must be specified.\")\n\n if not path:\n raise salt.exceptions.CommandExecutionError(\"The TFTP path must be specified.\")\n\n dn = \"sys/rack-unit-1/bios/fw-updatable\"\n\n inconfig = \"\"\"\"\"\".format(server, path)\n\n ret = __proxy__['cimc.set_config_modify'](dn, inconfig, False)\n\n return ret", "response": "Update the BIOS firmware through TFTP."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns True if the acl need to be updated False otherwise", "response": "def _acl_changes(name, id=None, type=None, rules=None, consul_url=None, token=None):\n '''\n return True if the acl need to be update, False if it doesn't need to be update\n '''\n info = __salt__['consul.acl_info'](id=id, token=token, consul_url=consul_url)\n\n if info['res'] and info['data'][0]['Name'] != name:\n return True\n elif info['res'] and info['data'][0]['Rules'] != rules:\n return True\n elif info['res'] and info['data'][0]['Type'] != type:\n return True\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _acl_exists(name=None, id=None, token=None, consul_url=None):\n '''\n Check the acl exists by using the name or the ID,\n name is ignored if ID is specified,\n if only Name is used the ID associated with it is returned\n '''\n\n ret = {'result': False, 'id': None}\n\n if id:\n info = __salt__['consul.acl_info'](id=id, token=token, consul_url=consul_url)\n elif name:\n info = __salt__['consul.acl_list'](token=token, consul_url=consul_url)\n else:\n return ret\n\n if info.get('data'):\n for acl in info['data']:\n if id and acl['ID'] == id:\n ret['result'] = True\n ret['id'] = id\n elif name and acl['Name'] == name:\n ret['result'] = True\n ret['id'] = acl['ID']\n\n return ret", "response": "Check the acl exists by using the name or the ID"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef acl_present(name, id=None, token=None, type=\"client\", rules=\"\", consul_url='http://localhost:8500'):\n '''\n Ensure the ACL is present\n\n name\n Specifies a human-friendly name for the ACL token.\n\n id\n Specifies the ID of the ACL.\n\n type: client\n Specifies the type of ACL token. Valid values are: client and management.\n\n rules\n Specifies rules for this ACL token.\n\n consul_url : http://locahost:8500\n consul URL to query\n\n .. note::\n For more information https://www.consul.io/api/acl.html#create-acl-token, https://www.consul.io/api/acl.html#update-acl-token\n '''\n\n ret = {\n 'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'ACL \"{0}\" exists and is up to date'.format(name)}\n\n exists = _acl_exists(name, id, token, consul_url)\n\n if not exists['result']:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = \"the acl doesn't exist, it will be created\"\n return ret\n\n create = __salt__['consul.acl_create'](name=name, id=id, token=token, type=type, rules=rules, consul_url=consul_url)\n if create['res']:\n ret['result'] = True\n ret['comment'] = \"the acl has been created\"\n elif not create['res']:\n ret['result'] = False\n ret['comment'] = \"failed to create the acl\"\n elif exists['result']:\n changes = _acl_changes(name=name, id=exists['id'], token=token, type=type, rules=rules, consul_url=consul_url)\n if changes:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = \"the acl exists and will be updated\"\n return ret\n\n update = __salt__['consul.acl_update'](name=name, id=exists['id'], token=token, type=type, rules=rules, consul_url=consul_url)\n if update['res']:\n ret['result'] = True\n ret['comment'] = \"the acl has been updated\"\n elif not update['res']:\n ret['result'] = False\n ret['comment'] = \"failed to update the acl\"\n\n return ret", "response": "Ensure the ACL is present and is up to date."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring the ACL is absent", "response": "def acl_absent(name, id=None, token=None, consul_url='http://localhost:8500'):\n '''\n Ensure the ACL is absent\n\n name\n Specifies a human-friendly name for the ACL token.\n\n id\n Specifies the ID of the ACL.\n\n token\n token to authenticate you Consul query\n\n consul_url : http://locahost:8500\n consul URL to query\n\n .. note::\n For more information https://www.consul.io/api/acl.html#delete-acl-token\n\n '''\n ret = {\n 'name': id,\n 'changes': {},\n 'result': True,\n 'comment': 'ACL \"{0}\" does not exist'.format(id)}\n\n exists = _acl_exists(name, id, token, consul_url)\n if exists['result']:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = \"the acl exists, it will be deleted\"\n return ret\n\n delete = __salt__['consul.acl_delete'](id=exists['id'], token=token, consul_url=consul_url)\n if delete['res']:\n ret['result'] = True\n ret['comment'] = \"the acl has been deleted\"\n elif not delete['res']:\n ret['result'] = False\n ret['comment'] = \"failed to delete the acl\"\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _linux_nqn():\n '''\n Return NVMe NQN from a Linux host.\n '''\n ret = []\n\n initiator = '/etc/nvme/hostnqn'\n try:\n with salt.utils.files.fopen(initiator, 'r') as _nvme:\n for line in _nvme:\n line = line.strip()\n if line.startswith('nqn.'):\n ret.append(line)\n except IOError as ex:\n if ex.errno != errno.ENOENT:\n log.debug(\"Error while accessing '%s': %s\", initiator, ex)\n\n return ret", "response": "Return NVMe NQN from a Linux host."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate or manage a java keystore. name The path to the keystore file passphrase The password to the keystore entries A list containing an alias, certificate, and optional private_key. The certificate and private_key can be a file or a string .. code-block:: yaml - entries: - alias: hostname2 certificate: /path/to/cert.crt private_key: /path/to/key.key - alias: stringhost certificate: | -----BEGIN CERTIFICATE----- MIICEjCCAXsCAg36MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG ... 2VguKv4SWjRFoRkIfIlHX0qVviMhSlNy2ioFLy7JcPZb+v3ftDGywUqcBiVDoea0 -----END CERTIFICATE----- force_remove If True will cause the state to remove any entries found in the keystore which are not defined in the state. The default is False. Example .. code-block:: yaml define_keystore: keystore.managed: - name: /path/to/keystore - passphrase: changeit - force_remove: True - entries: - alias: hostname1 certificate: /path/to/cert.crt - alias: remotehost certificate: /path/to/cert2.crt private_key: /path/to/key2.key - alias: pillarhost certificate: {{ salt.pillar.get('path:to:cert') }}", "response": "def managed(name, passphrase, entries, force_remove=False):\n '''\n Create or manage a java keystore.\n\n name\n The path to the keystore file\n\n passphrase\n The password to the keystore\n\n entries\n A list containing an alias, certificate, and optional private_key.\n The certificate and private_key can be a file or a string\n\n .. code-block:: yaml\n\n - entries:\n - alias: hostname2\n certificate: /path/to/cert.crt\n private_key: /path/to/key.key\n - alias: stringhost\n certificate: |\n -----BEGIN CERTIFICATE-----\n MIICEjCCAXsCAg36MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\n ...\n 2VguKv4SWjRFoRkIfIlHX0qVviMhSlNy2ioFLy7JcPZb+v3ftDGywUqcBiVDoea0\n -----END CERTIFICATE-----\n\n force_remove\n If True will cause the state to remove any entries found in the keystore which are not\n defined in the state. The default is False.\n\n Example\n\n .. code-block:: yaml\n\n define_keystore:\n keystore.managed:\n - name: /path/to/keystore\n - passphrase: changeit\n - force_remove: True\n - entries:\n - alias: hostname1\n certificate: /path/to/cert.crt\n - alias: remotehost\n certificate: /path/to/cert2.crt\n private_key: /path/to/key2.key\n - alias: pillarhost\n certificate: {{ salt.pillar.get('path:to:cert') }}\n '''\n ret = {'changes': {},\n 'comment': '',\n 'name': name,\n 'result': True}\n\n keep_list = []\n old_aliases = []\n\n if force_remove:\n if os.path.exists(name):\n existing_entries = __salt__['keystore.list'](name, passphrase)\n for entry in existing_entries:\n old_aliases.append(entry.get('alias'))\n log.debug(\"Existing aliases list: %s\", old_aliases)\n\n for entry in entries:\n update_entry = True\n existing_entry = None\n if os.path.exists(name):\n if force_remove:\n keep_list.append(entry['alias'])\n\n existing_entry = __salt__['keystore.list'](name, passphrase, entry['alias'])\n if existing_entry:\n existing_sha1 = existing_entry[0]['sha1']\n new_sha1 = __salt__['x509.read_certificate'](entry['certificate'])['SHA1 Finger Print']\n if existing_sha1 == new_sha1:\n update_entry = False\n\n if update_entry:\n if __opts__['test']:\n ret['result'] = None\n if existing_entry:\n ret['comment'] += \"Alias {0} would have been updated\\n\".format(entry['alias'])\n else:\n ret['comment'] += \"Alias {0} would have been added\\n\".format(entry['alias'])\n else:\n if existing_entry:\n result = __salt__['keystore.remove'](entry['alias'], name, passphrase)\n result = __salt__['keystore.add'](entry['alias'],\n name,\n passphrase,\n entry['certificate'],\n private_key=entry.get('private_key', None)\n )\n if result:\n ret['changes'][entry['alias']] = \"Updated\"\n ret['comment'] += \"Alias {0} updated.\\n\".format(entry['alias'])\n else:\n result = __salt__['keystore.add'](entry['alias'],\n name,\n passphrase,\n entry['certificate'],\n private_key=entry.get('private_key', None)\n )\n if result:\n ret['changes'][entry['alias']] = \"Added\"\n ret['comment'] += \"Alias {0} added.\\n\".format(entry['alias'])\n\n if force_remove:\n # Determine which aliases need to be removed\n remove_list = list(set(old_aliases) - set(keep_list))\n log.debug(\"Will remove: %s\", remove_list)\n for alias_name in remove_list:\n if __opts__['test']:\n ret['comment'] += \"Alias {0} would have been removed\".format(alias_name)\n ret['result'] = None\n else:\n __salt__['keystore.remove'](alias_name, name, passphrase)\n ret['changes'][alias_name] = \"Removed\"\n ret['comment'] += \"Alias {0} removed.\\n\".format(alias_name)\n\n if not ret['changes'] and not ret['comment']:\n ret['comment'] = \"No changes made.\\n\"\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef disable():\n '''\n Disable the Packet Filter.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pf.disable\n '''\n ret = {}\n result = __salt__['cmd.run_all']('pfctl -d',\n output_loglevel='trace',\n python_shell=False)\n\n if result['retcode'] == 0:\n ret = {'comment': 'pf disabled', 'changes': True}\n else:\n # If pf was already disabled the return code is also non-zero.\n # Don't raise an exception in that case.\n if result['stderr'] == 'pfctl: pf not enabled':\n ret = {'comment': 'pf already disabled', 'changes': False}\n else:\n raise CommandExecutionError(\n 'Could not disable pf',\n info={'errors': [result['stderr']], 'changes': False}\n )\n\n return ret", "response": "Disable the Packet Filter."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef loglevel(level):\n '''\n Set the debug level which limits the severity of log messages printed by ``pf(4)``.\n\n level:\n Log level. Should be one of the following: emerg, alert, crit, err, warning, notice,\n info or debug (OpenBSD); or none, urgent, misc, loud (FreeBSD).\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pf.loglevel emerg\n '''\n # There's no way to getting the previous loglevel so imply we've\n # always made a change.\n ret = {'changes': True}\n\n myos = __grains__['os']\n if myos == 'FreeBSD':\n all_levels = ['none', 'urgent', 'misc', 'loud']\n else:\n all_levels = ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug']\n if level not in all_levels:\n raise SaltInvocationError('Unknown loglevel: {0}'.format(level))\n\n result = __salt__['cmd.run_all']('pfctl -x {0}'.format(level),\n output_loglevel='trace',\n python_shell=False)\n\n if result['retcode'] != 0:\n raise CommandExecutionError(\n 'Problem encountered setting loglevel',\n info={'errors': [result['stderr']], 'changes': False}\n )\n\n return ret", "response": "Set the debug level which limits the severity of log messages printed by pf ( 4 )."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef load(file='/etc/pf.conf', noop=False):\n '''\n Load a ruleset from the specific file, overwriting the currently loaded ruleset.\n\n file:\n Full path to the file containing the ruleset.\n\n noop:\n Don't actually load the rules, just parse them.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pf.load /etc/pf.conf.d/lockdown.conf\n '''\n # We cannot precisely determine if loading the ruleset implied\n # any changes so assume it always does.\n ret = {'changes': True}\n cmd = ['pfctl', '-f', file]\n\n if noop:\n ret['changes'] = False\n cmd.append('-n')\n\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n if result['retcode'] != 0:\n raise CommandExecutionError(\n 'Problem loading the ruleset from {0}'.format(file),\n info={'errors': [result['stderr']], 'changes': False}\n )\n\n return ret", "response": "Load a specific ruleset from the specific file overwriting the currently loaded ruleset."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef flush(modifier):\n '''\n Flush the specified packet filter parameters.\n\n modifier:\n Should be one of the following:\n\n - all\n - info\n - osfp\n - rules\n - sources\n - states\n - tables\n\n Please refer to the OpenBSD `pfctl(8) `_\n documentation for a detailed explanation of each command.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pf.flush states\n '''\n ret = {}\n\n all_modifiers = ['rules', 'states', 'info', 'osfp', 'all', 'sources', 'tables']\n\n # Accept the following two modifiers to allow for a consistent interface between\n # pfctl(8) and Salt.\n capital_modifiers = ['Sources', 'Tables']\n all_modifiers += capital_modifiers\n if modifier.title() in capital_modifiers:\n modifier = modifier.title()\n\n if modifier not in all_modifiers:\n raise SaltInvocationError('Unknown modifier: {0}'.format(modifier))\n\n cmd = 'pfctl -v -F {0}'.format(modifier)\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n if result['retcode'] == 0:\n if re.match(r'^0.*', result['stderr']):\n ret['changes'] = False\n else:\n ret['changes'] = True\n\n ret['comment'] = result['stderr']\n else:\n raise CommandExecutionError(\n 'Could not flush {0}'.format(modifier),\n info={'errors': [result['stderr']], 'changes': False}\n )\n\n return ret", "response": "Flush the specified packet filter parameters."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of commands that can be applied to a table.", "response": "def table(command, table, **kwargs):\n '''\n Apply a command on the specified table.\n\n table:\n Name of the table.\n\n command:\n Command to apply to the table. Supported commands are:\n\n - add\n - delete\n - expire\n - flush\n - kill\n - replace\n - show\n - test\n - zero\n\n Please refer to the OpenBSD `pfctl(8) `_\n documentation for a detailed explanation of each command.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pf.table expire table=spam_hosts number=300\n salt '*' pf.table add table=local_hosts addresses='[\"127.0.0.1\", \"::1\"]'\n '''\n ret = {}\n\n all_commands = ['kill', 'flush', 'add', 'delete', 'expire', 'replace', 'show', 'test', 'zero']\n if command not in all_commands:\n raise SaltInvocationError('Unknown table command: {0}'.format(command))\n\n cmd = ['pfctl', '-t', table, '-T', command]\n\n if command in ['add', 'delete', 'replace', 'test']:\n cmd += kwargs.get('addresses', [])\n elif command == 'expire':\n number = kwargs.get('number', None)\n if not number:\n raise SaltInvocationError('need expire_number argument for expire command')\n else:\n cmd.append(number)\n\n result = __salt__['cmd.run_all'](cmd,\n output_level='trace',\n python_shell=False)\n\n if result['retcode'] == 0:\n if command == 'show':\n ret = {'comment': result['stdout'].split()}\n elif command == 'test':\n ret = {'comment': result['stderr'], 'matches': True}\n else:\n if re.match(r'^(0.*|no changes)', result['stderr']):\n ret['changes'] = False\n else:\n ret['changes'] = True\n\n ret['comment'] = result['stderr']\n else:\n # 'test' returns a non-zero code if the address didn't match, even if\n # the command itself ran fine; also set 'matches' to False since not\n # everything matched.\n if command == 'test' and re.match(r'^\\d+/\\d+ addresses match.$', result['stderr']):\n ret = {'comment': result['stderr'], 'matches': False}\n else:\n raise CommandExecutionError(\n 'Could not apply {0} on table {1}'.format(command, table),\n info={'errors': [result['stderr']], 'changes': False}\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nshow filter parameters. modifier: Modifier to apply for filtering. Only a useful subset of what pfctl supports can be used with Salt. - rules - states - tables CLI example: .. code-block:: bash salt '*' pf.show rules", "response": "def show(modifier):\n '''\n Show filter parameters.\n\n modifier:\n Modifier to apply for filtering. Only a useful subset of what pfctl supports\n can be used with Salt.\n\n - rules\n - states\n - tables\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pf.show rules\n '''\n # By definition showing the parameters makes no changes.\n ret = {'changes': False}\n\n capital_modifiers = ['Tables']\n all_modifiers = ['rules', 'states', 'tables']\n all_modifiers += capital_modifiers\n if modifier.title() in capital_modifiers:\n modifier = modifier.title()\n\n if modifier not in all_modifiers:\n raise SaltInvocationError('Unknown modifier: {0}'.format(modifier))\n\n cmd = 'pfctl -s {0}'.format(modifier)\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n if result['retcode'] == 0:\n ret['comment'] = result['stdout'].split('\\n')\n else:\n raise CommandExecutionError(\n 'Could not show {0}'.format(modifier),\n info={'errors': [result['stderr']], 'changes': False}\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_event(name, message_type, routing_key='everyone', **kwargs):\n '''\n Create an event on the VictorOps service\n\n .. code-block:: yaml\n\n webserver-warning-message:\n victorops.create_event:\n - message_type: 'CRITICAL'\n - entity_id: 'webserver/diskspace'\n - state_message: 'Webserver diskspace is low.'\n\n database-server-warning-message:\n victorops.create_event:\n - message_type: 'WARNING'\n - entity_id: 'db_server/load'\n - state_message: 'Database Server load is high.'\n - entity_is_host: True\n - entity_display_name: 'dbdserver.example.com'\n\n The following parameters are required:\n\n name\n This is a short description of the event.\n\n message_type\n One of the following values: INFO, WARNING, ACKNOWLEDGEMENT, CRITICAL, RECOVERY.\n\n The following parameters are optional:\n\n routing_key\n The key for where messages should be routed. By default, sent to 'everyone' route.\n\n entity_id\n The name of alerting entity. If not provided, a random name will be assigned.\n\n timestamp\n Timestamp of the alert in seconds since epoch. Defaults to the time the alert is received at VictorOps.\n\n timestamp_fmt\n The date format for the timestamp parameter. Defaults to ''%Y-%m-%dT%H:%M:%S'.\n\n state_start_time\n The time this entity entered its current state (seconds since epoch). Defaults to the time alert is received.\n\n state_start_time_fmt\n The date format for the timestamp parameter. Defaults to '%Y-%m-%dT%H:%M:%S'.\n\n state_message\n Any additional status information from the alert item.\n\n entity_is_host\n Used within VictorOps to select the appropriate display format for the incident.\n\n entity_display_name\n Used within VictorOps to display a human-readable name for the entity.\n\n ack_message\n A user entered comment for the acknowledgment.\n\n ack_author\n The user that acknowledged the incident.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Need to create event: {0}'.format(name)\n return ret\n\n res = __salt__['victorops.create_event'](\n message_type=message_type,\n routing_key=routing_key,\n **kwargs\n )\n if res['result'] == 'success':\n ret['result'] = True\n ret['comment'] = 'Created event: {0} for entity {1}'.format(name, res['entity_id'])\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to create event: {0}'.format(res['message'])\n return ret", "response": "Creates an event on the VictorOps service."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _auth(profile=None):\n '''\n Set up neutron credentials\n '''\n if profile:\n credentials = __salt__['config.option'](profile)\n user = credentials['keystone.user']\n password = credentials['keystone.password']\n tenant = credentials['keystone.tenant']\n auth_url = credentials['keystone.auth_url']\n region_name = credentials.get('keystone.region_name', None)\n service_type = credentials.get('keystone.service_type', 'network')\n os_auth_system = credentials.get('keystone.os_auth_system', None)\n use_keystoneauth = credentials.get('keystone.use_keystoneauth', False)\n verify = credentials.get('keystone.verify', True)\n else:\n user = __salt__['config.option']('keystone.user')\n password = __salt__['config.option']('keystone.password')\n tenant = __salt__['config.option']('keystone.tenant')\n auth_url = __salt__['config.option']('keystone.auth_url')\n region_name = __salt__['config.option']('keystone.region_name')\n service_type = __salt__['config.option']('keystone.service_type')\n os_auth_system = __salt__['config.option']('keystone.os_auth_system')\n use_keystoneauth = __salt__['config.option']('keystone.use_keystoneauth')\n verify = __salt__['config.option']('keystone.verify')\n\n if use_keystoneauth is True:\n project_domain_name = credentials['keystone.project_domain_name']\n user_domain_name = credentials['keystone.user_domain_name']\n\n kwargs = {\n 'username': user,\n 'password': password,\n 'tenant_name': tenant,\n 'auth_url': auth_url,\n 'region_name': region_name,\n 'service_type': service_type,\n 'os_auth_plugin': os_auth_system,\n 'use_keystoneauth': use_keystoneauth,\n 'verify': verify,\n 'project_domain_name': project_domain_name,\n 'user_domain_name': user_domain_name\n }\n else:\n kwargs = {\n 'username': user,\n 'password': password,\n 'tenant_name': tenant,\n 'auth_url': auth_url,\n 'region_name': region_name,\n 'service_type': service_type,\n 'os_auth_plugin': os_auth_system\n }\n\n return suoneu.SaltNeutron(**kwargs)", "response": "Set up neutron credentials\notope"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates a tenant s quota", "response": "def update_quota(tenant_id,\n subnet=None,\n router=None,\n network=None,\n floatingip=None,\n port=None,\n security_group=None,\n security_group_rule=None,\n profile=None):\n '''\n Update a tenant's quota\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_quota tenant-id subnet=40 router=50\n network=10 floatingip=30 port=30\n\n :param tenant_id: ID of tenant\n :param subnet: Value of subnet quota (Optional)\n :param router: Value of router quota (Optional)\n :param network: Value of network quota (Optional)\n :param floatingip: Value of floatingip quota (Optional)\n :param port: Value of port quota (Optional)\n :param security_group: Value of security group (Optional)\n :param security_group_rule: Value of security group rule (Optional)\n :param profile: Profile to build on (Optional)\n :return: Value of updated quota\n '''\n conn = _auth(profile)\n return conn.update_quota(tenant_id, subnet, router, network,\n floatingip, port, security_group,\n security_group_rule)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a new port in a network", "response": "def create_port(name,\n network,\n device_id=None,\n admin_state_up=True,\n profile=None):\n '''\n Creates a new port\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_port network-name port-name\n\n :param name: Name of port to create\n :param network: Network name or ID\n :param device_id: ID of device (Optional)\n :param admin_state_up: Set admin state up to true or false,\n default: true (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created port information\n '''\n conn = _auth(profile)\n return conn.create_port(name, network, device_id, admin_state_up)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating a port in a node", "response": "def update_port(port, name, admin_state_up=True, profile=None):\n '''\n Updates a port\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_port port-name network-name new-port-name\n\n :param port: Port name or ID\n :param name: Name of this port\n :param admin_state_up: Set admin state up to true or false,\n default: true (Optional)\n :param profile: Profile to build on (Optional)\n :return: Value of updated port information\n '''\n conn = _auth(profile)\n return conn.update_port(port, name, admin_state_up)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a network in the network cache", "response": "def create_network(name, router_ext=None, admin_state_up=True, network_type=None, physical_network=None, segmentation_id=None, shared=None, profile=None):\n '''\n Creates a new network\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_network network-name\n salt '*' neutron.create_network network-name profile=openstack1\n\n :param name: Name of network to create\n :param admin_state_up: should the state of the network be up?\n default: True (Optional)\n :param router_ext: True then if create the external network (Optional)\n :param network_type: the Type of network that the provider is such as GRE, VXLAN, VLAN, FLAT, or LOCAL (Optional)\n :param physical_network: the name of the physical network as neutron knows it (Optional)\n :param segmentation_id: the vlan id or GRE id (Optional)\n :param shared: is the network shared or not (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created network information\n '''\n conn = _auth(profile)\n return conn.create_network(name, admin_state_up, router_ext, network_type, physical_network, segmentation_id, shared)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef update_network(network, name, profile=None):\n '''\n Updates a network\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_network network-name new-network-name\n\n :param network: ID or name of network to update\n :param name: Name of this network\n :param profile: Profile to build on (Optional)\n :return: Value of updated network information\n '''\n conn = _auth(profile)\n return conn.update_network(network, name)", "response": "Updates a network in a specific profile"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_subnet(network, cidr, name=None,\n ip_version=4, profile=None):\n '''\n Creates a new subnet\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_subnet network-name 192.168.1.0/24\n\n :param network: Network ID or name this subnet belongs to\n :param cidr: CIDR of subnet to create (Ex. '192.168.1.0/24')\n :param name: Name of the subnet to create (Optional)\n :param ip_version: Version to use, default is 4(IPv4) (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created subnet information\n '''\n conn = _auth(profile)\n return conn.create_subnet(network, cidr, name, ip_version)", "response": "Creates a new subnet in the network"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating a subnet in a node", "response": "def update_subnet(subnet, name, profile=None):\n '''\n Updates a subnet\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_subnet subnet-name new-subnet-name\n\n :param subnet: ID or name of subnet to update\n :param name: Name of this subnet\n :param profile: Profile to build on (Optional)\n :return: Value of updated subnet information\n '''\n conn = _auth(profile)\n return conn.update_subnet(subnet, name)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_router(name, ext_network=None,\n admin_state_up=True, profile=None):\n '''\n Creates a new router\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_router new-router-name\n\n :param name: Name of router to create (must be first)\n :param ext_network: ID or name of the external for the gateway (Optional)\n :param admin_state_up: Set admin state up to true or false,\n default:true (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created router information\n '''\n conn = _auth(profile)\n return conn.create_router(name, ext_network, admin_state_up)", "response": "Create a new router"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update_router(router,\n name=None,\n admin_state_up=None,\n profile=None,\n **kwargs):\n '''\n Updates a router\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_router router_id name=new-router-name\n admin_state_up=True\n\n :param router: ID or name of router to update\n :param name: Name of this router\n :param ext_network: ID or name of the external for the gateway (Optional)\n :param admin_state_up: Set admin state up to true or false,\n default: true (Optional)\n :param profile: Profile to build on (Optional)\n :param kwargs:\n :return: Value of updated router information\n '''\n conn = _auth(profile)\n return conn.update_router(router, name, admin_state_up, **kwargs)", "response": "Updates a router\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_router router_id name=new-router-name\n admin_state_up=True\n\n :param router: ID or name of router to update\n :param name: Name of this router\n :param ext_network: ID or name of the external for the gateway (Optional)\n :param admin_state_up: Set admin state up to true or false,\n default: true (Optional)\n :param profile: Profile to build on (Optional)\n :param kwargs:\n :return: Value of updated router information"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_interface_router(router, subnet, profile=None):\n '''\n Adds an internal network interface to the specified router\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.add_interface_router router-name subnet-name\n\n :param router: ID or name of the router\n :param subnet: ID or name of the subnet\n :param profile: Profile to build on (Optional)\n :return: Added interface information\n '''\n conn = _auth(profile)\n return conn.add_interface_router(router, subnet)", "response": "Adds an internal network interface to the specified router"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving an internal network interface from the specified router", "response": "def remove_interface_router(router, subnet, profile=None):\n '''\n Removes an internal network interface from the specified router\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.remove_interface_router router-name subnet-name\n\n :param router: ID or name of the router\n :param subnet: ID or name of the subnet\n :param profile: Profile to build on (Optional)\n :return: True(Succeed) or False\n '''\n conn = _auth(profile)\n return conn.remove_interface_router(router, subnet)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_gateway_router(router, ext_network, profile=None):\n '''\n Adds an external network gateway to the specified router\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.add_gateway_router router-name ext-network-name\n\n :param router: ID or name of the router\n :param ext_network: ID or name of the external network the gateway\n :param profile: Profile to build on (Optional)\n :return: Added Gateway router information\n '''\n conn = _auth(profile)\n return conn.add_gateway_router(router, ext_network)", "response": "Adds an external network gateway to the specified router"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates a new floatingIP", "response": "def create_floatingip(floating_network, port=None, profile=None):\n '''\n Creates a new floatingIP\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_floatingip network-name port-name\n\n :param floating_network: Network name or ID to allocate floatingIP from\n :param port: Of the port to be associated with the floatingIP (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created floatingIP information\n '''\n conn = _auth(profile)\n return conn.create_floatingip(floating_network, port)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating a floating IP", "response": "def update_floatingip(floatingip_id, port=None, profile=None):\n '''\n Updates a floatingIP\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_floatingip network-name port-name\n\n :param floatingip_id: ID of floatingIP\n :param port: ID or name of port, to associate floatingip to `None` or do\n not specify to disassociate the floatingip (Optional)\n :param profile: Profile to build on (Optional)\n :return: Value of updated floating IP information\n '''\n conn = _auth(profile)\n return conn.update_floatingip(floatingip_id, port)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_security_group(name=None, description=None, profile=None):\n '''\n Creates a new security group\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_security_group security-group-name \\\n description='Security group for servers'\n\n :param name: Name of security group (Optional)\n :param description: Description of security group (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created security group information\n '''\n conn = _auth(profile)\n return conn.create_security_group(name, description)", "response": "Creates a new security group"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef update_security_group(security_group, name=None, description=None,\n profile=None):\n '''\n Updates a security group\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_security_group security-group-name \\\n new-security-group-name\n\n :param security_group: ID or name of security group to update\n :param name: Name of this security group (Optional)\n :param description: Description of security group (Optional)\n :param profile: Profile to build on (Optional)\n :return: Value of updated security group information\n '''\n conn = _auth(profile)\n return conn.update_security_group(security_group, name, description)", "response": "Updates a security group"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_security_group_rule(security_group,\n remote_group_id=None,\n direction='ingress',\n protocol=None,\n port_range_min=None,\n port_range_max=None,\n ethertype='IPv4',\n profile=None):\n '''\n Creates a new security group rule\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.show_security_group_rule security-group-rule-id\n\n :param security_group: Security group name or ID to add rule\n :param remote_group_id: Remote security group name or ID to\n apply rule (Optional)\n :param direction: Direction of traffic: ingress/egress,\n default: ingress (Optional)\n :param protocol: Protocol of packet: null/icmp/tcp/udp,\n default: null (Optional)\n :param port_range_min: Starting port range (Optional)\n :param port_range_max: Ending port range (Optional)\n :param ethertype: IPv4/IPv6, default: IPv4 (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created security group rule information\n '''\n conn = _auth(profile)\n return conn.create_security_group_rule(security_group,\n remote_group_id,\n direction,\n protocol,\n port_range_min,\n port_range_max,\n ethertype)", "response": "Creates a new security group rule"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_vpnservices(retrieve_all=True, profile=None, **kwargs):\n '''\n Fetches a list of all configured VPN services for a tenant\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.list_vpnservices\n\n :param retrieve_all: True or False, default: True (Optional)\n :param profile: Profile to build on (Optional)\n :return: List of VPN service\n '''\n conn = _auth(profile)\n return conn.list_vpnservices(retrieve_all, **kwargs)", "response": "Fetches a list of all configured VPN services for a tenant"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfetching information of a VPN service", "response": "def show_vpnservice(vpnservice, profile=None, **kwargs):\n '''\n Fetches information of a specific VPN service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.show_vpnservice vpnservice-name\n\n :param vpnservice: ID or name of vpn service to look up\n :param profile: Profile to build on (Optional)\n :return: VPN service information\n '''\n conn = _auth(profile)\n return conn.show_vpnservice(vpnservice, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a VPN service", "response": "def create_vpnservice(subnet, router, name, admin_state_up=True, profile=None):\n '''\n Creates a new VPN service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_vpnservice router-name name\n\n :param subnet: Subnet unique identifier for the VPN service deployment\n :param router: Router unique identifier for the VPN service\n :param name: Set a name for the VPN service\n :param admin_state_up: Set admin state up to true or false,\n default:True (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created VPN service information\n '''\n conn = _auth(profile)\n return conn.create_vpnservice(subnet, router, name, admin_state_up)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_vpnservice(vpnservice, desc, profile=None):\n '''\n Updates a VPN service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_vpnservice vpnservice-name desc='VPN Service1'\n\n :param vpnservice: ID or name of vpn service to update\n :param desc: Set a description for the VPN service\n :param profile: Profile to build on (Optional)\n :return: Value of updated VPN service information\n '''\n conn = _auth(profile)\n return conn.update_vpnservice(vpnservice, desc)", "response": "Update VPN service information"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a new IPsecSiteConnection object.", "response": "def create_ipsec_site_connection(name,\n ipsecpolicy,\n ikepolicy,\n vpnservice,\n peer_cidrs,\n peer_address,\n peer_id,\n psk,\n admin_state_up=True,\n profile=None,\n **kwargs):\n '''\n Creates a new IPsecSiteConnection\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.show_ipsec_site_connection connection-name\n ipsec-policy-name ikepolicy-name vpnservice-name\n 192.168.XXX.XXX/24 192.168.XXX.XXX 192.168.XXX.XXX secret\n\n :param name: Set friendly name for the connection\n :param ipsecpolicy: IPSec policy ID or name associated with this connection\n :param ikepolicy: IKE policy ID or name associated with this connection\n :param vpnservice: VPN service instance ID or name associated with\n this connection\n :param peer_cidrs: Remote subnet(s) in CIDR format\n :param peer_address: Peer gateway public IPv4/IPv6 address or FQDN\n :param peer_id: Peer router identity for authentication\n Can be IPv4/IPv6 address, e-mail address, key id, or FQDN\n :param psk: Pre-shared key string\n :param initiator: Initiator state in lowercase, default:bi-directional\n :param admin_state_up: Set admin state up to true or false,\n default: True (Optional)\n :param mtu: size for the connection, default:1500 (Optional)\n :param dpd_action: Dead Peer Detection attribute: hold/clear/disabled/\n restart/restart-by-peer (Optional)\n :param dpd_interval: Dead Peer Detection attribute (Optional)\n :param dpd_timeout: Dead Peer Detection attribute (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created IPSec site connection information\n '''\n conn = _auth(profile)\n return conn.create_ipsec_site_connection(name,\n ipsecpolicy,\n ikepolicy,\n vpnservice,\n peer_cidrs,\n peer_address,\n peer_id,\n psk,\n admin_state_up,\n **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a new IKE policy", "response": "def create_ikepolicy(name, profile=None, **kwargs):\n '''\n Creates a new IKEPolicy\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_ikepolicy ikepolicy-name\n phase1_negotiation_mode=main auth_algorithm=sha1\n encryption_algorithm=aes-128 pfs=group5\n\n :param name: Name of the IKE policy\n :param phase1_negotiation_mode: IKE Phase1 negotiation mode in lowercase,\n default: main (Optional)\n :param auth_algorithm: Authentication algorithm in lowercase,\n default: sha1 (Optional)\n :param encryption_algorithm: Encryption algorithm in lowercase.\n default:aes-128 (Optional)\n :param pfs: Prefect Forward Security in lowercase,\n default: group5 (Optional)\n :param units: IKE lifetime attribute. default: seconds (Optional)\n :param value: IKE lifetime attribute. default: 3600 (Optional)\n :param ike_version: IKE version in lowercase, default: v1 (Optional)\n :param profile: Profile to build on (Optional)\n :param kwargs:\n :return: Created IKE policy information\n '''\n conn = _auth(profile)\n return conn.create_ikepolicy(name, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new IPSec policy", "response": "def create_ipsecpolicy(name, profile=None, **kwargs):\n '''\n Creates a new IPsecPolicy\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_ipsecpolicy ipsecpolicy-name\n transform_protocol=esp auth_algorithm=sha1\n encapsulation_mode=tunnel encryption_algorithm=aes-128\n\n :param name: Name of the IPSec policy\n :param transform_protocol: Transform protocol in lowercase,\n default: esp (Optional)\n :param auth_algorithm: Authentication algorithm in lowercase,\n default: sha1 (Optional)\n :param encapsulation_mode: Encapsulation mode in lowercase,\n default: tunnel (Optional)\n :param encryption_algorithm: Encryption algorithm in lowercase,\n default:aes-128 (Optional)\n :param pfs: Prefect Forward Security in lowercase,\n default: group5 (Optional)\n :param units: IPSec lifetime attribute. default: seconds (Optional)\n :param value: IPSec lifetime attribute. default: 3600 (Optional)\n :param profile: Profile to build on (Optional)\n :return: Created IPSec policy information\n '''\n conn = _auth(profile)\n return conn.create_ipsecpolicy(name, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_firewall_rule(protocol, action, profile=None, **kwargs):\n '''\n Creates a new firewall rule\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.create_firewall_rule protocol action\n tenant_id=TENANT_ID name=NAME description=DESCRIPTION ip_version=IP_VERSION\n source_ip_address=SOURCE_IP_ADDRESS destination_ip_address=DESTINATION_IP_ADDRESS source_port=SOURCE_PORT\n destination_port=DESTINATION_PORT shared=SHARED enabled=ENABLED\n\n :param protocol: Protocol for the firewall rule, choose \"tcp\",\"udp\",\"icmp\" or \"None\".\n :param action: Action for the firewall rule, choose \"allow\" or \"deny\".\n :param tenant_id: The owner tenant ID. (Optional)\n :param name: Name for the firewall rule. (Optional)\n :param description: Description for the firewall rule. (Optional)\n :param ip_version: IP protocol version, default: 4. (Optional)\n :param source_ip_address: Source IP address or subnet. (Optional)\n :param destination_ip_address: Destination IP address or subnet. (Optional)\n :param source_port: Source port (integer in [1, 65535] or range in a:b). (Optional)\n :param destination_port: Destination port (integer in [1, 65535] or range in a:b). (Optional)\n :param shared: Set shared to True, default: False. (Optional)\n :param enabled: To enable this rule, default: True. (Optional)\n '''\n conn = _auth(profile)\n return conn.create_firewall_rule(protocol, action, **kwargs)", "response": "Creates a firewall rule in the NIC."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef update_firewall_rule(firewall_rule,\n protocol=None,\n action=None,\n name=None,\n description=None,\n ip_version=None,\n source_ip_address=None,\n destination_ip_address=None,\n source_port=None,\n destination_port=None,\n shared=None,\n enabled=None,\n profile=None):\n '''\n Update a firewall rule\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutron.update_firewall_rule firewall_rule protocol=PROTOCOL action=ACTION\n name=NAME description=DESCRIPTION ip_version=IP_VERSION\n source_ip_address=SOURCE_IP_ADDRESS destination_ip_address=DESTINATION_IP_ADDRESS\n source_port=SOURCE_PORT destination_port=DESTINATION_PORT shared=SHARED enabled=ENABLED\n\n :param firewall_rule: ID or name of firewall rule to update.\n :param protocol: Protocol for the firewall rule, choose \"tcp\",\"udp\",\"icmp\" or \"None\". (Optional)\n :param action: Action for the firewall rule, choose \"allow\" or \"deny\". (Optional)\n :param name: Name for the firewall rule. (Optional)\n :param description: Description for the firewall rule. (Optional)\n :param ip_version: IP protocol version, default: 4. (Optional)\n :param source_ip_address: Source IP address or subnet. (Optional)\n :param destination_ip_address: Destination IP address or subnet. (Optional)\n :param source_port: Source port (integer in [1, 65535] or range in a:b). (Optional)\n :param destination_port: Destination port (integer in [1, 65535] or range in a:b). (Optional)\n :param shared: Set shared to True, default: False. (Optional)\n :param enabled: To enable this rule, default: True. (Optional)\n :param profile: Profile to build on (Optional)\n '''\n conn = _auth(profile)\n return conn.update_firewall_rule(firewall_rule, protocol, action, name, description, ip_version,\n source_ip_address, destination_ip_address, source_port, destination_port,\n shared, enabled)", "response": "Update a firewall rule."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef active(display_progress=False):\n '''\n Return a report on all actively running jobs from a job id centric\n perspective\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.active\n '''\n ret = {}\n client = salt.client.get_local_client(__opts__['conf_file'])\n try:\n active_ = client.cmd('*', 'saltutil.running', timeout=__opts__['timeout'])\n except SaltClientError as client_error:\n print(client_error)\n return ret\n\n if display_progress:\n __jid_event__.fire_event({\n 'message': 'Attempting to contact minions: {0}'.format(list(active_.keys()))\n }, 'progress')\n for minion, data in six.iteritems(active_):\n if display_progress:\n __jid_event__.fire_event({'message': 'Received reply from minion {0}'.format(minion)}, 'progress')\n if not isinstance(data, list):\n continue\n for job in data:\n if not job['jid'] in ret:\n ret[job['jid']] = _format_jid_instance(job['jid'], job)\n ret[job['jid']].update({'Running': [{minion: job.get('pid', None)}], 'Returned': []})\n else:\n ret[job['jid']]['Running'].append({minion: job['pid']})\n\n mminion = salt.minion.MasterMinion(__opts__)\n for jid in ret:\n returner = _get_returner((__opts__['ext_job_cache'], __opts__['master_job_cache']))\n data = mminion.returners['{0}.get_jid'.format(returner)](jid)\n if data:\n for minion in data:\n if minion not in ret[jid]['Returned']:\n ret[jid]['Returned'].append(minion)\n\n return ret", "response": "Return a list of all actively running jobs from a job id centric\n perspective\n CLI Example : bash\nFormula salt - run jobs. active\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef lookup_jid(jid,\n ext_source=None,\n returned=True,\n missing=False,\n display_progress=False):\n '''\n Return the printout from a previously executed job\n\n jid\n The jid to look up.\n\n ext_source\n The external job cache to use. Default: `None`.\n\n returned : True\n If ``True``, include the minions that did return from the command.\n\n .. versionadded:: 2015.8.0\n\n missing : False\n If ``True``, include the minions that did *not* return from the\n command.\n\n display_progress : False\n If ``True``, fire progress events.\n\n .. versionadded:: 2015.5.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.lookup_jid 20130916125524463507\n salt-run jobs.lookup_jid 20130916125524463507 --out=highstate\n '''\n ret = {}\n mminion = salt.minion.MasterMinion(__opts__)\n returner = _get_returner((\n __opts__['ext_job_cache'],\n ext_source,\n __opts__['master_job_cache']\n ))\n\n try:\n data = list_job(\n jid,\n ext_source=ext_source,\n display_progress=display_progress\n )\n except TypeError:\n return ('Requested returner could not be loaded. '\n 'No JIDs could be retrieved.')\n\n targeted_minions = data.get('Minions', [])\n returns = data.get('Result', {})\n\n if returns:\n for minion in returns:\n if display_progress:\n __jid_event__.fire_event({'message': minion}, 'progress')\n if u'return' in returns[minion]:\n if returned:\n ret[minion] = returns[minion].get(u'return')\n else:\n if returned:\n ret[minion] = returns[minion].get('return')\n if missing:\n for minion_id in (x for x in targeted_minions if x not in returns):\n ret[minion_id] = 'Minion did not return'\n\n # We need to check to see if the 'out' key is present and use it to specify\n # the correct outputter, so we get highstate output for highstate runs.\n try:\n # Check if the return data has an 'out' key. We'll use that as the\n # outputter in the absence of one being passed on the CLI.\n outputter = None\n _ret = returns[next(iter(returns))]\n if 'out' in _ret:\n outputter = _ret['out']\n elif 'outputter' in _ret.get('return', {}).get('return', {}):\n outputter = _ret['return']['return']['outputter']\n except (StopIteration, AttributeError):\n pass\n\n if outputter:\n return {'outputter': outputter, 'data': ret}\n else:\n return ret", "response": "Return the printout from a previously executed job."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_job(jid, ext_source=None, display_progress=False):\n '''\n List a specific job given by its jid\n\n ext_source\n If provided, specifies which external job cache to use.\n\n display_progress : False\n If ``True``, fire progress events.\n\n .. versionadded:: 2015.8.8\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.list_job 20130916125524463507\n salt-run jobs.list_job 20130916125524463507 --out=pprint\n '''\n ret = {'jid': jid}\n mminion = salt.minion.MasterMinion(__opts__)\n returner = _get_returner((\n __opts__['ext_job_cache'],\n ext_source,\n __opts__['master_job_cache']\n ))\n if display_progress:\n __jid_event__.fire_event(\n {'message': 'Querying returner: {0}'.format(returner)},\n 'progress'\n )\n\n job = mminion.returners['{0}.get_load'.format(returner)](jid)\n ret.update(_format_jid_instance(jid, job))\n ret['Result'] = mminion.returners['{0}.get_jid'.format(returner)](jid)\n\n fstr = '{0}.get_endtime'.format(__opts__['master_job_cache'])\n if (__opts__.get('job_cache_store_endtime')\n and fstr in mminion.returners):\n endtime = mminion.returners[fstr](jid)\n if endtime:\n ret['EndTime'] = endtime\n\n return ret", "response": "Return a specific job given by its jid"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_jobs(ext_source=None,\n outputter=None,\n search_metadata=None,\n search_function=None,\n search_target=None,\n start_time=None,\n end_time=None,\n display_progress=False):\n '''\n List all detectable jobs and associated functions\n\n ext_source\n If provided, specifies which external job cache to use.\n\n **FILTER OPTIONS**\n\n .. note::\n If more than one of the below options are used, only jobs which match\n *all* of the filters will be returned.\n\n search_metadata\n Specify a dictionary to match to the job's metadata. If any of the\n key-value pairs in this dictionary match, the job will be returned.\n Example:\n\n .. code-block:: bash\n\n salt-run jobs.list_jobs search_metadata='{\"foo\": \"bar\", \"baz\": \"qux\"}'\n\n search_function\n Can be passed as a string or a list. Returns jobs which match the\n specified function. Globbing is allowed. Example:\n\n .. code-block:: bash\n\n salt-run jobs.list_jobs search_function='test.*'\n salt-run jobs.list_jobs search_function='[\"test.*\", \"pkg.install\"]'\n\n .. versionchanged:: 2015.8.8\n Multiple targets can now also be passed as a comma-separated list.\n For example:\n\n .. code-block:: bash\n\n salt-run jobs.list_jobs search_function='test.*,pkg.install'\n\n search_target\n Can be passed as a string or a list. Returns jobs which match the\n specified minion name. Globbing is allowed. Example:\n\n .. code-block:: bash\n\n salt-run jobs.list_jobs search_target='*.mydomain.tld'\n salt-run jobs.list_jobs search_target='[\"db*\", \"myminion\"]'\n\n .. versionchanged:: 2015.8.8\n Multiple targets can now also be passed as a comma-separated list.\n For example:\n\n .. code-block:: bash\n\n salt-run jobs.list_jobs search_target='db*,myminion'\n\n start_time\n Accepts any timestamp supported by the dateutil_ Python module (if this\n module is not installed, this argument will be ignored). Returns jobs\n which started after this timestamp.\n\n end_time\n Accepts any timestamp supported by the dateutil_ Python module (if this\n module is not installed, this argument will be ignored). Returns jobs\n which started before this timestamp.\n\n .. _dateutil: https://pypi.python.org/pypi/python-dateutil\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.list_jobs\n salt-run jobs.list_jobs search_function='test.*' search_target='localhost' search_metadata='{\"bar\": \"foo\"}'\n salt-run jobs.list_jobs start_time='2015, Mar 16 19:00' end_time='2015, Mar 18 22:00'\n\n '''\n returner = _get_returner((\n __opts__['ext_job_cache'],\n ext_source,\n __opts__['master_job_cache']\n ))\n if display_progress:\n __jid_event__.fire_event(\n {'message': 'Querying returner {0} for jobs.'.format(returner)},\n 'progress'\n )\n mminion = salt.minion.MasterMinion(__opts__)\n\n ret = mminion.returners['{0}.get_jids'.format(returner)]()\n\n mret = {}\n for item in ret:\n _match = True\n if search_metadata:\n _match = False\n if 'Metadata' in ret[item]:\n if isinstance(search_metadata, dict):\n for key in search_metadata:\n if key in ret[item]['Metadata']:\n if ret[item]['Metadata'][key] == search_metadata[key]:\n _match = True\n else:\n log.info('The search_metadata parameter must be specified'\n ' as a dictionary. Ignoring.')\n if search_target and _match:\n _match = False\n if 'Target' in ret[item]:\n targets = ret[item]['Target']\n if isinstance(targets, six.string_types):\n targets = [targets]\n for target in targets:\n for key in salt.utils.args.split_input(search_target):\n if fnmatch.fnmatch(target, key):\n _match = True\n\n if search_function and _match:\n _match = False\n if 'Function' in ret[item]:\n for key in salt.utils.args.split_input(search_function):\n if fnmatch.fnmatch(ret[item]['Function'], key):\n _match = True\n\n if start_time and _match:\n _match = False\n if DATEUTIL_SUPPORT:\n parsed_start_time = dateutil_parser.parse(start_time)\n _start_time = dateutil_parser.parse(ret[item]['StartTime'])\n if _start_time >= parsed_start_time:\n _match = True\n else:\n log.error(\n '\\'dateutil\\' library not available, skipping start_time '\n 'comparison.'\n )\n\n if end_time and _match:\n _match = False\n if DATEUTIL_SUPPORT:\n parsed_end_time = dateutil_parser.parse(end_time)\n _start_time = dateutil_parser.parse(ret[item]['StartTime'])\n if _start_time <= parsed_end_time:\n _match = True\n else:\n log.error(\n '\\'dateutil\\' library not available, skipping end_time '\n 'comparison.'\n )\n\n if _match:\n mret[item] = ret[item]\n\n if outputter:\n return {'outputter': outputter, 'data': mret}\n else:\n return mret", "response": "Return a list of all detectable jobs and associated functions in a specific external job cache."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_jobs_filter(count,\n filter_find_job=True,\n ext_source=None,\n outputter=None,\n display_progress=False):\n '''\n List all detectable jobs and associated functions\n\n ext_source\n The external job cache to use. Default: `None`.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.list_jobs_filter 50\n salt-run jobs.list_jobs_filter 100 filter_find_job=False\n\n '''\n returner = _get_returner((\n __opts__['ext_job_cache'],\n ext_source,\n __opts__['master_job_cache']\n ))\n if display_progress:\n __jid_event__.fire_event(\n {'message': 'Querying returner {0} for jobs.'.format(returner)},\n 'progress'\n )\n mminion = salt.minion.MasterMinion(__opts__)\n\n fun = '{0}.get_jids_filter'.format(returner)\n if fun not in mminion.returners:\n raise NotImplementedError(\n '\\'{0}\\' returner function not implemented yet.'.format(fun)\n )\n ret = mminion.returners[fun](count, filter_find_job)\n\n if outputter:\n return {'outputter': outputter, 'data': ret}\n else:\n return ret", "response": "List all detectable jobs and associated functions"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef print_job(jid, ext_source=None):\n '''\n Print a specific job's detail given by it's jid, including the return data.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.print_job 20130916125524463507\n '''\n ret = {}\n\n returner = _get_returner((\n __opts__['ext_job_cache'],\n ext_source,\n __opts__['master_job_cache']\n ))\n mminion = salt.minion.MasterMinion(__opts__)\n\n try:\n job = mminion.returners['{0}.get_load'.format(returner)](jid)\n ret[jid] = _format_jid_instance(jid, job)\n except TypeError:\n ret[jid]['Result'] = (\n 'Requested returner {0} is not available. Jobs cannot be '\n 'retrieved. Check master log for details.'.format(returner)\n )\n return ret\n ret[jid]['Result'] = mminion.returners['{0}.get_jid'.format(returner)](jid)\n\n fstr = '{0}.get_endtime'.format(__opts__['master_job_cache'])\n if (__opts__.get('job_cache_store_endtime')\n and fstr in mminion.returners):\n endtime = mminion.returners[fstr](jid)\n if endtime:\n ret[jid]['EndTime'] = endtime\n\n return ret", "response": "Print a specific job s detail given by it s jid including the return data."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef exit_success(jid, ext_source=None):\n '''\n Check if a job has been executed and exit successfully\n\n jid\n The jid to look up.\n ext_source\n The external job cache to use. Default: `None`.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.exit_success 20160520145827701627\n '''\n ret = dict()\n\n data = list_job(\n jid,\n ext_source=ext_source\n )\n\n minions = data.get('Minions', [])\n result = data.get('Result', {})\n\n for minion in minions:\n if minion in result and 'return' in result[minion]:\n ret[minion] = True if result[minion]['return'] else False\n else:\n ret[minion] = False\n\n for minion in result:\n if 'return' in result[minion] and result[minion]['return']:\n ret[minion] = True\n return ret", "response": "Check if a job has been executed and exit successfully"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef last_run(ext_source=None,\n outputter=None,\n metadata=None,\n function=None,\n target=None,\n display_progress=False):\n '''\n .. versionadded:: 2015.8.0\n\n List all detectable jobs and associated functions\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run jobs.last_run\n salt-run jobs.last_run target=nodename\n salt-run jobs.last_run function='cmd.run'\n salt-run jobs.last_run metadata=\"{'foo': 'bar'}\"\n '''\n\n if metadata:\n if not isinstance(metadata, dict):\n log.info('The metadata parameter must be specified as a dictionary')\n return False\n\n _all_jobs = list_jobs(ext_source=ext_source,\n outputter=outputter,\n search_metadata=metadata,\n search_function=function,\n search_target=target,\n display_progress=display_progress)\n if _all_jobs:\n last_job = sorted(_all_jobs)[-1]\n return print_job(last_job, ext_source)\n else:\n return False", "response": "List all detectable jobs and associated functions and print the last job."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwalks through the job dir and return jobs", "response": "def _walk_through(job_dir, display_progress=False):\n '''\n Walk through the job dir and return jobs\n '''\n serial = salt.payload.Serial(__opts__)\n\n for top in os.listdir(job_dir):\n t_path = os.path.join(job_dir, top)\n\n for final in os.listdir(t_path):\n load_path = os.path.join(t_path, final, '.load.p')\n with salt.utils.files.fopen(load_path, 'rb') as rfh:\n job = serial.load(rfh)\n\n if not os.path.isfile(load_path):\n continue\n\n with salt.utils.files.fopen(load_path, 'rb') as rfh:\n job = serial.load(rfh)\n jid = job['jid']\n if display_progress:\n __jid_event__.fire_event(\n {'message': 'Found JID {0}'.format(jid)},\n 'progress'\n )\n yield jid, job, t_path, final"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ntriggering an event in IFTTT.", "response": "def trigger_event(name,\n event,\n value1=None,\n value2=None,\n value3=None\n ):\n '''\n Trigger an event in IFTTT\n\n .. code-block:: yaml\n\n ifttt-event:\n ifttt.trigger_event:\n - event: TestEvent\n - value1: 'A value that we want to send.'\n - value2: 'A second value that we want to send.'\n - value3: 'A third value that we want to send.'\n\n The following parameters are required:\n\n name\n The unique name for this event.\n\n event\n The name of the event to trigger in IFTTT.\n\n The following parameters are optional:\n\n value1\n One of the values that we can send to IFTT.\n\n value2\n One of the values that we can send to IFTT.\n\n value3\n One of the values that we can send to IFTT.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'The following trigger would be sent to IFTTT: {0}'.format(event)\n ret['result'] = None\n return ret\n\n ret['result'] = __salt__['ifttt.trigger_event'](\n event=event,\n value1=value1,\n value2=value2,\n value3=value3\n )\n\n if ret and ret['result']:\n ret['result'] = True\n ret['comment'] = 'Triggered Event: {0}'.format(name)\n else:\n ret['comment'] = 'Failed to trigger event: {0}'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure the CNAME with the given name or canonical name is removed", "response": "def absent(name=None, canonical=None, **api_opts):\n '''\n Ensure the CNAME with the given name or canonical name is removed\n '''\n ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}\n obj = __salt__['infoblox.get_cname'](name=name, canonical=canonical, **api_opts)\n\n if not obj:\n ret['result'] = True\n ret['comment'] = 'infoblox already removed'\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['changes'] = {'old': obj, 'new': 'absent'}\n return ret\n\n if __salt__['infoblox.delete_cname'](name=name, canonical=canonical, **api_opts):\n ret['result'] = True\n ret['changes'] = {'old': obj, 'new': 'absent'}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_options(ret=None):\n '''\n Get the redis options from salt.\n '''\n attrs = {'host': 'host',\n 'port': 'port',\n 'unix_socket_path': 'unix_socket_path',\n 'db': 'db',\n 'password': 'password',\n 'cluster_mode': 'cluster_mode',\n 'startup_nodes': 'cluster.startup_nodes',\n 'skip_full_coverage_check': 'cluster.skip_full_coverage_check',\n }\n\n if salt.utils.platform.is_proxy():\n return {\n 'host': __opts__.get('redis.host', 'salt'),\n 'port': __opts__.get('redis.port', 6379),\n 'unix_socket_path': __opts__.get('redis.unix_socket_path', None),\n 'db': __opts__.get('redis.db', '0'),\n 'password': __opts__.get('redis.password', ''),\n 'cluster_mode': __opts__.get('redis.cluster_mode', False),\n 'startup_nodes': __opts__.get('redis.cluster.startup_nodes', {}),\n 'skip_full_coverage_check': __opts__.get('redis.cluster.skip_full_coverage_check', False)\n }\n\n _options = salt.returners.get_returner_options(__virtualname__,\n ret,\n attrs,\n __salt__=__salt__,\n __opts__=__opts__)\n return _options", "response": "Get the redis options from salt.\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a redis server object", "response": "def _get_serv(ret=None):\n '''\n Return a redis server object\n '''\n _options = _get_options(ret)\n global REDIS_POOL\n if REDIS_POOL:\n return REDIS_POOL\n elif _options.get('cluster_mode'):\n REDIS_POOL = StrictRedisCluster(startup_nodes=_options.get('startup_nodes'),\n skip_full_coverage_check=_options.get('skip_full_coverage_check'),\n decode_responses=True)\n else:\n REDIS_POOL = redis.StrictRedis(host=_options.get('host'),\n port=_options.get('port'),\n unix_socket_path=_options.get('unix_socket_path', None),\n db=_options.get('db'),\n decode_responses=True,\n password=_options.get('password'))\n return REDIS_POOL"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef returner(ret):\n '''\n Return data to a redis data store\n '''\n serv = _get_serv(ret)\n pipeline = serv.pipeline(transaction=False)\n minion, jid = ret['id'], ret['jid']\n pipeline.hset('ret:{0}'.format(jid), minion, salt.utils.json.dumps(ret))\n pipeline.expire('ret:{0}'.format(jid), _get_ttl())\n pipeline.set('{0}:{1}'.format(minion, ret['fun']), jid)\n pipeline.sadd('minions', minion)\n pipeline.execute()", "response": "Return data to a redis data store\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsaves the load to the specified jid", "response": "def save_load(jid, load, minions=None):\n '''\n Save the load to the specified jid\n '''\n serv = _get_serv(ret=None)\n serv.setex('load:{0}'.format(jid), _get_ttl(), salt.utils.json.dumps(load))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_load(jid):\n '''\n Return the load data that marks a specified jid\n '''\n serv = _get_serv(ret=None)\n data = serv.get('load:{0}'.format(jid))\n if data:\n return salt.utils.json.loads(data)\n return {}", "response": "Return the load data that marks a specified jid\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_jid(jid):\n '''\n Return the information returned when the specified job id was executed\n '''\n serv = _get_serv(ret=None)\n ret = {}\n for minion, data in six.iteritems(serv.hgetall('ret:{0}'.format(jid))):\n if data:\n ret[minion] = salt.utils.json.loads(data)\n return ret", "response": "Return the information returned when the specified job id was executed\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_fun(fun):\n '''\n Return a dict of the last function called for all minions\n '''\n serv = _get_serv(ret=None)\n ret = {}\n for minion in serv.smembers('minions'):\n ind_str = '{0}:{1}'.format(minion, fun)\n try:\n jid = serv.get(ind_str)\n except Exception:\n continue\n if not jid:\n continue\n data = serv.get('{0}:{1}'.format(minion, jid))\n if data:\n ret[minion] = salt.utils.json.loads(data)\n return ret", "response": "Return a dict of the last called function for all minions\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_jids():\n '''\n Return a dict mapping all job ids to job information\n '''\n serv = _get_serv(ret=None)\n ret = {}\n for s in serv.mget(serv.keys('load:*')):\n if s is None:\n continue\n load = salt.utils.json.loads(s)\n jid = load['jid']\n ret[jid] = salt.utils.jid.format_jid_instance(jid, load)\n return ret", "response": "Return a dict mapping all job ids to job information"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef clean_old_jobs():\n '''\n Clean out minions's return data for old jobs.\n\n Normally, hset 'ret:' are saved with a TTL, and will eventually\n get cleaned by redis.But for jobs with some very late minion return, the\n corresponding hset's TTL will be refreshed to a too late timestamp, we'll\n do manually cleaning here.\n '''\n serv = _get_serv(ret=None)\n ret_jids = serv.keys('ret:*')\n living_jids = set(serv.keys('load:*'))\n to_remove = []\n for ret_key in ret_jids:\n load_key = ret_key.replace('ret:', 'load:', 1)\n if load_key not in living_jids:\n to_remove.append(ret_key)\n if to_remove:\n serv.delete(*to_remove)\n log.debug('clean old jobs: %s', to_remove)", "response": "Clean out jobs that are not in the load list"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\npreparing the salt - api daemon.", "response": "def prepare(self):\n '''\n Run the preparation sequence required to start a salt-api daemon.\n\n If sub-classed, don't **ever** forget to run:\n\n super(YourSubClass, self).prepare()\n '''\n super(SaltAPI, self).prepare()\n\n try:\n if self.config['verify_env']:\n logfile = self.config['log_file']\n if logfile is not None and not logfile.startswith(('tcp://',\n 'udp://',\n 'file://')):\n # Logfile is not using Syslog, verify\n with salt.utils.files.set_umask(0o027):\n verify_files([logfile], self.config['user'])\n except OSError as err:\n log.exception('Failed to prepare salt environment')\n self.shutdown(err.errno)\n\n self.setup_logfile_logger()\n verify_log(self.config)\n log.info('Setting up the Salt API')\n self.api = salt.client.netapi.NetapiClient(self.config)\n self.daemonize_if_required()\n self.set_pidfile()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nstart the actual master. If sub-classed, don't **ever** forget to run: super(YourSubClass, self).start() NOTE: Run any required code before calling `super()`.", "response": "def start(self):\n '''\n Start the actual master.\n\n If sub-classed, don't **ever** forget to run:\n\n super(YourSubClass, self).start()\n\n NOTE: Run any required code before calling `super()`.\n '''\n super(SaltAPI, self).start()\n if check_user(self.config['user']):\n log.info('The salt-api is starting up')\n self.api.run()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nshutting down the salt - api.", "response": "def shutdown(self, exitcode=0, exitmsg=None):\n '''\n If sub-classed, run any shutdown operations on this method.\n '''\n log.info('The salt-api is shutting down..')\n msg = 'The salt-api is shutdown. '\n if exitmsg is not None:\n exitmsg = msg + exitmsg\n else:\n exitmsg = msg.strip()\n super(SaltAPI, self).shutdown(exitcode, exitmsg)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef present(name, value, acls=None, ephemeral=False, sequence=False, makepath=False, version=-1,\n profile=None, hosts=None, scheme=None, username=None, password=None, default_acl=None):\n '''\n Make sure znode is present in the correct state with the correct acls\n\n name\n path to znode\n\n value\n value znode should be set to\n\n acls\n list of acl dictionaries to set on znode (make sure the ones salt is connected with are included)\n Default: None\n\n ephemeral\n Boolean to indicate if ephemeral znode should be created\n Default: False\n\n sequence\n Boolean to indicate if znode path is suffixed with a unique index\n Default: False\n\n makepath\n Boolean to indicate if the parent paths should be created\n Default: False\n\n version\n For updating, specify the version which should be updated\n Default: -1 (always match)\n\n profile\n Configured Zookeeper profile to authenticate with (Default: None)\n\n hosts\n Lists of Zookeeper Hosts (Default: '127.0.0.1:2181)\n\n scheme\n Scheme to authenticate with (Default: 'digest')\n\n username\n Username to authenticate (Default: None)\n\n password\n Password to authenticate (Default: None)\n\n default_acl\n Default acls to assign if a node is created in this connection (Default: None)\n\n .. code-block:: yaml\n\n add znode:\n zookeeper.present:\n - name: /test/name\n - value: gtmanfred\n - makepath: True\n\n update znode:\n zookeeper.present:\n - name: /test/name\n - value: daniel\n - acls:\n - username: daniel\n password: test\n read: true\n - username: gtmanfred\n password: test\n read: true\n write: true\n create: true\n delete: true\n admin: true\n - makepath: True\n '''\n\n ret = {'name': name,\n 'result': False,\n 'comment': 'Failed to setup znode {0}'.format(name),\n 'changes': {}}\n connkwargs = {'profile': profile, 'hosts': hosts, 'scheme': scheme,\n 'username': username, 'password': password,\n 'default_acl': default_acl}\n if acls is None:\n chk_acls = []\n else:\n chk_acls = [__salt__['zookeeper.make_digest_acl'](**acl) for acl in acls]\n if __salt__['zookeeper.exists'](name, **connkwargs):\n cur_value = __salt__['zookeeper.get'](name, **connkwargs)\n cur_acls = __salt__['zookeeper.get_acls'](name, **connkwargs)\n if cur_value == value and (not chk_acls or _check_acls(cur_acls, chk_acls)):\n ret['result'] = True\n ret['comment'] = 'Znode {0} is already set to the correct value with the correct acls'.format(name)\n return ret\n elif __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] = 'Znode {0} will be updated'.format(name)\n ret['changes']['old'] = {}\n ret['changes']['new'] = {}\n if value != cur_value:\n ret['changes']['old']['value'] = cur_value\n ret['changes']['new']['value'] = value\n if chk_acls and not _check_acls(chk_acls, cur_acls):\n ret['changes']['old']['acls'] = cur_acls\n ret['changes']['new']['acls'] = chk_acls\n return ret\n else:\n value_result, acl_result = True, True\n changes = {}\n if value != cur_value:\n __salt__['zookeeper.set'](name, value, version, **connkwargs)\n new_value = __salt__['zookeeper.get'](name, **connkwargs)\n value_result = new_value == value\n changes.setdefault('new', {}).setdefault('value', new_value)\n changes.setdefault('old', {}).setdefault('value', cur_value)\n if chk_acls and not _check_acls(chk_acls, cur_acls):\n __salt__['zookeeper.set_acls'](name, acls, version, **connkwargs)\n new_acls = __salt__['zookeeper.get_acls'](name, **connkwargs)\n acl_result = _check_acls(new_acls, chk_acls)\n changes.setdefault('new', {}).setdefault('acls', new_acls)\n changes.setdefault('old', {}).setdefault('value', cur_acls)\n ret['changes'] = changes\n if value_result and acl_result:\n ret['result'] = True\n ret['comment'] = 'Znode {0} successfully updated'.format(name)\n return ret\n\n if __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] = '{0} is will be created'.format(name)\n ret['changes']['old'] = {}\n ret['changes']['new'] = {}\n ret['changes']['new']['acls'] = chk_acls\n ret['changes']['new']['value'] = value\n return ret\n\n __salt__['zookeeper.create'](name, value, acls, ephemeral, sequence, makepath, **connkwargs)\n\n value_result, acl_result = True, True\n changes = {'old': {}}\n\n new_value = __salt__['zookeeper.get'](name, **connkwargs)\n value_result = new_value == value\n changes.setdefault('new', {}).setdefault('value', new_value)\n\n new_acls = __salt__['zookeeper.get_acls'](name, **connkwargs)\n acl_result = acls is None or _check_acls(new_acls, chk_acls)\n changes.setdefault('new', {}).setdefault('acls', new_acls)\n\n ret['changes'] = changes\n if value_result and acl_result:\n ret['result'] = True\n ret['comment'] = 'Znode {0} successfully created'.format(name)\n\n return ret", "response": "Ensures that the named node is present in the correct state with the specified acls."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nensuring that the named znode is absent", "response": "def absent(name, version=-1, recursive=False, profile=None, hosts=None, scheme=None,\n username=None, password=None, default_acl=None):\n '''\n Make sure znode is absent\n\n name\n path to znode\n\n version\n Specify the version which should be deleted\n Default: -1 (always match)\n\n recursive\n Boolean to indicate if children should be recursively deleted\n Default: False\n\n profile\n Configured Zookeeper profile to authenticate with (Default: None)\n\n hosts\n Lists of Zookeeper Hosts (Default: '127.0.0.1:2181)\n\n scheme\n Scheme to authenticate with (Default: 'digest')\n\n username\n Username to authenticate (Default: None)\n\n password\n Password to authenticate (Default: None)\n\n default_acl\n Default acls to assign if a node is created in this connection (Default: None)\n\n .. code-block:: yaml\n\n delete znode:\n zookeeper.absent:\n - name: /test\n - recursive: True\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': 'Failed to delete znode {0}'.format(name),\n 'changes': {}}\n connkwargs = {'profile': profile, 'hosts': hosts, 'scheme': scheme,\n 'username': username, 'password': password,\n 'default_acl': default_acl}\n\n if __salt__['zookeeper.exists'](name, **connkwargs) is False:\n ret['result'] = True\n ret['comment'] = 'Znode {0} does not exist'.format(name)\n return ret\n\n changes = {}\n changes['value'] = __salt__['zookeeper.get'](name, **connkwargs)\n changes['acls'] = __salt__['zookeeper.get_acls'](name, **connkwargs)\n if recursive is True:\n changes['children'] = __salt__['zookeeper.get_children'](name, **connkwargs)\n\n if __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] = 'Znode {0} will be removed'.format(name)\n ret['changes']['old'] = changes\n return ret\n\n __salt__['zookeeper.delete'](name, version, recursive, **connkwargs)\n\n if __salt__['zookeeper.exists'](name, **connkwargs) is False:\n ret['result'] = True\n ret['comment'] = 'Znode {0} has been removed'.format(name)\n ret['changes']['old'] = changes\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting acls on a znode", "response": "def acls(name, acls, version=-1, profile=None, hosts=None, scheme=None,\n username=None, password=None, default_acl=None):\n '''\n Update acls on a znode\n\n name\n path to znode\n\n acls\n list of acl dictionaries to set on znode\n\n version\n Specify the version which should be deleted\n Default: -1 (always match)\n\n profile\n Configured Zookeeper profile to authenticate with (Default: None)\n\n hosts\n Lists of Zookeeper Hosts (Default: '127.0.0.1:2181)\n\n scheme\n Scheme to authenticate with (Default: 'digest')\n\n username\n Username to authenticate (Default: None)\n\n password\n Password to authenticate (Default: None)\n\n default_acl\n Default acls to assign if a node is created in this connection (Default: None)\n\n .. code-block:: yaml\n\n update acls:\n zookeeper.acls:\n - name: /test/name\n - acls:\n - username: daniel\n password: test\n all: True\n - username: gtmanfred\n password: test\n all: True\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': 'Failed to set acls on znode {0}'.format(name),\n 'changes': {}}\n connkwargs = {'profile': profile, 'hosts': hosts, 'scheme': scheme,\n 'username': username, 'password': password,\n 'default_acl': default_acl}\n if isinstance(acls, dict):\n acls = [acls]\n chk_acls = [__salt__['zookeeper.make_digest_acl'](**acl) for acl in acls]\n\n if not __salt__['zookeeper.exists'](name, **connkwargs):\n ret['comment'] += ': Znode does not exist'\n return ret\n\n cur_acls = __salt__['zookeeper.get_acls'](name, **connkwargs)\n if _check_acls(cur_acls, chk_acls):\n ret['result'] = True\n ret['comment'] = 'Znode {0} acls already set'.format(name)\n return ret\n\n if __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] = 'Znode {0} acls will be updated'.format(name)\n ret['changes']['old'] = cur_acls\n ret['changes']['new'] = chk_acls\n return ret\n\n __salt__['zookeeper.set_acls'](name, acls, version, **connkwargs)\n\n new_acls = __salt__['zookeeper.get_acls'](name, **connkwargs)\n ret['changes'] = {'old': cur_acls, 'new': new_acls}\n if _check_acls(new_acls, chk_acls):\n ret['result'] = True\n ret['comment'] = 'Znode {0} acls updated'.format(name)\n return ret\n ret['comment'] = 'Znode {0} acls failed to update'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _timedatectl():\n '''\n get the output of timedatectl\n '''\n ret = __salt__['cmd.run_all'](['timedatectl'], python_shell=False)\n\n if ret['retcode'] != 0:\n msg = 'timedatectl failed: {0}'.format(ret['stderr'])\n raise CommandExecutionError(msg)\n\n return ret", "response": "get the output of timedatectl\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the timezone in the system clock", "response": "def _get_adjtime_timezone():\n '''\n Return the timezone in /etc/adjtime of the system clock\n '''\n adjtime_file = '/etc/adjtime'\n if os.path.exists(adjtime_file):\n cmd = ['tail', '-n', '1', adjtime_file]\n return __salt__['cmd.run'](cmd, python_shell=False)\n elif os.path.exists('/dev/rtc'):\n raise CommandExecutionError(\n 'Unable to get hwclock timezone from ' + adjtime_file\n )\n else:\n # There is no RTC.\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the current timezone for the current system", "response": "def get_zone():\n '''\n Get current timezone (i.e. America/Denver)\n\n .. versionchanged:: 2016.11.4\n\n .. note::\n\n On AIX operating systems, Posix values can also be returned\n 'CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00'\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' timezone.get_zone\n '''\n if salt.utils.path.which('timedatectl'):\n ret = _timedatectl()\n\n for line in (x.strip() for x in salt.utils.itertools.split(ret['stdout'], '\\n')):\n try:\n return re.match(r'Time ?zone:\\s+(\\S+)', line).group(1)\n except AttributeError:\n pass\n\n msg = ('Failed to parse timedatectl output: {0}\\n'\n 'Please file an issue with SaltStack').format(ret['stdout'])\n raise CommandExecutionError(msg)\n\n else:\n if __grains__['os'].lower() == 'centos':\n return _get_zone_etc_localtime()\n os_family = __grains__['os_family']\n for family in ('RedHat', 'Suse'):\n if family in os_family:\n return _get_zone_sysconfig()\n for family in ('Debian', 'Gentoo'):\n if family in os_family:\n return _get_zone_etc_timezone()\n if os_family in ('FreeBSD', 'OpenBSD', 'NetBSD', 'NILinuxRT'):\n return _get_zone_etc_localtime()\n elif 'Solaris' in os_family:\n return _get_zone_solaris()\n elif 'AIX' in os_family:\n return _get_zone_aix()\n raise CommandExecutionError('Unable to get timezone')"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting current numeric timezone offset from UCT", "response": "def get_offset():\n '''\n Get current numeric timezone offset from UCT (i.e. -0700)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' timezone.get_offset\n '''\n if 'AIX' not in __grains__['os_family']:\n return __salt__['cmd.run'](['date', '+%z'], python_shell=False)\n\n salt_path = '/opt/salt/bin/date'\n\n if not os.path.exists(salt_path):\n return 'date in salt binaries does not exist: {0}'.format(salt_path)\n\n return __salt__['cmd.run']([salt_path, '+%z'], python_shell=False)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting the timezone of the current system process.", "response": "def set_zone(timezone):\n '''\n Unlinks, then symlinks /etc/localtime to the set timezone.\n\n The timezone is crucial to several system processes, each of which SHOULD\n be restarted (for instance, whatever you system uses as its cron and\n syslog daemons). This will not be automagically done and must be done\n manually!\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' timezone.set_zone 'America/Denver'\n\n .. versionchanged:: 2016.11.4\n\n .. note::\n\n On AIX operating systems, Posix values are also allowed, see below\n\n .. code-block:: bash\n\n salt '*' timezone.set_zone 'CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00'\n\n '''\n if salt.utils.path.which('timedatectl'):\n try:\n __salt__['cmd.run']('timedatectl set-timezone {0}'.format(timezone))\n except CommandExecutionError:\n pass\n\n if 'Solaris' in __grains__['os_family'] or 'AIX' in __grains__['os_family']:\n zonepath = '/usr/share/lib/zoneinfo/{0}'.format(timezone)\n else:\n zonepath = '/usr/share/zoneinfo/{0}'.format(timezone)\n\n if not os.path.exists(zonepath) and 'AIX' not in __grains__['os_family']:\n return 'Zone does not exist: {0}'.format(zonepath)\n\n tzfile = _get_localtime_path()\n if os.path.exists(tzfile):\n os.unlink(tzfile)\n\n if 'Solaris' in __grains__['os_family']:\n __salt__['file.sed'](\n '/etc/default/init', '^TZ=.*', 'TZ={0}'.format(timezone))\n elif 'AIX' in __grains__['os_family']:\n # timezone could be Olson or Posix\n curtzstring = get_zone()\n cmd = ['chtz', timezone]\n result = __salt__['cmd.retcode'](cmd, python_shell=False)\n if result == 0:\n return True\n\n # restore orig timezone, since AIX chtz failure sets UTC\n cmd = ['chtz', curtzstring]\n __salt__['cmd.retcode'](cmd, python_shell=False)\n return False\n else:\n os.symlink(zonepath, tzfile)\n\n if 'RedHat' in __grains__['os_family']:\n __salt__['file.sed'](\n '/etc/sysconfig/clock', '^ZONE=.*', 'ZONE=\"{0}\"'.format(timezone))\n elif 'Suse' in __grains__['os_family']:\n __salt__['file.sed'](\n '/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE=\"{0}\"'.format(timezone))\n elif 'Debian' in __grains__['os_family'] or 'Gentoo' in __grains__['os_family']:\n with salt.utils.files.fopen('/etc/timezone', 'w') as ofh:\n ofh.write(salt.utils.stringutils.to_str(timezone).strip())\n ofh.write('\\n')\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef zone_compare(timezone):\n '''\n Compares the given timezone name with the system timezone name.\n Checks the hash sum between the given timezone, and the one set in\n /etc/localtime. Returns True if names and hash sums match, and False if not.\n Mostly useful for running state checks.\n\n .. versionchanged:: 2016.3.0\n\n .. note::\n\n On Solaris-link operating systems only a string comparison is done.\n\n .. versionchanged:: 2016.11.4\n\n .. note::\n\n On AIX operating systems only a string comparison is done.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' timezone.zone_compare 'America/Denver'\n '''\n if 'Solaris' in __grains__['os_family'] or 'AIX' in __grains__['os_family']:\n return timezone == get_zone()\n\n if 'FreeBSD' in __grains__['os_family']:\n if not os.path.isfile(_get_localtime_path()):\n return timezone == get_zone()\n\n tzfile = _get_localtime_path()\n zonepath = _get_zone_file(timezone)\n try:\n return filecmp.cmp(tzfile, zonepath, shallow=False)\n except OSError as exc:\n problematic_file = exc.filename\n if problematic_file == zonepath:\n raise SaltInvocationError(\n 'Can\\'t find a local timezone \"{0}\"'.format(timezone))\n elif problematic_file == tzfile:\n raise CommandExecutionError(\n 'Failed to read {0} to determine current timezone: {1}'\n .format(tzfile, exc.strerror))\n raise", "response": "Compare the given timezone name with the system timezone name."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_hwclock():\n '''\n Get current hardware clock setting (UTC or localtime)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' timezone.get_hwclock\n '''\n if salt.utils.path.which('timedatectl'):\n ret = _timedatectl()\n for line in (x.strip() for x in ret['stdout'].splitlines()):\n if 'rtc in local tz' in line.lower():\n try:\n if line.split(':')[-1].strip().lower() == 'yes':\n return 'localtime'\n else:\n return 'UTC'\n except IndexError:\n pass\n\n msg = ('Failed to parse timedatectl output: {0}\\n'\n 'Please file an issue with SaltStack').format(ret['stdout'])\n raise CommandExecutionError(msg)\n\n else:\n os_family = __grains__['os_family']\n for family in ('RedHat', 'Suse', 'NILinuxRT'):\n if family in os_family:\n return _get_adjtime_timezone()\n\n if 'Debian' in __grains__['os_family']:\n # Original way to look up hwclock on Debian-based systems\n try:\n with salt.utils.files.fopen('/etc/default/rcS', 'r') as fp_:\n for line in fp_:\n line = salt.utils.stringutils.to_unicode(line)\n if re.match(r'^\\s*#', line):\n continue\n if 'UTC=' in line:\n is_utc = line.rstrip('\\n').split('=')[-1].lower()\n if is_utc == 'yes':\n return 'UTC'\n else:\n return 'localtime'\n except IOError as exc:\n pass\n # Since Wheezy\n return _get_adjtime_timezone()\n\n if 'Gentoo' in __grains__['os_family']:\n if not os.path.exists('/etc/adjtime'):\n offset_file = '/etc/conf.d/hwclock'\n try:\n with salt.utils.files.fopen(offset_file, 'r') as fp_:\n for line in fp_:\n line = salt.utils.stringutils.to_unicode(line)\n if line.startswith('clock='):\n line = line.rstrip('\\n')\n line = line.split('=')[-1].strip('\\'\"')\n if line == 'UTC':\n return line\n if line == 'local':\n return 'LOCAL'\n raise CommandExecutionError(\n 'Correct offset value not found in {0}'\n .format(offset_file)\n )\n except IOError as exc:\n raise CommandExecutionError(\n 'Problem reading offset file {0}: {1}'\n .format(offset_file, exc.strerror)\n )\n return _get_adjtime_timezone()\n\n if 'Solaris' in __grains__['os_family']:\n offset_file = '/etc/rtc_config'\n try:\n with salt.utils.files.fopen(offset_file, 'r') as fp_:\n for line in fp_:\n line = salt.utils.stringutils.to_unicode(line)\n if line.startswith('zone_info=GMT'):\n return 'UTC'\n return 'localtime'\n except IOError as exc:\n if exc.errno == errno.ENOENT:\n # offset file does not exist\n return 'UTC'\n raise CommandExecutionError(\n 'Problem reading offset file {0}: {1}'\n .format(offset_file, exc.strerror)\n )\n\n if 'AIX' in __grains__['os_family']:\n offset_file = '/etc/environment'\n try:\n with salt.utils.files.fopen(offset_file, 'r') as fp_:\n for line in fp_:\n line = salt.utils.stringutils.to_unicode(line)\n if line.startswith('TZ=UTC'):\n return 'UTC'\n return 'localtime'\n except IOError as exc:\n if exc.errno == errno.ENOENT:\n # offset file does not exist\n return 'UTC'\n raise CommandExecutionError(\n 'Problem reading offset file {0}: {1}'\n .format(offset_file, exc.strerror)\n )", "response": "Return the current hardware clock setting"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nset the hardware clock for the current system to either UTC or localtime", "response": "def set_hwclock(clock):\n '''\n Sets the hardware clock to be either UTC or localtime\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' timezone.set_hwclock UTC\n '''\n if salt.utils.path.which('timedatectl'):\n cmd = ['timedatectl', 'set-local-rtc',\n 'true' if clock == 'localtime' else 'false']\n return __salt__['cmd.retcode'](cmd, python_shell=False) == 0\n else:\n os_family = __grains__['os_family']\n if os_family in ('AIX', 'NILinuxRT'):\n if clock.lower() != 'utc':\n raise SaltInvocationError(\n 'UTC is the only permitted value'\n )\n return True\n\n timezone = get_zone()\n\n if 'Solaris' in __grains__['os_family']:\n if clock.lower() not in ('localtime', 'utc'):\n raise SaltInvocationError(\n 'localtime and UTC are the only permitted values'\n )\n if 'sparc' in __grains__['cpuarch']:\n raise SaltInvocationError(\n 'UTC is the only choice for SPARC architecture'\n )\n cmd = ['rtc', '-z', 'GMT' if clock.lower() == 'utc' else timezone]\n return __salt__['cmd.retcode'](cmd, python_shell=False) == 0\n\n zonepath = '/usr/share/zoneinfo/{0}'.format(timezone)\n\n if not os.path.exists(zonepath):\n raise CommandExecutionError(\n 'Zone \\'{0}\\' does not exist'.format(zonepath)\n )\n\n os.unlink('/etc/localtime')\n os.symlink(zonepath, '/etc/localtime')\n\n if 'Arch' in __grains__['os_family']:\n cmd = ['timezonectl', 'set-local-rtc',\n 'true' if clock == 'localtime' else 'false']\n return __salt__['cmd.retcode'](cmd, python_shell=False) == 0\n elif 'RedHat' in __grains__['os_family']:\n __salt__['file.sed'](\n '/etc/sysconfig/clock', '^ZONE=.*', 'ZONE=\"{0}\"'.format(timezone))\n elif 'Suse' in __grains__['os_family']:\n __salt__['file.sed'](\n '/etc/sysconfig/clock', '^TIMEZONE=.*', 'TIMEZONE=\"{0}\"'.format(timezone))\n elif 'Debian' in __grains__['os_family']:\n if clock == 'UTC':\n __salt__['file.sed']('/etc/default/rcS', '^UTC=.*', 'UTC=yes')\n elif clock == 'localtime':\n __salt__['file.sed']('/etc/default/rcS', '^UTC=.*', 'UTC=no')\n elif 'Gentoo' in __grains__['os_family']:\n if clock not in ('UTC', 'localtime'):\n raise SaltInvocationError(\n 'Only \\'UTC\\' and \\'localtime\\' are allowed'\n )\n if clock == 'localtime':\n clock = 'local'\n __salt__['file.sed'](\n '/etc/conf.d/hwclock', '^clock=.*', 'clock=\"{0}\"'.format(clock))\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a new Chocolatey package if it is already installed.", "response": "def installed(name, version=None, source=None, force=False, pre_versions=False,\n install_args=None, override_args=False, force_x86=False,\n package_args=None, allow_multiple=False, execution_timeout=None):\n '''\n Installs a package if not already installed\n\n Args:\n\n name (str):\n The name of the package to be installed. Required.\n\n version (str):\n Install a specific version of the package. Defaults to latest\n version. If the version is different to the one installed then the\n specified version will be installed. Default is None.\n\n source (str):\n Chocolatey repository (directory, share or remote URL, feed).\n Defaults to the official Chocolatey feed. Default is None.\n\n force (bool):\n Reinstall the current version of an existing package. Do not use\n with ``allow_multiple``. Default is False.\n\n pre_versions (bool):\n Include pre-release packages. Default is False.\n\n install_args (str):\n Install arguments you want to pass to the installation process, i.e\n product key or feature list. Default is None.\n\n override_args (bool):\n Set to True if you want to override the original install arguments\n (for the native installer) in the package and use your own. When\n this is set to False install_args will be appended to the end of the\n default arguments. Default is False.\n\n force_x86 (bool):\n Force x86 (32bit) installation on 64 bit systems. Default is False.\n\n package_args (str):\n Arguments you want to pass to the package. Default is None.\n\n allow_multiple (bool):\n Allow mulitiple versions of the package to be installed. Do not use\n with ``force``. Does not work with all packages. Default is False.\n\n .. versionadded:: 2017.7.0\n\n execution_timeout (str):\n Chocolatey execution timeout value you want to pass to the\n installation process. Default is None.\n\n .. code-block:: yaml\n\n Installsomepackage:\n chocolatey.installed:\n - name: packagename\n - version: '12.04'\n - source: 'mychocolatey/source'\n - force: True\n '''\n if force and allow_multiple:\n raise SaltInvocationError(\n 'Cannot use \\'force\\' in conjunction with \\'allow_multiple\\'')\n\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n # Get list of currently installed packages\n pre_install = __salt__['chocolatey.list'](local_only=True)\n\n # Determine action\n # Package not installed\n if name.lower() not in [package.lower() for package in pre_install.keys()]:\n if version:\n ret['changes'] = {name: 'Version {0} will be installed'.format(version)}\n else:\n ret['changes'] = {name: 'Latest version will be installed'}\n\n # Package installed\n else:\n version_info = __salt__['chocolatey.version'](name=name,\n check_remote=True,\n source=source)\n\n full_name = name\n for pkg in version_info:\n if name.lower() == pkg.lower():\n full_name = pkg\n\n installed_version = version_info[full_name]['installed'][0]\n\n if version:\n if salt.utils.versions.compare(\n ver1=installed_version, oper=\"==\", ver2=version):\n if force:\n ret['changes'] = {\n name: 'Version {0} will be reinstalled'.format(version)}\n ret['comment'] = 'Reinstall {0} {1}'.format(full_name, version)\n else:\n ret['comment'] = '{0} {1} is already installed'.format(name, version)\n if __opts__['test']:\n ret['result'] = None\n return ret\n else:\n if allow_multiple:\n ret['changes'] = {\n name: 'Version {0} will be installed side by side with '\n 'Version {1} if supported'.format(version, installed_version)\n }\n ret['comment'] = (\n 'Install {0} {1} side-by-side with {0} {2}'.format(\n full_name, version, installed_version\n )\n )\n else:\n ret['changes'] = {\n name: 'Version {0} will be installed over Version {1}'.format(version, installed_version)\n }\n ret['comment'] = 'Install {0} {1} over {0} {2}'.format(\n full_name, version, installed_version\n )\n force = True\n else:\n version = installed_version\n if force:\n ret['changes'] = {\n name: 'Version {0} will be reinstalled'.format(version)}\n ret['comment'] = 'Reinstall {0} {1}'.format(full_name, version)\n else:\n ret['comment'] = '{0} {1} is already installed'.format(name, version)\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'The installation was tested'\n return ret\n\n # Install the package\n result = __salt__['chocolatey.install'](name=name,\n version=version,\n source=source,\n force=force,\n pre_versions=pre_versions,\n install_args=install_args,\n override_args=override_args,\n force_x86=force_x86,\n package_args=package_args,\n allow_multiple=allow_multiple,\n execution_timeout=execution_timeout)\n\n if 'Running chocolatey failed' not in result:\n ret['result'] = True\n else:\n ret['result'] = False\n\n if not ret['result']:\n ret['comment'] = 'Failed to install the package {0}'.format(name)\n\n # Get list of installed packages after 'chocolatey.install'\n post_install = __salt__['chocolatey.list'](local_only=True)\n\n ret['changes'] = salt.utils.data.compare_dicts(pre_install, post_install)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef uninstalled(name, version=None, uninstall_args=None, override_args=False):\n '''\n Uninstalls a package\n\n name\n The name of the package to be uninstalled\n\n version\n Uninstalls a specific version of the package. Defaults to latest\n version installed.\n\n uninstall_args\n A list of uninstall arguments you want to pass to the uninstallation\n process i.e product key or feature list\n\n override_args\n Set to true if you want to override the original uninstall arguments (\n for the native uninstaller)in the package and use your own.\n When this is set to False uninstall_args will be appended to the end of\n the default arguments\n\n .. code-block: yaml\n\n Removemypackage:\n chocolatey.uninstalled:\n - name: mypackage\n - version: '21.5'\n\n '''\n\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n # Get list of currently installed packages\n pre_uninstall = __salt__['chocolatey.list'](local_only=True)\n\n # Determine if package is installed\n if name.lower() in [package.lower() for package in pre_uninstall.keys()]:\n try:\n ret['changes'] = {\n name: '{0} version {1} will be removed'.format(\n name, pre_uninstall[name][0]\n )\n }\n except KeyError:\n ret['changes'] = {name: '{0} will be removed'.format(name)}\n else:\n ret['comment'] = 'The package {0} is not installed'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'The uninstall was tested'\n return ret\n\n # Uninstall the package\n result = __salt__['chocolatey.uninstall'](name,\n version,\n uninstall_args,\n override_args)\n\n if 'Running chocolatey failed' not in result:\n ret['result'] = True\n else:\n ret['result'] = False\n\n if not ret['result']:\n ret['comment'] = 'Failed to uninstall the package {0}'.format(name)\n\n # Get list of installed packages after 'chocolatey.uninstall'\n post_uninstall = __salt__['chocolatey.list'](local_only=True)\n\n ret['changes'] = salt.utils.data.compare_dicts(pre_uninstall, post_uninstall)\n\n return ret", "response": "Uninstalls a package from the chocolatey system"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstore the job information using the configured master_job_cache", "response": "def store_job(opts, load, event=None, mminion=None):\n '''\n Store job information using the configured master_job_cache\n '''\n # Generate EndTime\n endtime = salt.utils.jid.jid_to_time(salt.utils.jid.gen_jid(opts))\n # If the return data is invalid, just ignore it\n if any(key not in load for key in ('return', 'jid', 'id')):\n return False\n if not salt.utils.verify.valid_id(opts, load['id']):\n return False\n if mminion is None:\n mminion = salt.minion.MasterMinion(opts, states=False, rend=False)\n\n job_cache = opts['master_job_cache']\n if load['jid'] == 'req':\n # The minion is returning a standalone job, request a jobid\n load['arg'] = load.get('arg', load.get('fun_args', []))\n load['tgt_type'] = 'glob'\n load['tgt'] = load['id']\n\n prep_fstr = '{0}.prep_jid'.format(opts['master_job_cache'])\n try:\n load['jid'] = mminion.returners[prep_fstr](nocache=load.get('nocache', False))\n except KeyError:\n emsg = \"Returner '{0}' does not support function prep_jid\".format(job_cache)\n log.error(emsg)\n raise KeyError(emsg)\n\n # save the load, since we don't have it\n saveload_fstr = '{0}.save_load'.format(job_cache)\n try:\n mminion.returners[saveload_fstr](load['jid'], load)\n except KeyError:\n emsg = \"Returner '{0}' does not support function save_load\".format(job_cache)\n log.error(emsg)\n raise KeyError(emsg)\n elif salt.utils.jid.is_jid(load['jid']):\n # Store the jid\n jidstore_fstr = '{0}.prep_jid'.format(job_cache)\n try:\n mminion.returners[jidstore_fstr](False, passed_jid=load['jid'])\n except KeyError:\n emsg = \"Returner '{0}' does not support function prep_jid\".format(job_cache)\n log.error(emsg)\n raise KeyError(emsg)\n\n if event:\n # If the return data is invalid, just ignore it\n log.info('Got return from %s for job %s', load['id'], load['jid'])\n event.fire_event(load,\n salt.utils.event.tagify([load['jid'], 'ret', load['id']], 'job'))\n event.fire_ret_load(load)\n\n # if you have a job_cache, or an ext_job_cache, don't write to\n # the regular master cache\n if not opts['job_cache'] or opts.get('ext_job_cache'):\n return\n\n # do not cache job results if explicitly requested\n if load.get('jid') == 'nocache':\n log.debug('Ignoring job return with jid for caching %s from %s',\n load['jid'], load['id'])\n return\n\n # otherwise, write to the master cache\n savefstr = '{0}.save_load'.format(job_cache)\n getfstr = '{0}.get_load'.format(job_cache)\n fstr = '{0}.returner'.format(job_cache)\n updateetfstr = '{0}.update_endtime'.format(job_cache)\n if 'fun' not in load and load.get('return', {}):\n ret_ = load.get('return', {})\n if 'fun' in ret_:\n load.update({'fun': ret_['fun']})\n if 'user' in ret_:\n load.update({'user': ret_['user']})\n\n # Try to reach returner methods\n try:\n savefstr_func = mminion.returners[savefstr]\n getfstr_func = mminion.returners[getfstr]\n fstr_func = mminion.returners[fstr]\n except KeyError as error:\n emsg = \"Returner '{0}' does not support function {1}\".format(job_cache, error)\n log.error(emsg)\n raise KeyError(emsg)\n\n if job_cache != 'local_cache':\n try:\n mminion.returners[savefstr](load['jid'], load)\n except KeyError as e:\n log.error(\"Load does not contain 'jid': %s\", e)\n\n mminion.returners[fstr](load)\n\n if (opts.get('job_cache_store_endtime')\n and updateetfstr in mminion.returners):\n mminion.returners[updateetfstr](load['jid'], endtime)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nstoring additional minions matched on lower - level masters using the configured master_job_cache", "response": "def store_minions(opts, jid, minions, mminion=None, syndic_id=None):\n '''\n Store additional minions matched on lower-level masters using the configured\n master_job_cache\n '''\n if mminion is None:\n mminion = salt.minion.MasterMinion(opts, states=False, rend=False)\n job_cache = opts['master_job_cache']\n minions_fstr = '{0}.save_minions'.format(job_cache)\n\n try:\n mminion.returners[minions_fstr](jid, minions, syndic_id=syndic_id)\n except KeyError:\n raise KeyError(\n 'Returner \\'{0}\\' does not support function save_minions'.format(\n job_cache\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndetermines a retcode for a given return value", "response": "def get_retcode(ret):\n '''\n Determine a retcode for a given return\n '''\n retcode = 0\n # if there is a dict with retcode, use that\n if isinstance(ret, dict) and ret.get('retcode', 0) != 0:\n return ret['retcode']\n # if its a boolean, False means 1\n elif isinstance(ret, bool) and not ret:\n return 1\n return retcode"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef beacon(config):\n '''\n Checks for locked accounts due to too many invalid login attempts, 3 or higher.\n\n .. code-block:: yaml\n\n beacons:\n aix_account:\n user: ALL\n interval: 120\n\n '''\n\n ret = []\n\n user = config['user']\n\n locked_accounts = __salt__['shadow.login_failures'](user)\n ret.append({'accounts': locked_accounts})\n\n return ret", "response": "Check for locked accounts due to too many invalid login attempts 3 or higher."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\npost data to OpsGenie. It's designed for Salt's Event Reactor. After configuring the sls reaction file as shown above, you can trigger the module with your designated tag (og-tag in this case). CLI Example: .. code-block:: bash salt-call event.send 'og-tag' '{\"reason\" : \"Overheating CPU!\"}' Required parameters: api_key It's the API Key you've copied while adding integration in OpsGenie. reason It will be used as alert's default message in OpsGenie. action_type OpsGenie supports the default values Create/Close for action_type. You can customize this field with OpsGenie's custom actions for other purposes like adding notes or acknowledging alerts. Optional parameters: name It will be used as alert's alias. If you want to use the close functionality you must provide name field for both states like in this case.", "response": "def post_data(api_key=None, name='OpsGenie Execution Module', reason=None,\n action_type=None):\n '''\n Post data to OpsGenie. It's designed for Salt's Event Reactor.\n\n After configuring the sls reaction file as shown above, you can trigger the\n module with your designated tag (og-tag in this case).\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call event.send 'og-tag' '{\"reason\" : \"Overheating CPU!\"}'\n\n Required parameters:\n\n api_key\n It's the API Key you've copied while adding integration in OpsGenie.\n\n reason\n It will be used as alert's default message in OpsGenie.\n\n action_type\n OpsGenie supports the default values Create/Close for action_type. You\n can customize this field with OpsGenie's custom actions for other\n purposes like adding notes or acknowledging alerts.\n\n Optional parameters:\n\n name\n It will be used as alert's alias. If you want to use the close\n functionality you must provide name field for both states like in\n this case.\n '''\n if api_key is None or reason is None:\n raise salt.exceptions.SaltInvocationError(\n 'API Key or Reason cannot be None.')\n\n data = dict()\n data['alias'] = name\n data['message'] = reason\n # data['actions'] = action_type\n data['cpuModel'] = __grains__['cpu_model']\n data['cpuArch'] = __grains__['cpuarch']\n data['fqdn'] = __grains__['fqdn']\n data['host'] = __grains__['host']\n data['id'] = __grains__['id']\n data['kernel'] = __grains__['kernel']\n data['kernelRelease'] = __grains__['kernelrelease']\n data['master'] = __grains__['master']\n data['os'] = __grains__['os']\n data['saltPath'] = __grains__['saltpath']\n data['saltVersion'] = __grains__['saltversion']\n data['username'] = __grains__['username']\n data['uuid'] = __grains__['uuid']\n\n log.debug('Below data will be posted:\\n%s', data)\n log.debug('API Key: %s \\t API Endpoint: %s', api_key, API_ENDPOINT)\n\n if action_type == \"Create\":\n response = requests.post(\n url=API_ENDPOINT,\n data=salt.utils.json.dumps(data),\n headers={'Content-Type': 'application/json',\n 'Authorization': 'GenieKey ' + api_key})\n else:\n response = requests.post(\n url=API_ENDPOINT + \"/\" + name + \"/close?identifierType=alias\",\n data=salt.utils.json.dumps(data),\n headers={'Content-Type': 'application/json',\n 'Authorization': 'GenieKey ' + api_key})\n\n return response.status_code, response.text"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninstalling a Mac OS Package from a pkg or dmg file.", "response": "def installed(name, target=\"LocalSystem\", dmg=False, store=False, app=False, mpkg=False, user=None, onlyif=None,\n unless=None, force=False, allow_untrusted=False, version_check=None):\n '''\n Install a Mac OS Package from a pkg or dmg file, if given a dmg file it\n will first be mounted in a temporary location\n\n name\n The pkg or dmg file to install\n\n target\n The location in which to install the package. This can be a path or LocalSystem\n\n dmg\n Is the given file a dmg file?\n\n store\n Should the pkg be installed as if it was from the Mac OS Store?\n\n app\n Is the file a .app? If so then we'll just copy that to /Applications/ or the given\n target\n\n mpkg\n Is the file a .mpkg? If so then we'll check all of the .pkg files found are installed\n\n user\n Name of the user performing the unless or onlyif checks\n\n onlyif\n A command to run as a check, run the named command only if the command\n passed to the ``onlyif`` option returns true\n\n unless\n A command to run as a check, only run the named command if the command\n passed to the ``unless`` option returns false\n\n force\n Force the package to be installed even if its already been found installed\n\n allow_untrusted\n Allow the installation of untrusted packages\n\n version_check\n The command and version that we want to check against, the version number can use regex.\n\n .. code-block:: yaml\n\n version_check: python --version_check=2.7.[0-9]\n\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}}\n found = []\n installing = []\n\n real_pkg = name\n\n # Check onlyif, unless first\n run_check_cmd_kwargs = {'runas': user, 'python_shell': True}\n if 'shell' in __grains__:\n run_check_cmd_kwargs['shell'] = __grains__['shell']\n\n cret = _mod_run_check(run_check_cmd_kwargs, onlyif, unless)\n\n if isinstance(cret, dict):\n ret.update(cret)\n return ret\n\n # Check version info\n if version_check is not None:\n split = version_check.split(\"=\")\n if len(split) == 2:\n version_cmd = split[0]\n expected_version = split[1]\n try:\n version_out = __salt__['cmd.run'](version_cmd, output_loglevel=\"quiet\", ignore_retcode=True)\n version_out = version_out.strip()\n except CommandExecutionError:\n version_out = \"\"\n\n if re.match(expected_version, version_out) is not None:\n ret['comment'] += \"Version already matches {0}\".format(expected_version)\n return ret\n else:\n ret['comment'] += \"Version {0} doesn't match {1}. \".format(version_out, expected_version)\n\n if app and target == \"LocalSystem\":\n target = \"/Applications/\"\n\n # Mount the dmg first\n mount_point = None\n if dmg:\n out, mount_point = __salt__['macpackage.mount'](name)\n if 'attach failed' in out:\n ret['result'] = False\n ret['comment'] += 'Unable to mount {0}'.format(name)\n return ret\n\n if app:\n real_pkg = mount_point + \"/*.app\"\n elif mpkg:\n real_pkg = mount_point + \"/*.mpkg\"\n else:\n real_pkg = mount_point + \"/*.pkg\"\n\n try:\n # Check if we have already installed this\n if app:\n if dmg:\n # Run with python shell due to the wildcard\n cmd = 'ls -d *.app'\n out = __salt__['cmd.run'](cmd, cwd=mount_point, python_shell=True)\n\n if '.app' not in out:\n ret['result'] = False\n ret['comment'] += 'Unable to find .app in {0}'.format(mount_point)\n return ret\n else:\n pkg_ids = out.split(\"\\n\")\n else:\n pkg_ids = [os.path.basename(name)]\n mount_point = os.path.dirname(name)\n\n if onlyif is None and unless is None and version_check is None:\n for p in pkg_ids:\n if target[-4:] == \".app\":\n install_dir = target\n else:\n install_dir = os.path.join(target, p)\n if os.path.exists(install_dir) and force is False:\n found.append(p)\n else:\n installing.append(p)\n else:\n installing = pkg_ids\n else:\n installed_pkgs = __salt__['macpackage.installed_pkgs']()\n\n if mpkg:\n pkg_ids = __salt__['macpackage.get_mpkg_ids'](real_pkg)\n else:\n pkg_ids = __salt__['macpackage.get_pkg_id'](real_pkg)\n\n if pkg_ids:\n for p in pkg_ids:\n if p in installed_pkgs and force is False:\n found.append(p)\n else:\n installing.append(p)\n if len(pkg_ids) == len(found):\n return ret\n\n if app:\n def failed_pkg(f_pkg):\n ret['result'] = False\n ret['comment'] += '{0} failed to install: {1}'.format(name, out)\n\n if 'failed' in ret['changes']:\n ret['changes']['failed'].append(f_pkg)\n else:\n ret['changes']['failed'] = [f_pkg]\n\n for app in installing:\n try:\n log.info('Copying %s to %s', app, target)\n\n out = __salt__['macpackage.install_app'](os.path.join(mount_point, app), target)\n\n if out:\n failed_pkg(app)\n else:\n ret['comment'] += '{0} installed'.format(app)\n if 'installed' in ret['changes']:\n ret['changes']['installed'].append(app)\n else:\n ret['changes']['installed'] = [app]\n\n except OSError:\n failed_pkg(app)\n else:\n out = __salt__['macpackage.install'](real_pkg, target, store, allow_untrusted)\n\n if out['retcode'] != 0:\n ret['result'] = False\n ret['comment'] += '. {0} failed to install: {1}'.format(name, out)\n else:\n ret['comment'] += '{0} installed'.format(name)\n ret['changes']['installed'] = installing\n\n finally:\n if dmg:\n # Unmount to be kind\n __salt__['macpackage.unmount'](mount_point)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _mod_run_check(cmd_kwargs, onlyif, unless):\n '''\n Execute the onlyif and unless logic.\n Return a result dict if:\n * onlyif failed (onlyif != 0)\n * unless succeeded (unless == 0)\n else return True\n '''\n if onlyif:\n if __salt__['cmd.retcode'](onlyif, **cmd_kwargs) != 0:\n return {'comment': 'onlyif condition is false',\n 'skip_watch': True,\n 'result': True}\n\n if unless:\n if __salt__['cmd.retcode'](unless, **cmd_kwargs) == 0:\n return {'comment': 'unless condition is true',\n 'skip_watch': True,\n 'result': True}\n\n # No reason to stop, return True\n return True", "response": "Execute the onlyif and unless logic."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the status of a service via rest_sample. service_status", "response": "def status(name, sig=None):\n '''\n Return the status for a service via rest_sample.\n If the name contains globbing, a dict mapping service name to True/False\n values is returned.\n\n .. versionadded:: 2015.8.0\n\n .. versionchanged:: 2018.3.0\n The service name can now be a glob (e.g. ``salt*``)\n\n Args:\n name (str): The name of the service to check\n sig (str): Not implemented\n\n Returns:\n bool: True if running, False otherwise\n dict: Maps service name to True if running, False otherwise\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.status \n '''\n\n proxy_fn = 'rest_sample.service_status'\n contains_globbing = bool(re.search(r'\\*|\\?|\\[.+\\]', name))\n if contains_globbing:\n services = fnmatch.filter(get_all(), name)\n else:\n services = [name]\n results = {}\n for service in services:\n resp = __proxy__[proxy_fn](service)\n if resp['comment'] == 'running':\n results[service] = True\n else:\n results[service] = False\n if contains_globbing:\n return results\n return results[name]"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef clean_proc_dir(opts):\n\n '''\n Loop through jid files in the minion proc directory (default /var/cache/salt/minion/proc)\n and remove any that refer to processes that no longer exist\n '''\n\n for basefilename in os.listdir(salt.minion.get_proc_dir(opts['cachedir'])):\n fn_ = os.path.join(salt.minion.get_proc_dir(opts['cachedir']), basefilename)\n with salt.utils.files.fopen(fn_, 'rb') as fp_:\n job = None\n try:\n job = salt.payload.Serial(opts).load(fp_)\n except Exception: # It's corrupted\n # Windows cannot delete an open file\n if salt.utils.platform.is_windows():\n fp_.close()\n try:\n os.unlink(fn_)\n continue\n except OSError:\n continue\n log.debug(\n 'schedule.clean_proc_dir: checking job %s for process '\n 'existence', job\n )\n if job is not None and 'pid' in job:\n if salt.utils.process.os_is_running(job['pid']):\n log.debug(\n 'schedule.clean_proc_dir: Cleaning proc dir, pid %s '\n 'still exists.', job['pid']\n )\n else:\n # Windows cannot delete an open file\n if salt.utils.platform.is_windows():\n fp_.close()\n # Maybe the file is already gone\n try:\n os.unlink(fn_)\n except OSError:\n pass", "response": "Clean the proc directory and remove any processes that no longer exist."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef option(self, opt):\n '''\n Return options merged from config and pillar\n '''\n if 'config.merge' in self.functions:\n return self.functions['config.merge'](opt, {}, omit_master=True)\n return self.opts.get(opt, {})", "response": "Return options merged from config and pillar\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the schedule data structure", "response": "def _get_schedule(self,\n include_opts=True,\n include_pillar=True,\n remove_hidden=False):\n '''\n Return the schedule data structure\n '''\n schedule = {}\n if include_pillar:\n pillar_schedule = self.opts.get('pillar', {}).get('schedule', {})\n if not isinstance(pillar_schedule, dict):\n raise ValueError('Schedule must be of type dict.')\n schedule.update(pillar_schedule)\n if include_opts:\n opts_schedule = self.opts.get('schedule', {})\n if not isinstance(opts_schedule, dict):\n raise ValueError('Schedule must be of type dict.')\n schedule.update(opts_schedule)\n\n if remove_hidden:\n _schedule = copy.deepcopy(schedule)\n for job in _schedule:\n if isinstance(_schedule[job], dict):\n for item in _schedule[job]:\n if item.startswith('_'):\n del schedule[job][item]\n return schedule"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _check_max_running(self, func, data, opts, now):\n '''\n Return the schedule data structure\n '''\n # Check to see if there are other jobs with this\n # signature running. If there are more than maxrunning\n # jobs present then don't start another.\n # If jid_include is False for this job we can ignore all this\n # NOTE--jid_include defaults to True, thus if it is missing from the data\n # dict we treat it like it was there and is True\n\n # Check if we're able to run\n if not data['run']:\n return data\n if 'jid_include' not in data or data['jid_include']:\n jobcount = 0\n if self.opts['__role'] == 'master':\n current_jobs = salt.utils.master.get_running_jobs(self.opts)\n else:\n current_jobs = salt.utils.minion.running(self.opts)\n for job in current_jobs:\n if 'schedule' in job:\n log.debug(\n 'schedule.handle_func: Checking job against fun '\n '%s: %s', func, job\n )\n if data['name'] == job['schedule'] \\\n and salt.utils.process.os_is_running(job['pid']):\n jobcount += 1\n log.debug(\n 'schedule.handle_func: Incrementing jobcount, '\n 'now %s, maxrunning is %s',\n jobcount, data['maxrunning']\n )\n if jobcount >= data['maxrunning']:\n log.debug(\n 'schedule.handle_func: The scheduled job '\n '%s was not started, %s already running',\n data['name'], data['maxrunning']\n )\n data['_skip_reason'] = 'maxrunning'\n data['_skipped'] = True\n data['_skipped_time'] = now\n data['run'] = False\n return data\n return data", "response": "Check to see if the job is still running and if so start another job if not"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef persist(self):\n '''\n Persist the modified schedule into <>/<>/_schedule.conf\n '''\n config_dir = self.opts.get('conf_dir', None)\n if config_dir is None and 'conf_file' in self.opts:\n config_dir = os.path.dirname(self.opts['conf_file'])\n if config_dir is None:\n config_dir = salt.syspaths.CONFIG_DIR\n\n minion_d_dir = os.path.join(\n config_dir,\n os.path.dirname(self.opts.get('default_include',\n salt.config.DEFAULT_MINION_OPTS['default_include'])))\n\n if not os.path.isdir(minion_d_dir):\n os.makedirs(minion_d_dir)\n\n schedule_conf = os.path.join(minion_d_dir, '_schedule.conf')\n log.debug('Persisting schedule')\n schedule_data = self._get_schedule(include_pillar=False,\n remove_hidden=True)\n try:\n with salt.utils.files.fopen(schedule_conf, 'wb+') as fp_:\n fp_.write(\n salt.utils.stringutils.to_bytes(\n salt.utils.yaml.safe_dump(\n {'schedule': schedule_data}\n )\n )\n )\n except (IOError, OSError):\n log.error('Failed to persist the updated schedule',\n exc_info_on_loglevel=logging.DEBUG)", "response": "Persist the modified schedule into the _schedule. conf file"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeletes a job from the scheduler. Ignore jobs from pillar", "response": "def delete_job(self, name, persist=True):\n '''\n Deletes a job from the scheduler. Ignore jobs from pillar\n '''\n # ensure job exists, then delete it\n if name in self.opts['schedule']:\n del self.opts['schedule'][name]\n elif name in self._get_schedule(include_opts=False):\n log.warning(\"Cannot delete job %s, it's in the pillar!\", name)\n\n # Fire the complete event back along with updated list of schedule\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True,\n 'schedule': self._get_schedule()},\n tag='/salt/minion/minion_schedule_delete_complete')\n\n # remove from self.intervals\n if name in self.intervals:\n del self.intervals[name]\n\n if persist:\n self.persist()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nresets the scheduler to defaults", "response": "def reset(self):\n '''\n Reset the scheduler to defaults\n '''\n self.skip_function = None\n self.skip_during_range = None\n self.enabled = True\n self.splay = None\n self.opts['schedule'] = {}"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete_job_prefix(self, name, persist=True):\n '''\n Deletes a job from the scheduler. Ignores jobs from pillar\n '''\n # ensure job exists, then delete it\n for job in list(self.opts['schedule'].keys()):\n if job.startswith(name):\n del self.opts['schedule'][job]\n for job in self._get_schedule(include_opts=False):\n if job.startswith(name):\n log.warning(\"Cannot delete job %s, it's in the pillar!\", job)\n\n # Fire the complete event back along with updated list of schedule\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True,\n 'schedule': self._get_schedule()},\n tag='/salt/minion/minion_schedule_delete_complete')\n\n # remove from self.intervals\n for job in list(self.intervals.keys()):\n if job.startswith(name):\n del self.intervals[job]\n\n if persist:\n self.persist()", "response": "Deletes a job from the scheduler. Ignores jobs from pillar\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_job(self, data, persist=True):\n '''\n Adds a new job to the scheduler. The format is the same as required in\n the configuration file. See the docs on how YAML is interpreted into\n python data-structures to make sure, you pass correct dictionaries.\n '''\n\n # we don't do any checking here besides making sure its a dict.\n # eval() already does for us and raises errors accordingly\n if not isinstance(data, dict):\n raise ValueError('Scheduled jobs have to be of type dict.')\n if not len(data) == 1:\n raise ValueError('You can only schedule one new job at a time.')\n\n # if enabled is not included in the job,\n # assume job is enabled.\n for job in data:\n if 'enabled' not in data[job]:\n data[job]['enabled'] = True\n\n new_job = next(six.iterkeys(data))\n\n if new_job in self._get_schedule(include_opts=False):\n log.warning(\"Cannot update job %s, it's in the pillar!\", new_job)\n\n elif new_job in self.opts['schedule']:\n log.info('Updating job settings for scheduled job: %s', new_job)\n self.opts['schedule'].update(data)\n\n else:\n log.info('Added new job %s to scheduler', new_job)\n self.opts['schedule'].update(data)\n\n # Fire the complete event back along with updated list of schedule\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True,\n 'schedule': self._get_schedule()},\n tag='/salt/minion/minion_schedule_add_complete')\n\n if persist:\n self.persist()", "response": "Adds a new job to the scheduler."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nmodifying a job in the scheduler. Ignores jobs from pillar", "response": "def modify_job(self, name, schedule, persist=True):\n '''\n Modify a job in the scheduler. Ignores jobs from pillar\n '''\n # ensure job exists, then replace it\n if name in self.opts['schedule']:\n self.delete_job(name, persist)\n elif name in self._get_schedule(include_opts=False):\n log.warning(\"Cannot modify job %s, it's in the pillar!\", name)\n return\n\n self.opts['schedule'][name] = schedule\n\n if persist:\n self.persist()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef run_job(self, name):\n '''\n Run a schedule job now\n '''\n data = self._get_schedule().get(name, {})\n\n if 'function' in data:\n func = data['function']\n elif 'func' in data:\n func = data['func']\n elif 'fun' in data:\n func = data['fun']\n else:\n func = None\n if not isinstance(func, list):\n func = [func]\n for _func in func:\n if _func not in self.functions:\n log.error(\n 'Invalid function: %s in scheduled job %s.',\n _func, name\n )\n\n if 'name' not in data:\n data['name'] = name\n log.info('Running Job: %s', name)\n\n # Grab run, assume True\n run = data.get('run', True)\n if run:\n self._run_job(_func, data)", "response": "Run a scheduled job now\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreloads the schedule from saved schedule file.", "response": "def reload(self, schedule):\n '''\n Reload the schedule from saved schedule file.\n '''\n # Remove all jobs from self.intervals\n self.intervals = {}\n\n if 'schedule' in schedule:\n schedule = schedule['schedule']\n self.opts.setdefault('schedule', {}).update(schedule)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist the current schedule items", "response": "def list(self, where):\n '''\n List the current schedule items\n '''\n if where == 'pillar':\n schedule = self._get_schedule(include_opts=False)\n elif where == 'opts':\n schedule = self._get_schedule(include_pillar=False)\n else:\n schedule = self._get_schedule()\n\n # Fire the complete event back along with the list of schedule\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True, 'schedule': schedule},\n tag='/salt/minion/minion_schedule_list_complete')"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef save_schedule(self):\n '''\n Save the current schedule\n '''\n self.persist()\n\n # Fire the complete event back along with the list of schedule\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True},\n tag='/salt/minion/minion_schedule_saved')", "response": "Save the current schedule"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef skip_job(self, name, data):\n '''\n Skip a job at a specific time in the scheduler.\n Ignores jobs from pillar\n '''\n time = data['time']\n time_fmt = data.get('time_fmt', '%Y-%m-%dT%H:%M:%S')\n\n # ensure job exists, then disable it\n if name in self.opts['schedule']:\n if 'skip_explicit' not in self.opts['schedule'][name]:\n self.opts['schedule'][name]['skip_explicit'] = []\n self.opts['schedule'][name]['skip_explicit'].append({'time': time,\n 'time_fmt': time_fmt})\n\n elif name in self._get_schedule(include_opts=False):\n log.warning(\"Cannot modify job %s, it's in the pillar!\", name)\n\n # Fire the complete event back along with updated list of schedule\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True,\n 'schedule': self._get_schedule()},\n tag='/salt/minion/minion_schedule_skip_job_complete')", "response": "Skip a job at a specific time in the scheduler."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_next_fire_time(self, name, fmt='%Y-%m-%dT%H:%M:%S'):\n '''\n Return the next fire time for the specified job\n '''\n\n schedule = self._get_schedule()\n _next_fire_time = None\n if schedule:\n _next_fire_time = schedule.get(name, {}).get('_next_fire_time', None)\n if _next_fire_time:\n _next_fire_time = _next_fire_time.strftime(fmt)\n\n # Fire the complete event back along with updated list of schedule\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True, 'next_fire_time': _next_fire_time},\n tag='/salt/minion/minion_schedule_next_fire_time_complete')", "response": "Return the next fire time for the specified job"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef handle_func(self, multiprocessing_enabled, func, data):\n '''\n Execute this method in a multiprocess or thread\n '''\n if salt.utils.platform.is_windows() \\\n or self.opts.get('transport') == 'zeromq':\n # Since function references can't be pickled and pickling\n # is required when spawning new processes on Windows, regenerate\n # the functions and returners.\n # This also needed for ZeroMQ transport to reset all functions\n # context data that could keep paretns connections. ZeroMQ will\n # hang on polling parents connections from the child process.\n if self.opts['__role'] == 'master':\n self.functions = salt.loader.runner(self.opts, utils=self.utils)\n else:\n self.functions = salt.loader.minion_mods(self.opts, proxy=self.proxy, utils=self.utils)\n self.returners = salt.loader.returners(self.opts, self.functions, proxy=self.proxy)\n ret = {'id': self.opts.get('id', 'master'),\n 'fun': func,\n 'fun_args': [],\n 'schedule': data['name'],\n 'jid': salt.utils.jid.gen_jid(self.opts)}\n\n if 'metadata' in data:\n if isinstance(data['metadata'], dict):\n ret['metadata'] = data['metadata']\n ret['metadata']['_TOS'] = self.time_offset\n ret['metadata']['_TS'] = time.ctime()\n ret['metadata']['_TT'] = time.strftime('%Y %B %d %a %H %m', time.gmtime())\n else:\n log.warning('schedule: The metadata parameter must be '\n 'specified as a dictionary. Ignoring.')\n\n if multiprocessing_enabled:\n # We just want to modify the process name if we're on a different process\n salt.utils.process.appendproctitle('{0} {1}'.format(self.__class__.__name__, ret['jid']))\n data_returner = data.get('returner', None)\n\n if not self.standalone:\n proc_fn = os.path.join(\n salt.minion.get_proc_dir(self.opts['cachedir']),\n ret['jid']\n )\n\n if multiprocessing_enabled and not salt.utils.platform.is_windows():\n # Reconfigure multiprocessing logging after daemonizing\n log_setup.setup_multiprocessing_logging()\n\n if multiprocessing_enabled:\n # Don't *BEFORE* to go into try to don't let it triple execute the finally section.\n salt.utils.process.daemonize_if(self.opts)\n\n # TODO: Make it readable! Splt to funcs, remove nested try-except-finally sections.\n try:\n\n minion_blackout_violation = False\n if self.opts.get('pillar', {}).get('minion_blackout', False):\n whitelist = self.opts.get('pillar', {}).get('minion_blackout_whitelist', [])\n # this minion is blacked out. Only allow saltutil.refresh_pillar and the whitelist\n if func != 'saltutil.refresh_pillar' and func not in whitelist:\n minion_blackout_violation = True\n elif self.opts.get('grains', {}).get('minion_blackout', False):\n whitelist = self.opts.get('grains', {}).get('minion_blackout_whitelist', [])\n if func != 'saltutil.refresh_pillar' and func not in whitelist:\n minion_blackout_violation = True\n if minion_blackout_violation:\n raise SaltInvocationError('Minion in blackout mode. Set \\'minion_blackout\\' '\n 'to False in pillar or grains to resume operations. Only '\n 'saltutil.refresh_pillar allowed in blackout mode.')\n\n ret['pid'] = os.getpid()\n\n if not self.standalone:\n if 'jid_include' not in data or data['jid_include']:\n log.debug(\n 'schedule.handle_func: adding this job to the '\n 'jobcache with data %s', ret\n )\n # write this to /var/cache/salt/minion/proc\n with salt.utils.files.fopen(proc_fn, 'w+b') as fp_:\n fp_.write(salt.payload.Serial(self.opts).dumps(ret))\n\n args = tuple()\n if 'args' in data:\n args = data['args']\n ret['fun_args'].extend(data['args'])\n\n kwargs = {}\n if 'kwargs' in data:\n kwargs = data['kwargs']\n ret['fun_args'].append(copy.deepcopy(kwargs))\n\n if func not in self.functions:\n ret['return'] = self.functions.missing_fun_string(func)\n salt.utils.error.raise_error(\n message=self.functions.missing_fun_string(func))\n\n # if the func support **kwargs, lets pack in the pub data we have\n # TODO: pack the *same* pub data as a minion?\n argspec = salt.utils.args.get_function_argspec(self.functions[func])\n if argspec.keywords:\n # this function accepts **kwargs, pack in the publish data\n for key, val in six.iteritems(ret):\n if key is not 'kwargs':\n kwargs['__pub_{0}'.format(key)] = copy.deepcopy(val)\n\n # Only include these when running runner modules\n if self.opts['__role'] == 'master':\n jid = salt.utils.jid.gen_jid(self.opts)\n tag = salt.utils.event.tagify(jid, prefix='salt/scheduler/')\n\n event = salt.utils.event.get_event(\n self.opts['__role'],\n self.opts['sock_dir'],\n self.opts['transport'],\n opts=self.opts,\n listen=False)\n\n namespaced_event = salt.utils.event.NamespacedEvent(\n event,\n tag,\n print_func=None\n )\n\n func_globals = {\n '__jid__': jid,\n '__user__': salt.utils.user.get_user(),\n '__tag__': tag,\n '__jid_event__': weakref.proxy(namespaced_event),\n }\n self_functions = copy.copy(self.functions)\n salt.utils.lazy.verify_fun(self_functions, func)\n\n # Inject some useful globals to *all* the function's global\n # namespace only once per module-- not per func\n completed_funcs = []\n\n for mod_name in six.iterkeys(self_functions):\n if '.' not in mod_name:\n continue\n mod, _ = mod_name.split('.', 1)\n if mod in completed_funcs:\n continue\n completed_funcs.append(mod)\n for global_key, value in six.iteritems(func_globals):\n self.functions[mod_name].__globals__[global_key] = value\n\n self.functions.pack['__context__']['retcode'] = 0\n\n ret['return'] = self.functions[func](*args, **kwargs)\n\n if not self.standalone:\n # runners do not provide retcode\n if 'retcode' in self.functions.pack['__context__']:\n ret['retcode'] = self.functions.pack['__context__']['retcode']\n\n ret['success'] = True\n\n if data_returner or self.schedule_returner:\n if 'return_config' in data:\n ret['ret_config'] = data['return_config']\n if 'return_kwargs' in data:\n ret['ret_kwargs'] = data['return_kwargs']\n rets = []\n for returner in [data_returner, self.schedule_returner]:\n if isinstance(returner, six.string_types):\n rets.append(returner)\n elif isinstance(returner, list):\n rets.extend(returner)\n # simple de-duplication with order retained\n for returner in OrderedDict.fromkeys(rets):\n ret_str = '{0}.returner'.format(returner)\n if ret_str in self.returners:\n self.returners[ret_str](ret)\n else:\n log.info(\n 'Job %s using invalid returner: %s. Ignoring.',\n func, returner\n )\n\n except Exception:\n log.exception('Unhandled exception running %s', ret['fun'])\n # Although catch-all exception handlers are bad, the exception here\n # is to let the exception bubble up to the top of the thread context,\n # where the thread will die silently, which is worse.\n if 'return' not in ret:\n ret['return'] = \"Unhandled exception running {0}\".format(ret['fun'])\n ret['success'] = False\n ret['retcode'] = 254\n finally:\n # Only attempt to return data to the master if the scheduled job is running\n # on a master itself or a minion.\n if '__role' in self.opts and self.opts['__role'] in ('master', 'minion'):\n # The 'return_job' option is enabled by default even if not set\n if 'return_job' in data and not data['return_job']:\n pass\n else:\n # Send back to master so the job is included in the job list\n mret = ret.copy()\n # No returners defined, so we're only sending back to the master\n if not data_returner and not self.schedule_returner:\n mret['jid'] = 'req'\n if data.get('return_job') == 'nocache':\n # overwrite 'req' to signal to master that\n # this job shouldn't be stored\n mret['jid'] = 'nocache'\n load = {'cmd': '_return', 'id': self.opts['id']}\n for key, value in six.iteritems(mret):\n load[key] = value\n\n if '__role' in self.opts and self.opts['__role'] == 'minion':\n event = salt.utils.event.get_event('minion',\n opts=self.opts,\n listen=False)\n elif '__role' in self.opts and self.opts['__role'] == 'master':\n event = salt.utils.event.get_master_event(self.opts,\n self.opts['sock_dir'])\n try:\n event.fire_event(load, '__schedule_return')\n except Exception as exc:\n log.exception('Unhandled exception firing __schedule_return event')\n\n if not self.standalone:\n log.debug('schedule.handle_func: Removing %s', proc_fn)\n\n try:\n os.unlink(proc_fn)\n except OSError as exc:\n if exc.errno == errno.EEXIST or exc.errno == errno.ENOENT:\n # EEXIST and ENOENT are OK because the file is gone and that's what\n # we wanted\n pass\n else:\n log.error(\"Failed to delete '%s': %s\", proc_fn, exc.errno)\n # Otherwise, failing to delete this file is not something\n # we can cleanly handle.\n raise\n finally:\n if multiprocessing_enabled:\n # Let's make sure we exit the process!\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)", "response": "Execute this method in a multiprocess or thread."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nevaluates and execute the schedule.", "response": "def eval(self, now=None):\n '''\n Evaluate and execute the schedule\n\n :param datetime now: Override current time with a datetime object instance``\n\n '''\n\n log.trace('==== evaluating schedule now %s =====', now)\n\n loop_interval = self.opts['loop_interval']\n if not isinstance(loop_interval, datetime.timedelta):\n loop_interval = datetime.timedelta(seconds=loop_interval)\n\n def _splay(splaytime):\n '''\n Calculate splaytime\n '''\n splay_ = None\n if isinstance(splaytime, dict):\n if splaytime['end'] >= splaytime['start']:\n splay_ = random.randint(splaytime['start'],\n splaytime['end'])\n else:\n log.error('schedule.handle_func: Invalid Splay, '\n 'end must be larger than start. Ignoring splay.')\n else:\n splay_ = random.randint(1, splaytime)\n return splay_\n\n def _handle_time_elements(data):\n '''\n Handle schedule item with time elements\n seconds, minutes, hours, days\n '''\n if '_seconds' not in data:\n interval = int(data.get('seconds', 0))\n interval += int(data.get('minutes', 0)) * 60\n interval += int(data.get('hours', 0)) * 3600\n interval += int(data.get('days', 0)) * 86400\n\n data['_seconds'] = interval\n\n if not data['_next_fire_time']:\n data['_next_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])\n\n if interval < self.loop_interval:\n self.loop_interval = interval\n\n data['_next_scheduled_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])\n\n def _handle_once(data, loop_interval):\n '''\n Handle schedule item with once\n '''\n if data['_next_fire_time']:\n if data['_next_fire_time'] < now - loop_interval or \\\n data['_next_fire_time'] > now and \\\n not data['_splay']:\n data['_continue'] = True\n\n if not data['_next_fire_time'] and \\\n not data['_splay']:\n once = data['once']\n if not isinstance(once, datetime.datetime):\n once_fmt = data.get('once_fmt', '%Y-%m-%dT%H:%M:%S')\n try:\n once = datetime.datetime.strptime(data['once'],\n once_fmt)\n except (TypeError, ValueError):\n data['_error'] = ('Date string could not '\n 'be parsed: {0}, {1}. '\n 'Ignoring job {2}.'.format(\n data['once'],\n once_fmt,\n data['name']))\n log.error(data['_error'])\n return\n data['_next_fire_time'] = once\n data['_next_scheduled_fire_time'] = once\n # If _next_fire_time is less than now, continue\n if once < now - loop_interval:\n data['_continue'] = True\n\n def _handle_when(data, loop_interval):\n '''\n Handle schedule item with when\n '''\n if not _WHEN_SUPPORTED:\n data['_error'] = ('Missing python-dateutil. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n if not isinstance(data['when'], list):\n _when_data = [data['when']]\n else:\n _when_data = data['when']\n\n _when = []\n for i in _when_data:\n if ('pillar' in self.opts and 'whens' in self.opts['pillar'] and\n i in self.opts['pillar']['whens']):\n if not isinstance(self.opts['pillar']['whens'],\n dict):\n data['_error'] = ('Pillar item \"whens\" '\n 'must be a dict. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n when_ = self.opts['pillar']['whens'][i]\n elif ('whens' in self.opts['grains'] and\n i in self.opts['grains']['whens']):\n if not isinstance(self.opts['grains']['whens'],\n dict):\n data['_error'] = ('Grain \"whens\" must be a dict. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n when_ = self.opts['grains']['whens'][i]\n else:\n when_ = i\n\n if not isinstance(when_, datetime.datetime):\n try:\n when_ = dateutil_parser.parse(when_)\n except ValueError:\n data['_error'] = ('Invalid date string {0}. '\n 'Ignoring job {1}.'.format(i, data['name']))\n log.error(data['_error'])\n return\n\n _when.append(when_)\n\n if data['_splay']:\n _when.append(data['_splay'])\n\n # Sort the list of \"whens\" from earlier to later schedules\n _when.sort()\n\n # Copy the list so we can loop through it\n for i in copy.deepcopy(_when):\n if len(_when) > 1:\n if i < now - loop_interval:\n # Remove all missed schedules except the latest one.\n # We need it to detect if it was triggered previously.\n _when.remove(i)\n\n if _when:\n # Grab the first element, which is the next run time or\n # last scheduled time in the past.\n when = _when[0]\n\n if when < now - loop_interval and \\\n not data.get('_run', False) and \\\n not data.get('run', False) and \\\n not data['_splay']:\n data['_next_fire_time'] = None\n data['_continue'] = True\n return\n\n if '_run' not in data:\n # Prevent run of jobs from the past\n data['_run'] = bool(when >= now - loop_interval)\n\n if not data['_next_fire_time']:\n data['_next_fire_time'] = when\n\n data['_next_scheduled_fire_time'] = when\n\n if data['_next_fire_time'] < when and \\\n not run and \\\n not data['_run']:\n data['_next_fire_time'] = when\n data['_run'] = True\n\n elif not data.get('_run', False):\n data['_next_fire_time'] = None\n data['_continue'] = True\n\n def _handle_cron(data, loop_interval):\n '''\n Handle schedule item with cron\n '''\n if not _CRON_SUPPORTED:\n data['_error'] = ('Missing python-croniter. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n if data['_next_fire_time'] is None:\n # Get next time frame for a \"cron\" job if it has been never\n # executed before or already executed in the past.\n try:\n data['_next_fire_time'] = croniter.croniter(data['cron'], now).get_next(datetime.datetime)\n data['_next_scheduled_fire_time'] = croniter.croniter(data['cron'], now).get_next(datetime.datetime)\n except (ValueError, KeyError):\n data['_error'] = ('Invalid cron string. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n # If next job run is scheduled more than 1 minute ahead and\n # configured loop interval is longer than that, we should\n # shorten it to get our job executed closer to the beginning\n # of desired time.\n interval = (now - data['_next_fire_time']).total_seconds()\n if interval >= 60 and interval < self.loop_interval:\n self.loop_interval = interval\n\n def _handle_run_explicit(data, loop_interval):\n '''\n Handle schedule item with run_explicit\n '''\n _run_explicit = []\n for _run_time in data['run_explicit']:\n if isinstance(_run_time, datetime.datetime):\n _run_explicit.append(_run_time)\n else:\n _run_explicit.append(datetime.datetime.strptime(_run_time['time'],\n _run_time['time_fmt']))\n data['run'] = False\n\n # Copy the list so we can loop through it\n for i in copy.deepcopy(_run_explicit):\n if len(_run_explicit) > 1:\n if i < now - loop_interval:\n _run_explicit.remove(i)\n\n if _run_explicit:\n if _run_explicit[0] <= now < _run_explicit[0] + loop_interval:\n data['run'] = True\n data['_next_fire_time'] = _run_explicit[0]\n\n def _handle_skip_explicit(data, loop_interval):\n '''\n Handle schedule item with skip_explicit\n '''\n data['run'] = False\n\n _skip_explicit = []\n for _skip_time in data['skip_explicit']:\n if isinstance(_skip_time, datetime.datetime):\n _skip_explicit.append(_skip_time)\n else:\n _skip_explicit.append(datetime.datetime.strptime(_skip_time['time'],\n _skip_time['time_fmt']))\n\n # Copy the list so we can loop through it\n for i in copy.deepcopy(_skip_explicit):\n if i < now - loop_interval:\n _skip_explicit.remove(i)\n\n if _skip_explicit:\n if _skip_explicit[0] <= now <= (_skip_explicit[0] + loop_interval):\n if self.skip_function:\n data['run'] = True\n data['func'] = self.skip_function\n else:\n data['_skip_reason'] = 'skip_explicit'\n data['_skipped_time'] = now\n data['_skipped'] = True\n data['run'] = False\n else:\n data['run'] = True\n\n def _handle_skip_during_range(data, loop_interval):\n '''\n Handle schedule item with skip_explicit\n '''\n if not _RANGE_SUPPORTED:\n data['_error'] = ('Missing python-dateutil. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n if not isinstance(data['skip_during_range'], dict):\n data['_error'] = ('schedule.handle_func: Invalid, range '\n 'must be specified as a dictionary. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n start = data['skip_during_range']['start']\n end = data['skip_during_range']['end']\n if not isinstance(start, datetime.datetime):\n try:\n start = dateutil_parser.parse(start)\n except ValueError:\n data['_error'] = ('Invalid date string for start in '\n 'skip_during_range. Ignoring '\n 'job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n if not isinstance(end, datetime.datetime):\n try:\n end = dateutil_parser.parse(end)\n except ValueError:\n data['_error'] = ('Invalid date string for end in '\n 'skip_during_range. Ignoring '\n 'job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n # Check to see if we should run the job immediately\n # after the skip_during_range is over\n if 'run_after_skip_range' in data and \\\n data['run_after_skip_range']:\n if 'run_explicit' not in data:\n data['run_explicit'] = []\n # Add a run_explicit for immediately after the\n # skip_during_range ends\n _run_immediate = (end + loop_interval).strftime('%Y-%m-%dT%H:%M:%S')\n if _run_immediate not in data['run_explicit']:\n data['run_explicit'].append({'time': _run_immediate,\n 'time_fmt': '%Y-%m-%dT%H:%M:%S'})\n\n if end > start:\n if start <= now <= end:\n if self.skip_function:\n data['run'] = True\n data['func'] = self.skip_function\n else:\n data['_skip_reason'] = 'in_skip_range'\n data['_skipped_time'] = now\n data['_skipped'] = True\n data['run'] = False\n else:\n data['run'] = True\n else:\n data['_error'] = ('schedule.handle_func: Invalid '\n 'range, end must be larger than '\n 'start. Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n\n def _handle_range(data):\n '''\n Handle schedule item with skip_explicit\n '''\n if not _RANGE_SUPPORTED:\n data['_error'] = ('Missing python-dateutil. '\n 'Ignoring job {0}'.format(data['name']))\n log.error(data['_error'])\n return\n\n if not isinstance(data['range'], dict):\n data['_error'] = ('schedule.handle_func: Invalid, range '\n 'must be specified as a dictionary.'\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n start = data['range']['start']\n end = data['range']['end']\n if not isinstance(start, datetime.datetime):\n try:\n start = dateutil_parser.parse(start)\n except ValueError:\n data['_error'] = ('Invalid date string for start. '\n 'Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n if not isinstance(end, datetime.datetime):\n try:\n end = dateutil_parser.parse(end)\n except ValueError:\n data['_error'] = ('Invalid date string for end.'\n ' Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n return\n\n if end > start:\n if 'invert' in data['range'] and data['range']['invert']:\n if now <= start or now >= end:\n data['run'] = True\n else:\n data['_skip_reason'] = 'in_skip_range'\n data['run'] = False\n else:\n if start <= now <= end:\n data['run'] = True\n else:\n if self.skip_function:\n data['run'] = True\n data['func'] = self.skip_function\n else:\n data['_skip_reason'] = 'not_in_range'\n data['run'] = False\n else:\n data['_error'] = ('schedule.handle_func: Invalid '\n 'range, end must be larger '\n 'than start. Ignoring job {0}.'.format(data['name']))\n log.error(data['_error'])\n\n def _handle_after(data):\n '''\n Handle schedule item with after\n '''\n if not _WHEN_SUPPORTED:\n data['_error'] = ('Missing python-dateutil. '\n 'Ignoring job {0}'.format(data['name']))\n log.error(data['_error'])\n return\n\n after = data['after']\n if not isinstance(after, datetime.datetime):\n after = dateutil_parser.parse(after)\n\n if after >= now:\n log.debug(\n 'After time has not passed skipping job: %s.',\n data['name']\n )\n data['_skip_reason'] = 'after_not_passed'\n data['_skipped_time'] = now\n data['_skipped'] = True\n data['run'] = False\n else:\n data['run'] = True\n\n def _handle_until(data):\n '''\n Handle schedule item with until\n '''\n if not _WHEN_SUPPORTED:\n data['_error'] = ('Missing python-dateutil. '\n 'Ignoring job {0}'.format(data['name']))\n log.error(data['_error'])\n return\n\n until = data['until']\n if not isinstance(until, datetime.datetime):\n until = dateutil_parser.parse(until)\n\n if until <= now:\n log.debug(\n 'Until time has passed skipping job: %s.',\n data['name']\n )\n data['_skip_reason'] = 'until_passed'\n data['_skipped_time'] = now\n data['_skipped'] = True\n data['run'] = False\n else:\n data['run'] = True\n\n def _chop_ms(dt):\n '''\n Remove the microseconds from a datetime object\n '''\n return dt - datetime.timedelta(microseconds=dt.microsecond)\n\n schedule = self._get_schedule()\n if not isinstance(schedule, dict):\n raise ValueError('Schedule must be of type dict.')\n if 'skip_function' in schedule:\n self.skip_function = schedule['skip_function']\n if 'skip_during_range' in schedule:\n self.skip_during_range = schedule['skip_during_range']\n if 'enabled' in schedule:\n self.enabled = schedule['enabled']\n if 'splay' in schedule:\n self.splay = schedule['splay']\n\n _hidden = ['enabled',\n 'skip_function',\n 'skip_during_range',\n 'splay']\n for job, data in six.iteritems(schedule):\n\n # Skip anything that is a global setting\n if job in _hidden:\n continue\n\n # Clear these out between runs\n for item in ['_continue',\n '_error',\n '_enabled',\n '_skipped',\n '_skip_reason',\n '_skipped_time']:\n if item in data:\n del data[item]\n run = False\n\n if 'name' in data:\n job_name = data['name']\n else:\n job_name = data['name'] = job\n\n if not isinstance(data, dict):\n log.error(\n 'Scheduled job \"%s\" should have a dict value, not %s',\n job_name, type(data)\n )\n continue\n if 'function' in data:\n func = data['function']\n elif 'func' in data:\n func = data['func']\n elif 'fun' in data:\n func = data['fun']\n else:\n func = None\n if not isinstance(func, list):\n func = [func]\n for _func in func:\n if _func not in self.functions:\n log.info(\n 'Invalid function: %s in scheduled job %s.',\n _func, job_name\n )\n\n if '_next_fire_time' not in data:\n data['_next_fire_time'] = None\n\n if '_splay' not in data:\n data['_splay'] = None\n\n if 'run_on_start' in data and \\\n data['run_on_start'] and \\\n '_run_on_start' not in data:\n data['_run_on_start'] = True\n\n if not now:\n now = datetime.datetime.now()\n\n # Used for quick lookups when detecting invalid option\n # combinations.\n schedule_keys = set(data.keys())\n\n time_elements = ('seconds', 'minutes', 'hours', 'days')\n scheduling_elements = ('when', 'cron', 'once')\n\n invalid_sched_combos = [\n set(i) for i in itertools.combinations(scheduling_elements, 2)\n ]\n\n if any(i <= schedule_keys for i in invalid_sched_combos):\n log.error(\n 'Unable to use \"%s\" options together. Ignoring.',\n '\", \"'.join(scheduling_elements)\n )\n continue\n\n invalid_time_combos = []\n for item in scheduling_elements:\n all_items = itertools.chain([item], time_elements)\n invalid_time_combos.append(\n set(itertools.combinations(all_items, 2)))\n\n if any(set(x) <= schedule_keys for x in invalid_time_combos):\n log.error(\n 'Unable to use \"%s\" with \"%s\" options. Ignoring',\n '\", \"'.join(time_elements),\n '\", \"'.join(scheduling_elements)\n )\n continue\n\n if 'run_explicit' in data:\n _handle_run_explicit(data, loop_interval)\n run = data['run']\n\n if True in [True for item in time_elements if item in data]:\n _handle_time_elements(data)\n elif 'once' in data:\n _handle_once(data, loop_interval)\n elif 'when' in data:\n _handle_when(data, loop_interval)\n elif 'cron' in data:\n _handle_cron(data, loop_interval)\n else:\n continue\n\n # Something told us to continue, so we continue\n if '_continue' in data and data['_continue']:\n continue\n\n # An error occurred so we bail out\n if '_error' in data and data['_error']:\n continue\n\n seconds = int((_chop_ms(data['_next_fire_time']) - _chop_ms(now)).total_seconds())\n\n # If there is no job specific splay available,\n # grab the global which defaults to None.\n if 'splay' not in data:\n data['splay'] = self.splay\n\n if 'splay' in data and data['splay']:\n # Got \"splay\" configured, make decision to run a job based on that\n if not data['_splay']:\n # Try to add \"splay\" time only if next job fire time is\n # still in the future. We should trigger job run\n # immediately otherwise.\n splay = _splay(data['splay'])\n if now < data['_next_fire_time'] + datetime.timedelta(seconds=splay):\n log.debug('schedule.handle_func: Adding splay of '\n '%s seconds to next run.', splay)\n data['_splay'] = data['_next_fire_time'] + datetime.timedelta(seconds=splay)\n if 'when' in data:\n data['_run'] = True\n else:\n run = True\n\n if data['_splay']:\n # The \"splay\" configuration has been already processed, just use it\n seconds = (data['_splay'] - now).total_seconds()\n if 'when' in data:\n data['_next_fire_time'] = data['_splay']\n\n if '_seconds' in data:\n if seconds <= 0:\n run = True\n elif 'when' in data and data['_run']:\n if data['_next_fire_time'] <= now <= (data['_next_fire_time'] + loop_interval):\n data['_run'] = False\n run = True\n elif 'cron' in data:\n # Reset next scheduled time because it is in the past now,\n # and we should trigger the job run, then wait for the next one.\n if seconds <= 0:\n data['_next_fire_time'] = None\n run = True\n elif 'once' in data:\n if data['_next_fire_time'] <= now <= (data['_next_fire_time'] + loop_interval):\n run = True\n elif seconds == 0:\n run = True\n\n if '_run_on_start' in data and data['_run_on_start']:\n run = True\n data['_run_on_start'] = False\n elif run:\n if 'range' in data:\n _handle_range(data)\n\n # An error occurred so we bail out\n if '_error' in data and data['_error']:\n continue\n\n run = data['run']\n # Override the functiton if passed back\n if 'func' in data:\n func = data['func']\n\n # If there is no job specific skip_during_range available,\n # grab the global which defaults to None.\n if 'skip_during_range' not in data and self.skip_during_range:\n data['skip_during_range'] = self.skip_during_range\n\n if 'skip_during_range' in data and data['skip_during_range']:\n _handle_skip_during_range(data, loop_interval)\n\n # An error occurred so we bail out\n if '_error' in data and data['_error']:\n continue\n\n run = data['run']\n # Override the functiton if passed back\n if 'func' in data:\n func = data['func']\n\n if 'skip_explicit' in data:\n _handle_skip_explicit(data, loop_interval)\n\n # An error occurred so we bail out\n if '_error' in data and data['_error']:\n continue\n\n run = data['run']\n # Override the functiton if passed back\n if 'func' in data:\n func = data['func']\n\n if 'until' in data:\n _handle_until(data)\n\n # An error occurred so we bail out\n if '_error' in data and data['_error']:\n continue\n\n run = data['run']\n\n if 'after' in data:\n _handle_after(data)\n\n # An error occurred so we bail out\n if '_error' in data and data['_error']:\n continue\n\n run = data['run']\n\n # If args is a list and less than the number of functions\n # run is set to False.\n if 'args' in data and isinstance(data['args'], list):\n if len(data['args']) < len(func):\n data['_error'] = ('Number of arguments is less than '\n 'the number of functions. Ignoring job.')\n log.error(data['_error'])\n run = False\n\n # If the job item has continue, then we set run to False\n # so the job does not run but we still get the important\n # information calculated, eg. _next_fire_time\n if '_continue' in data and data['_continue']:\n run = False\n\n # If there is no job specific enabled available,\n # grab the global which defaults to True.\n if 'enabled' not in data:\n data['enabled'] = self.enabled\n\n # If globally disabled, disable the job\n if not self.enabled:\n data['enabled'] = self.enabled\n data['_skip_reason'] = 'disabled'\n data['_skipped_time'] = now\n data['_skipped'] = True\n run = False\n\n # Job is disabled, set run to False\n if 'enabled' in data and not data['enabled']:\n data['_enabled'] = False\n data['_skip_reason'] = 'disabled'\n data['_skipped_time'] = now\n data['_skipped'] = True\n run = False\n\n miss_msg = ''\n if seconds < 0:\n miss_msg = ' (runtime missed ' \\\n 'by {0} seconds)'.format(abs(seconds))\n\n try:\n if run:\n # Job is disabled, continue\n if 'enabled' in data and not data['enabled']:\n log.debug('Job: %s is disabled', job_name)\n data['_skip_reason'] = 'disabled'\n data['_skipped_time'] = now\n data['_skipped'] = True\n continue\n\n if 'jid_include' not in data or data['jid_include']:\n data['jid_include'] = True\n log.debug('schedule: Job %s was scheduled with jid_include, '\n 'adding to cache (jid_include defaults to True)',\n job_name)\n if 'maxrunning' in data:\n log.debug('schedule: Job %s was scheduled with a max '\n 'number of %s', job_name, data['maxrunning'])\n else:\n log.info('schedule: maxrunning parameter was not specified for '\n 'job %s, defaulting to 1.', job_name)\n data['maxrunning'] = 1\n\n if not self.standalone:\n data['run'] = run\n data = self._check_max_running(func,\n data,\n self.opts,\n now)\n run = data['run']\n\n # Check run again, just in case _check_max_running\n # set run to False\n if run:\n log.info('Running scheduled job: %s%s', job_name, miss_msg)\n self._run_job(func, data)\n\n finally:\n # Only set _last_run if the job ran\n if run:\n data['_last_run'] = now\n data['_splay'] = None\n if '_seconds' in data:\n if self.standalone:\n data['_next_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])\n elif '_skipped' in data and data['_skipped']:\n data['_next_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])\n elif run:\n data['_next_fire_time'] = now + datetime.timedelta(seconds=data['_seconds'])"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrunning the given command against AWS and returns the output as a json string", "response": "def _run_aws(cmd, region, opts, user, **kwargs):\n '''\n Runs the given command against AWS.\n cmd\n Command to run\n region\n Region to execute cmd in\n opts\n Pass in from salt\n user\n Pass in from salt\n kwargs\n Key-value arguments to pass to the command\n '''\n # These args need a specific key value that aren't\n # valid python parameter keys\n receipthandle = kwargs.pop('receipthandle', None)\n if receipthandle:\n kwargs['receipt-handle'] = receipthandle\n num = kwargs.pop('num', None)\n if num:\n kwargs['max-number-of-messages'] = num\n\n _formatted_args = [\n '--{0} \"{1}\"'.format(k, v) for k, v in six.iteritems(kwargs)]\n\n cmd = 'aws sqs {cmd} {args} {region} {out}'.format(\n cmd=cmd,\n args=' '.join(_formatted_args),\n region=_region(region),\n out=_OUTPUT)\n\n rtn = __salt__['cmd.run'](cmd, runas=user, python_shell=False)\n\n return salt.utils.json.loads(rtn) if rtn else ''"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef receive_message(queue, region, num=1, opts=None, user=None):\n '''\n Receive one or more messages from a queue in a region\n\n queue\n The name of the queue to receive messages from\n\n region\n Region where SQS queues exists\n\n num : 1\n The max number of messages to receive\n\n opts : None\n Any additional options to add to the command line\n\n user : None\n Run as a user other than what the minion runs as\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aws_sqs.receive_message \n salt '*' aws_sqs.receive_message num=10\n\n .. versionadded:: 2014.7.0\n\n '''\n ret = {\n 'Messages': None,\n }\n queues = list_queues(region, opts, user)\n url_map = _parse_queue_list(queues)\n if queue not in url_map:\n log.info('\"%s\" queue does not exist.', queue)\n return ret\n\n out = _run_aws('receive-message', region, opts, user, queue=url_map[queue],\n num=num)\n ret['Messages'] = out['Messages']\n return ret", "response": "Receive one or more messages from a queue in a region"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndeletes one or more messages from a queue in a region", "response": "def delete_message(queue, region, receipthandle, opts=None, user=None):\n '''\n Delete one or more messages from a queue in a region\n\n queue\n The name of the queue to delete messages from\n\n region\n Region where SQS queues exists\n\n receipthandle\n The ReceiptHandle of the message to delete. The ReceiptHandle\n is obtained in the return from receive_message\n\n opts : None\n Any additional options to add to the command line\n\n user : None\n Run as a user other than what the minion runs as\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aws_sqs.delete_message receipthandle=''\n\n .. versionadded:: 2014.7.0\n\n '''\n queues = list_queues(region, opts, user)\n url_map = _parse_queue_list(queues)\n if queue not in url_map:\n log.info('\"%s\" queue does not exist.', queue)\n return False\n\n out = _run_aws('delete-message', region, opts, user,\n receipthandle=receipthandle, queue=url_map[queue],)\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlist the SQS queues in the specified region", "response": "def list_queues(region, opts=None, user=None):\n '''\n List the queues in the selected region.\n\n region\n Region to list SQS queues for\n\n opts : None\n Any additional options to add to the command line\n\n user : None\n Run hg as a user other than what the minion runs as\n\n CLI Example:\n\n salt '*' aws_sqs.list_queues \n\n '''\n out = _run_aws('list-queues', region, opts, user)\n\n ret = {\n 'retcode': 0,\n 'stdout': out['QueueUrls'],\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a new SQS queue with the correct name and region", "response": "def create_queue(name, region, opts=None, user=None):\n '''\n Creates a queue with the correct name.\n\n name\n Name of the SQS queue to create\n\n region\n Region to create the SQS queue in\n\n opts : None\n Any additional options to add to the command line\n\n user : None\n Run hg as a user other than what the minion runs as\n\n CLI Example:\n\n salt '*' aws_sqs.create_queue \n\n '''\n\n create = {'queue-name': name}\n out = _run_aws(\n 'create-queue', region=region, opts=opts,\n user=user, **create)\n\n ret = {\n 'retcode': 0,\n 'stdout': out['QueueUrl'],\n 'stderr': '',\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete_queue(name, region, opts=None, user=None):\n '''\n Deletes a queue in the region.\n\n name\n Name of the SQS queue to deletes\n region\n Name of the region to delete the queue from\n\n opts : None\n Any additional options to add to the command line\n\n user : None\n Run hg as a user other than what the minion runs as\n\n CLI Example:\n\n salt '*' aws_sqs.delete_queue \n\n '''\n queues = list_queues(region, opts, user)\n url_map = _parse_queue_list(queues)\n\n log.debug('map %s', url_map)\n if name in url_map:\n delete = {'queue-url': url_map[name]}\n\n rtn = _run_aws(\n 'delete-queue',\n region=region,\n opts=opts,\n user=user,\n **delete)\n success = True\n err = ''\n out = '{0} deleted'.format(name)\n\n else:\n out = ''\n err = \"Delete failed\"\n success = False\n\n ret = {\n 'retcode': 0 if success else 1,\n 'stdout': out,\n 'stderr': err,\n }\n return ret", "response": "Delete a SQS queue in a specific region"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef queue_exists(name, region, opts=None, user=None):\n '''\n Returns True or False on whether the queue exists in the region\n\n name\n Name of the SQS queue to search for\n\n region\n Name of the region to search for the queue in\n\n opts : None\n Any additional options to add to the command line\n\n user : None\n Run hg as a user other than what the minion runs as\n\n CLI Example:\n\n salt '*' aws_sqs.queue_exists \n\n '''\n output = list_queues(region, opts, user)\n\n return name in _parse_queue_list(output)", "response": "Returns True or False on whether the queue exists in the region"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses the queue list to get a dict of name -> URL", "response": "def _parse_queue_list(list_output):\n '''\n Parse the queue to get a dict of name -> URL\n '''\n queues = dict((q.split('/')[-1], q) for q in list_output['stdout'])\n return queues"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a connection to the XenServer host", "response": "def _get_session():\n '''\n Get a connection to the XenServer host\n '''\n api_version = '1.0'\n originator = 'salt_cloud_{}_driver'.format(__virtualname__)\n url = config.get_cloud_config_value(\n 'url',\n get_configured_provider(),\n __opts__,\n search_global=False\n )\n user = config.get_cloud_config_value(\n 'user',\n get_configured_provider(),\n __opts__,\n search_global=False\n )\n password = config.get_cloud_config_value(\n 'password',\n get_configured_provider(),\n __opts__,\n search_global=False\n )\n ignore_ssl = config.get_cloud_config_value(\n 'ignore_ssl',\n get_configured_provider(),\n __opts__,\n default=False,\n search_global=False\n )\n try:\n session = XenAPI.Session(url, ignore_ssl=ignore_ssl)\n log.debug(\n 'url: %s user: %s password: %s, originator: %s',\n url, user, 'XXX-pw-redacted-XXX', originator\n )\n session.xenapi.login_with_password(\n user, password, api_version, originator)\n except XenAPI.Failure as ex:\n pool_master_addr = six.text_type(ex.__dict__['details'][1])\n slash_parts = url.split('/')\n new_url = '/'.join(slash_parts[:2]) + '/' + pool_master_addr\n session = XenAPI.Session(new_url)\n log.debug(\n 'session is -> url: %s user: %s password: %s, originator:%s',\n new_url, user, 'XXX-pw-redacted-XXX', originator\n )\n session.xenapi.login_with_password(\n user, password, api_version, originator)\n return session"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlist virtual machines in a single virtual machine.", "response": "def list_nodes():\n '''\n List virtual machines\n\n .. code-block:: bash\n\n salt-cloud -Q\n\n '''\n session = _get_session()\n vms = session.xenapi.VM.get_all_records()\n ret = {}\n for vm in vms:\n record = session.xenapi.VM.get_record(vm)\n if not record['is_a_template'] and not record['is_control_domain']:\n try:\n base_template_name = record['other_config']['base_template_name']\n except Exception:\n base_template_name = None\n log.debug(\n 'VM %s, doesnt have base_template_name attribute',\n record['name_label']\n )\n ret[record['name_label']] = {'id': record['uuid'],\n 'image': base_template_name,\n 'name': record['name_label'],\n 'size': record['memory_dynamic_max'],\n 'state': record['power_state'],\n 'private_ips': get_vm_ip(record['name_label'], session),\n 'public_ips': None}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the IP address of the VM", "response": "def get_vm_ip(name=None, session=None, call=None):\n '''\n Get the IP address of the VM\n\n .. code-block:: bash\n\n salt-cloud -a get_vm_ip xenvm01\n\n .. note:: Requires xen guest tools to be installed in VM\n\n '''\n if call == 'function':\n raise SaltCloudException(\n 'This function must be called with -a or --action.'\n )\n if session is None:\n log.debug('New session being created')\n session = _get_session()\n vm = _get_vm(name, session=session)\n ret = None\n # -- try to get ip from vif\n vifs = session.xenapi.VM.get_VIFs(vm)\n if vifs is not None:\n for vif in vifs:\n if session.xenapi.VIF.get_ipv4_addresses(vif):\n cidr = session.xenapi.VIF.get_ipv4_addresses(vif).pop()\n ret, subnet = cidr.split('/')\n log.debug(\n 'VM vif returned for instance: %s ip: %s', name, ret)\n return ret\n # -- try to get ip from get tools metrics\n vgm = session.xenapi.VM.get_guest_metrics(vm)\n try:\n net = session.xenapi.VM_guest_metrics.get_networks(vgm)\n if \"0/ip\" in net.keys():\n log.debug(\n 'VM guest metrics returned for instance: %s 0/ip: %s',\n name, net[\"0/ip\"]\n )\n ret = net[\"0/ip\"]\n # except Exception as ex:\n except XenAPI.Failure:\n log.info('Could not get vm metrics at this time')\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsetting the IP address on a virtual interface.", "response": "def set_vm_ip(name=None,\n ipv4_cidr=None,\n ipv4_gw=None,\n session=None,\n call=None):\n '''\n Set the IP address on a virtual interface (vif)\n\n '''\n mode = 'static'\n # TODO: Need to add support for IPv6\n if call == 'function':\n raise SaltCloudException(\n 'The function must be called with -a or --action.')\n\n log.debug(\n 'Setting name: %s ipv4_cidr: %s ipv4_gw: %s mode: %s',\n name, ipv4_cidr, ipv4_gw, mode\n )\n if session is None:\n log.debug('New session being created')\n session = _get_session()\n vm = _get_vm(name, session)\n # -- try to get ip from vif\n # TODO: for now will take first interface\n # addition consideration needed for\n # multiple interface(vif) VMs\n vifs = session.xenapi.VM.get_VIFs(vm)\n if vifs is not None:\n log.debug('There are %s vifs.', len(vifs))\n for vif in vifs:\n record = session.xenapi.VIF.get_record(vif)\n log.debug(record)\n try:\n session.xenapi.VIF.configure_ipv4(\n vif, mode, ipv4_cidr, ipv4_gw)\n except XenAPI.Failure:\n log.info('Static IP assignment could not be performed.')\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_nodes_full(session=None):\n '''\n List full virtual machines\n\n .. code-block:: bash\n\n salt-cloud -F\n\n '''\n if session is None:\n session = _get_session()\n\n ret = {}\n vms = session.xenapi.VM.get_all()\n for vm in vms:\n record = session.xenapi.VM.get_record(vm)\n if not record['is_a_template'] and not record['is_control_domain']:\n # deal with cases where the VM doesn't have 'base_template_name' attribute\n try:\n base_template_name = record['other_config']['base_template_name']\n except Exception:\n base_template_name = None\n log.debug(\n 'VM %s, doesnt have base_template_name attribute',\n record['name_label']\n )\n vm_cfg = session.xenapi.VM.get_record(vm)\n vm_cfg['id'] = record['uuid']\n vm_cfg['name'] = record['name_label']\n vm_cfg['image'] = base_template_name\n vm_cfg['size'] = None\n vm_cfg['state'] = record['power_state']\n vm_cfg['private_ips'] = get_vm_ip(record['name_label'], session)\n vm_cfg['public_ips'] = None\n if 'snapshot_time' in vm_cfg.keys():\n del vm_cfg['snapshot_time']\n ret[record['name_label']] = vm_cfg\n\n provider = __active_provider_name__ or 'xen'\n if ':' in provider:\n comps = provider.split(':')\n provider = comps[0]\n log.debug('ret: %s', ret)\n log.debug('provider: %s', provider)\n log.debug('__opts__: %s', __opts__)\n __utils__['cloud.cache_node_list'](ret, provider, __opts__)\n return ret", "response": "List all virtual machines in the current state"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of available Xen VDI images", "response": "def vdi_list(call=None, kwargs=None):\n '''\n Return available Xen VDI images\n\n If this function is called with the ``-f`` or ``--function`` then\n it can return a list with minimal deatil using the ``terse=True`` keyword\n argument.\n\n .. code-block:: bash\n\n salt-cloud -f vdi_list myxen terse=True\n\n '''\n if call == 'action':\n raise SaltCloudException(\n 'This function must be called with -f or --function.')\n log.debug('kwargs is %s', kwargs)\n if kwargs is not None:\n if 'terse' in kwargs:\n if kwargs['terse'] == 'True':\n terse = True\n else:\n terse = False\n else:\n terse = False\n else:\n kwargs = {}\n terse = False\n session = _get_session()\n vdis = session.xenapi.VDI.get_all()\n ret = {}\n for vdi in vdis:\n data = session.xenapi.VDI.get_record(vdi)\n log.debug(type(terse))\n if terse is True:\n ret[data.get('name_label')] = {\n 'uuid': data.get('uuid'),\n 'OpqueRef': vdi}\n else:\n data.update({'OpaqueRef': vdi})\n ret[data.get('name_label')] = data\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef template_list(call=None):\n '''\n Return available Xen template information.\n\n This returns the details of\n each template to show number cores, memory sizes, etc..\n\n .. code-block:: bash\n\n salt-cloud -f template_list myxen\n\n '''\n templates = {}\n session = _get_session()\n vms = session.xenapi.VM.get_all()\n for vm in vms:\n record = session.xenapi.VM.get_record(vm)\n if record['is_a_template']:\n templates[record['name_label']] = record\n return templates", "response": "Return available Xen template information."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nshow information about a specific VM or template.", "response": "def show_instance(name, session=None, call=None):\n '''\n Show information about a specific VM or template\n\n .. code-block:: bash\n\n salt-cloud -a show_instance xenvm01\n\n .. note:: memory is memory_dynamic_max\n\n '''\n if call == 'function':\n raise SaltCloudException(\n 'The show_instnce function must be called with -a or --action.'\n )\n log.debug('show_instance-> name: %s session: %s', name, session)\n if session is None:\n session = _get_session()\n vm = _get_vm(name, session=session)\n record = session.xenapi.VM.get_record(vm)\n if not record['is_a_template'] and not record['is_control_domain']:\n try:\n base_template_name = record['other_config']['base_template_name']\n except Exception:\n base_template_name = None\n log.debug(\n 'VM %s, doesnt have base_template_name attribute',\n record['name_label']\n )\n ret = {'id': record['uuid'],\n 'image': base_template_name,\n 'name': record['name_label'],\n 'size': record['memory_dynamic_max'],\n 'state': record['power_state'],\n 'private_ips': get_vm_ip(name, session),\n 'public_ips': None}\n\n __utils__['cloud.cache_node'](\n ret,\n __active_provider_name__,\n __opts__\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _determine_resource_pool(session, vm_):\n '''\n Called by create() used to determine resource pool\n '''\n resource_pool = ''\n if 'resource_pool' in vm_.keys():\n resource_pool = _get_pool(vm_['resource_pool'], session)\n else:\n pool = session.xenapi.pool.get_all()\n if not pool:\n resource_pool = None\n else:\n first_pool = session.xenapi.pool.get_all()[0]\n resource_pool = first_pool\n pool_record = session.xenapi.pool.get_record(resource_pool)\n log.debug('resource pool: %s', pool_record['name_label'])\n return resource_pool", "response": "Called by create to determine resource pool"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndetermines the storage repo for the current resource pool.", "response": "def _determine_storage_repo(session, resource_pool, vm_):\n '''\n Called by create() used to determine storage repo for create\n '''\n storage_repo = ''\n if 'storage_repo' in vm_.keys():\n storage_repo = _get_sr(vm_['storage_repo'], session)\n else:\n storage_repo = None\n if resource_pool:\n default_sr = session.xenapi.pool.get_default_SR(resource_pool)\n sr_record = session.xenapi.SR.get_record(default_sr)\n log.debug('storage repository: %s', sr_record['name_label'])\n storage_repo = default_sr\n else:\n storage_repo = None\n log.debug('storage repository: %s', storage_repo)\n return storage_repo"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a VM in Xen", "response": "def create(vm_):\n '''\n Create a VM in Xen\n\n The configuration for this function is read from the profile settings.\n\n .. code-block:: bash\n\n salt-cloud -p some_profile xenvm01\n\n '''\n name = vm_['name']\n record = {}\n ret = {}\n\n # fire creating event\n __utils__['cloud.fire_event'](\n 'event',\n 'starting create',\n 'salt/cloud/{0}/creating'.format(name),\n args={\n 'name': name,\n 'profile': vm_['profile'],\n 'provider': vm_['driver'],\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n log.debug('Adding %s to cloud cache.', name)\n __utils__['cloud.cachedir_index_add'](\n vm_['name'], vm_['profile'], 'xen', vm_['driver']\n )\n\n # connect to xen\n session = _get_session()\n\n # determine resource pool\n resource_pool = _determine_resource_pool(session, vm_)\n\n # determine storage repo\n storage_repo = _determine_storage_repo(session, resource_pool, vm_)\n\n # build VM\n image = vm_.get('image')\n clone = vm_.get('clone')\n if clone is None:\n clone = True\n log.debug('Clone: %s ', clone)\n\n # fire event to read new vm properties (requesting)\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(name),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n # create by cloning template\n if clone:\n _clone_vm(image, name, session)\n else:\n _copy_vm(image, name, session, storage_repo)\n\n # provision template to vm\n _provision_vm(name, session)\n vm = _get_vm(name, session)\n\n # start vm\n start(name, None, session)\n\n # get new VM\n vm = _get_vm(name, session)\n\n # wait for vm to report IP via guest tools\n _wait_for_ip(name, session)\n\n # set static IP if configured\n _set_static_ip(name, session, vm_)\n\n # if not deploying salt then exit\n deploy = vm_.get('deploy', True)\n log.debug('delopy is set to %s', deploy)\n if deploy:\n record = session.xenapi.VM.get_record(vm)\n if record is not None:\n _deploy_salt_minion(name, session, vm_)\n else:\n log.debug(\n 'The Salt minion will not be installed, deploy: %s',\n vm_['deploy']\n )\n record = session.xenapi.VM.get_record(vm)\n ret = show_instance(name)\n ret.update({'extra': record})\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(name),\n args={\n 'name': name,\n 'profile': vm_['profile'],\n 'provider': vm_['driver'],\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _deploy_salt_minion(name, session, vm_):\n '''\n Deploy salt minion during create()\n '''\n # Get bootstrap values\n vm_['ssh_host'] = get_vm_ip(name, session)\n vm_['user'] = vm_.get('user', 'root')\n vm_['password'] = vm_.get('password', 'p@ssw0rd!')\n vm_['provider'] = vm_.get('provider', 'xen')\n log.debug('%s has IP of %s', name, vm_['ssh_host'])\n # Bootstrap Salt minion!\n if vm_['ssh_host'] is not None:\n log.info('Installing Salt minion on %s', name)\n boot_ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n log.debug('boot return: %s', boot_ret)", "response": "Deploy a salt minion during create"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _set_static_ip(name, session, vm_):\n '''\n Set static IP during create() if defined\n '''\n ipv4_cidr = ''\n ipv4_gw = ''\n if 'ipv4_gw' in vm_.keys():\n log.debug('ipv4_gw is found in keys')\n ipv4_gw = vm_['ipv4_gw']\n if 'ipv4_cidr' in vm_.keys():\n log.debug('ipv4_cidr is found in keys')\n ipv4_cidr = vm_['ipv4_cidr']\n log.debug('attempting to set IP in instance')\n set_vm_ip(name, ipv4_cidr, ipv4_gw, session, None)", "response": "Set static IP in the VM if it is not already set"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nwaiting for IP to be available during create", "response": "def _wait_for_ip(name, session):\n '''\n Wait for IP to be available during create()\n '''\n start_time = datetime.now()\n status = None\n while status is None:\n status = get_vm_ip(name, session)\n if status is not None:\n # ignore APIPA address\n if status.startswith('169'):\n status = None\n check_time = datetime.now()\n delta = check_time - start_time\n log.debug(\n 'Waited %s seconds for %s to report ip address...',\n delta.seconds, name\n )\n if delta.seconds > 180:\n log.warning('Timeout getting IP address')\n break\n time.sleep(5)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrunning XenAPI task in asynchronous mode to prevent timeouts", "response": "def _run_async_task(task=None, session=None):\n '''\n Run XenAPI task in asynchronous mode to prevent timeouts\n '''\n if task is None or session is None:\n return None\n task_name = session.xenapi.task.get_name_label(task)\n log.debug('Running %s', task_name)\n while session.xenapi.task.get_status(task) == 'pending':\n progress = round(session.xenapi.task.get_progress(task), 2) * 100\n log.debug('Task progress %.2f%%', progress)\n time.sleep(1)\n log.debug('Cleaning up task %s', task_name)\n session.xenapi.task.destroy(task)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _clone_vm(image=None, name=None, session=None):\n '''\n Create VM by cloning\n\n This is faster and should be used if source and target are\n in the same storage repository\n\n '''\n if session is None:\n session = _get_session()\n log.debug('Creating VM %s by cloning %s', name, image)\n source = _get_vm(image, session)\n task = session.xenapi.Async.VM.clone(source, name)\n _run_async_task(task, session)", "response": "Create VM by cloning the source VM and then cloning the target VM"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _copy_vm(template=None, name=None, session=None, sr=None):\n '''\n Create VM by copy\n\n This is slower and should be used if source and target are\n NOT in the same storage repository\n\n template = object reference\n name = string name of new VM\n session = object reference\n sr = object reference\n '''\n if session is None:\n session = _get_session()\n log.debug('Creating VM %s by copying %s', name, template)\n source = _get_vm(template, session)\n task = session.xenapi.Async.VM.copy(source, name, sr)\n _run_async_task(task, session)", "response": "Create VM by copying a template VM"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _provision_vm(name=None, session=None):\n '''\n Provision vm right after clone/copy\n '''\n if session is None:\n session = _get_session()\n log.info('Provisioning VM %s', name)\n vm = _get_vm(name, session)\n task = session.xenapi.Async.VM.provision(vm)\n _run_async_task(task, session)", "response": "Provision vm right after clone or copy"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef unpause(name, call=None, session=None):\n '''\n UnPause a vm\n\n .. code-block:: bash\n\n salt-cloud -a unpause xenvm01\n\n '''\n if call == 'function':\n raise SaltCloudException(\n 'The show_instnce function must be called with -a or --action.'\n )\n if session is None:\n session = _get_session()\n log.info('Unpausing VM %s', name)\n vm = _get_vm(name, session)\n task = session.xenapi.Async.VM.unpause(vm)\n _run_async_task(task, session)\n return show_instance(name)", "response": "UnPause a vm\n\n .. code-block:: bash\n\n salt-cloud -a unpause xenvm01"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nresume a VM from disk", "response": "def resume(name, call=None, session=None):\n '''\n Resume a vm from disk\n\n .. code-block:: bash\n\n salt-cloud -a resume xenvm01\n\n '''\n if call == 'function':\n raise SaltCloudException(\n 'The show_instnce function must be called with -a or --action.'\n )\n if session is None:\n session = _get_session()\n log.info('Resuming VM %s', name)\n vm = _get_vm(name, session)\n task = session.xenapi.Async.VM.resume(vm, False, True)\n _run_async_task(task, session)\n return show_instance(name)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef stop(name, call=None, session=None):\n '''\n Stop a vm\n\n .. code-block:: bash\n\n salt-cloud -a stop xenvm01\n\n\n '''\n if call == 'function':\n raise SaltCloudException(\n 'The show_instnce function must be called with -a or --action.'\n )\n return shutdown(name, call, session)", "response": "Stop a VM and all its children"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_vm(name=None, session=None):\n '''\n Get XEN vm instance object reference\n '''\n if session is None:\n session = _get_session()\n vms = session.xenapi.VM.get_by_name_label(name)\n if len(vms) == 1:\n return vms[0]\n return None", "response": "Get XEN vm instance object reference"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget XEN sr object reference", "response": "def _get_sr(name=None, session=None):\n '''\n Get XEN sr (storage repo) object reference\n '''\n if session is None:\n session = _get_session()\n srs = session.xenapi.SR.get_by_name_label(name)\n if len(srs) == 1:\n return srs[0]\n return None"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget XEN resource pool object reference", "response": "def _get_pool(name=None, session=None):\n '''\n Get XEN resource pool object reference\n '''\n if session is None:\n session = _get_session()\n pools = session.xenapi.pool.get_all()\n for pool in pools:\n pool_record = session.xenapi.pool.get_record(pool)\n if name in pool_record.get('name_label'):\n return pool\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef destroy(name=None, call=None):\n '''\n Destroy Xen VM or template instance\n\n .. code-block:: bash\n\n salt-cloud -d xenvm01\n\n '''\n if call == 'function':\n raise SaltCloudSystemExit(\n 'The destroy action must be called with -d, --destroy, '\n '-a or --action.'\n )\n ret = {}\n __utils__['cloud.fire_event'](\n 'event',\n 'destroying instance',\n 'salt/cloud/{0}/destroying'.format(name),\n args={'name': name},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n session = _get_session()\n vm = _get_vm(name)\n if vm:\n # get vm\n record = session.xenapi.VM.get_record(vm)\n log.debug('power_state: %s', record['power_state'])\n # shut down\n if record['power_state'] != 'Halted':\n task = session.xenapi.Async.VM.hard_shutdown(vm)\n _run_async_task(task, session)\n\n # destroy disk (vdi) by reading vdb on vm\n ret['vbd'] = destroy_vm_vdis(name, session)\n # destroy vm\n task = session.xenapi.Async.VM.destroy(vm)\n _run_async_task(task, session)\n ret['destroyed'] = True\n __utils__['cloud.fire_event'](\n 'event',\n 'destroyed instance',\n 'salt/cloud/{0}/destroyed'.format(name),\n args={'name': name},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n if __opts__.get('update_cachedir', False) is True:\n __utils__['cloud.delete_minion_cachedir'](\n name,\n __active_provider_name__.split(':')[0],\n __opts__\n )\n __utils__['cloud.cachedir_index_del'](name)\n return ret", "response": "Destroy an Xen VM or template instance."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef sr_list(call=None):\n '''\n Geta list of storage repositories\n\n .. code-block:: bash\n\n salt-cloud -f sr_list myxen\n\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'This function must be called with -f, --function argument.'\n )\n ret = {}\n session = _get_session()\n srs = session.xenapi.SR.get_all()\n for sr in srs:\n sr_record = session.xenapi.SR.get_record(sr)\n ret[sr_record['name_label']] = sr_record\n return ret", "response": "Return a list of storage repositories"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of Xen Servers in this VMware environment", "response": "def host_list(call=None):\n '''\n Get a list of Xen Servers\n\n .. code-block:: bash\n\n salt-cloud -f host_list myxen\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'This function must be called with -f, --function argument.'\n )\n ret = {}\n session = _get_session()\n hosts = session.xenapi.host.get_all()\n for host in hosts:\n host_record = session.xenapi.host.get_record(host)\n ret[host_record['name_label']] = host_record\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of Resource Pools", "response": "def pool_list(call=None):\n '''\n Get a list of Resource Pools\n\n .. code-block:: bash\n\n salt-cloud -f pool_list myxen\n\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'This function must be called with -f, --function argument.'\n )\n ret = {}\n session = _get_session()\n pools = session.xenapi.pool.get_all()\n for pool in pools:\n pool_record = session.xenapi.pool.get_record(pool)\n ret[pool_record['name_label']] = pool_record\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of Resource Pools", "response": "def pif_list(call=None):\n '''\n Get a list of Resource Pools\n\n .. code-block:: bash\n\n salt-cloud -f pool_list myxen\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'This function must be called with -f, --function argument.'\n )\n ret = {}\n session = _get_session()\n pifs = session.xenapi.PIF.get_all()\n for pif in pifs:\n record = session.xenapi.PIF.get_record(pif)\n ret[record['uuid']] = record\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef vif_list(name, call=None, kwargs=None):\n '''\n Get a list of virtual network interfaces on a VM\n\n **requires**: the name of the vm with the vbd definition\n\n .. code-block:: bash\n\n salt-cloud -a vif_list xenvm01\n\n '''\n if call == 'function':\n raise SaltCloudSystemExit(\n 'This function must be called with -a, --action argument.'\n )\n if name is None:\n return 'A name kwarg is rquired'\n ret = {}\n data = {}\n session = _get_session()\n vm = _get_vm(name)\n vifs = session.xenapi.VM.get_VIFs(vm)\n if vifs is not None:\n x = 0\n for vif in vifs:\n vif_record = session.xenapi.VIF.get_record(vif)\n data['vif-{}'.format(x)] = vif_record\n x += 1\n ret[name] = data\n return ret", "response": "Return a list of virtual network interfaces on a VM"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of VBDs on a VM", "response": "def vbd_list(name=None, call=None):\n '''\n Get a list of VBDs on a VM\n\n **requires**: the name of the vm with the vbd definition\n\n .. code-block:: bash\n\n salt-cloud -a vbd_list xenvm01\n\n '''\n if call == 'function':\n raise SaltCloudSystemExit(\n 'This function must be called with -a, --action argument.'\n )\n if name is None:\n return 'A name kwarg is rquired'\n ret = {}\n data = {}\n session = _get_session()\n vms = session.xenapi.VM.get_by_name_label(name)\n if len(vms) == 1:\n vm = vms[0]\n vbds = session.xenapi.VM.get_VBDs(vm)\n if vbds is not None:\n x = 0\n for vbd in vbds:\n vbd_record = session.xenapi.VBD.get_record(vbd)\n data['vbd-{}'.format(x)] = vbd_record\n x += 1\n ret = data\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef destroy_vm_vdis(name=None, session=None, call=None):\n '''\n Get virtual block devices on VM\n\n .. code-block:: bash\n\n salt-cloud -a destroy_vm_vdis xenvm01\n\n '''\n if session is None:\n session = _get_session()\n ret = {}\n # get vm object\n vms = session.xenapi.VM.get_by_name_label(name)\n if len(vms) == 1:\n # read virtual block device (vdb)\n vbds = session.xenapi.VM.get_VBDs(vms[0])\n if vbds is not None:\n x = 0\n for vbd in vbds:\n vbd_record = session.xenapi.VBD.get_record(vbd)\n if vbd_record['VDI'] != 'OpaqueRef:NULL':\n # read vdi on vdb\n vdi_record = session.xenapi.VDI.get_record(\n vbd_record['VDI'])\n if 'iso' not in vdi_record['name_label']:\n session.xenapi.VDI.destroy(vbd_record['VDI'])\n ret['vdi-{}'.format(x)] = vdi_record['name_label']\n x += 1\n return ret", "response": "Destroy virtual block devices on VM\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndestroys a specific Xen VM or template instance.", "response": "def destroy_template(name=None, call=None, kwargs=None):\n '''\n Destroy Xen VM or template instance\n\n .. code-block:: bash\n\n salt-cloud -f destroy_template myxen name=testvm2\n\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The destroy_template function must be called with -f.'\n )\n if kwargs is None:\n kwargs = {}\n name = kwargs.get('name', None)\n session = _get_session()\n vms = session.xenapi.VM.get_all_records()\n ret = {}\n found = False\n for vm in vms:\n record = session.xenapi.VM.get_record(vm)\n if record['is_a_template']:\n if record['name_label'] == name:\n found = True\n # log.debug(record['name_label'])\n session.xenapi.VM.destroy(vm)\n ret[name] = {'status': 'destroyed'}\n if not found:\n ret[name] = {'status': 'not found'}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the PV arguments for a VM", "response": "def get_pv_args(name, session=None, call=None):\n '''\n Get PV arguments for a VM\n\n .. code-block:: bash\n\n salt-cloud -a get_pv_args xenvm01\n\n '''\n if call == 'function':\n raise SaltCloudException(\n 'This function must be called with -a or --action.'\n )\n if session is None:\n log.debug('New session being created')\n session = _get_session()\n vm = _get_vm(name, session=session)\n pv_args = session.xenapi.VM.get_PV_args(vm)\n if pv_args:\n return pv_args\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_pv_args(name, kwargs=None, session=None, call=None):\n '''\n Set PV arguments for a VM\n\n .. code-block:: bash\n\n salt-cloud -a set_pv_args xenvm01 pv_args=\"utf-8 graphical\"\n\n '''\n if call == 'function':\n raise SaltCloudException(\n 'This function must be called with -a or --action.'\n )\n if session is None:\n log.debug('New session being created')\n session = _get_session()\n vm = _get_vm(name, session=session)\n try:\n log.debug('Setting PV Args: %s', kwargs['pv_args'])\n session.xenapi.VM.set_PV_args(vm, str(kwargs['pv_args']))\n except KeyError:\n log.error('No pv_args parameter found.')\n return False\n except XenAPI.Failure:\n log.info('Setting PV Args failed.')\n return False\n return True", "response": "Set PV arguments for a VM"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _name_in_services(name, services):\n '''\n Checks to see if the given service is in the given services.\n\n :param str name: Service label, file name, or full path\n\n :param dict services: The currently available services.\n\n :return: The service information for the service, otherwise\n an empty dictionary\n\n :rtype: dict\n '''\n if name in services:\n # Match on label\n return services[name]\n\n for service in six.itervalues(services):\n if service['file_path'].lower() == name:\n # Match on full path\n return service\n basename, ext = os.path.splitext(service['file_name'])\n if basename.lower() == name:\n # Match on basename\n return service\n\n return dict()", "response": "Checks to see if the given service is in the given services."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget information about a service.", "response": "def _get_service(name):\n '''\n Get information about a service. If the service is not found, raise an\n error\n\n :param str name: Service label, file name, or full path\n\n :return: The service information for the service, otherwise an Error\n :rtype: dict\n '''\n services = __utils__['mac_utils.available_services']()\n name = name.lower()\n\n service = _name_in_services(name, services)\n\n # if we would the service we can return it\n if service:\n return service\n\n # if we got here our service is not available, now we can check to see if\n # we received a cached batch of services, if not we did a fresh check\n # so we need to raise that the service could not be found.\n try:\n if not __context__['using_cached_services']:\n raise CommandExecutionError('Service not found: {0}'.format(name))\n except KeyError:\n pass\n\n # we used a cached version to check, a service could have been made\n # between now and then, we should refresh our available services.\n services = __utils__['mac_utils.available_services'](refresh=True)\n\n # check to see if we found the service we are looking for.\n service = _name_in_services(name, services)\n\n if not service:\n # Could not find the service after refresh raise.\n raise CommandExecutionError('Service not found: {0}'.format(name))\n\n # found it :)\n return service"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _always_running_service(name):\n '''\n Check if the service should always be running based on the KeepAlive Key\n in the service plist.\n\n :param str name: Service label, file name, or full path\n\n :return: True if the KeepAlive key is set to True, False if set to False or\n not set in the plist at all.\n\n :rtype: bool\n\n .. versionadded:: 2019.2.0\n '''\n\n # get all the info from the launchctl service\n service_info = show(name)\n\n # get the value for the KeepAlive key in service plist\n try:\n keep_alive = service_info['plist']['KeepAlive']\n except KeyError:\n return False\n\n # check if KeepAlive is True and not just set.\n\n if isinstance(keep_alive, dict):\n # check for pathstate\n for _file, value in six.iteritems(keep_alive.get('PathState', {})):\n if value is True and os.path.exists(_file):\n return True\n elif value is False and not os.path.exists(_file):\n return True\n\n if keep_alive is True:\n return True\n\n return False", "response": "Check if the service should always be running based on the KeepAlive key in the service plist."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_domain_target(name, service_target=False):\n '''\n Returns the domain/service target and path for a service. This is used to\n determine whether or not a service should be loaded in a user space or\n system space.\n\n :param str name: Service label, file name, or full path\n\n :param bool service_target: Whether to return a full\n service target. This is needed for the enable and disable\n subcommands of /bin/launchctl. Defaults to False\n\n :return: Tuple of the domain/service target and the path to the service.\n\n :rtype: tuple\n\n .. versionadded:: 2019.2.0\n '''\n\n # Get service information\n service = _get_service(name)\n\n # get the path to the service\n path = service['file_path']\n\n # most of the time we'll be at the system level.\n domain_target = 'system'\n\n # check if a LaunchAgent as we should treat these differently.\n if 'LaunchAgents' in path:\n # Get the console user so we can service in the correct session\n uid = __utils__['mac_utils.console_user']()\n domain_target = 'gui/{}'.format(uid)\n\n # check to see if we need to make it a full service target.\n if service_target is True:\n domain_target = '{}/{}'.format(domain_target, service['plist']['Label'])\n\n return (domain_target, path)", "response": "Returns the domain target and path for a service."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_(name=None, runas=None):\n '''\n Run launchctl list and return the output\n\n :param str name: The name of the service to list\n\n :param str runas: User to run launchctl commands\n\n :return: If a name is passed returns information about the named service,\n otherwise returns a list of all services and pids\n :rtype: str\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.list\n salt '*' service.list org.cups.cupsd\n '''\n if name:\n # Get service information and label\n service = _get_service(name)\n label = service['plist']['Label']\n\n # we can assume if we are trying to list a LaunchAgent we need\n # to run as a user, if not provided, we'll use the console user.\n if not runas and _launch_agent(name):\n runas = __utils__['mac_utils.console_user'](username=True)\n\n # Collect information on service: will raise an error if it fails\n return launchctl('list',\n label,\n return_stdout=True,\n runas=runas)\n\n # Collect information on all services: will raise an error if it fails\n return launchctl('list',\n return_stdout=True,\n runas=runas)", "response": "List the services and pids of a service."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef enable(name, runas=None):\n '''\n Enable a launchd service. Raises an error if the service fails to be enabled\n\n :param str name: Service label, file name, or full path\n\n :param str runas: User to run launchctl commands\n\n :return: ``True`` if successful or if the service is already enabled\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.enable org.cups.cupsd\n '''\n # Get the domain target. enable requires a full \n service_target = _get_domain_target(name, service_target=True)[0]\n\n # Enable the service: will raise an error if it fails\n return launchctl('enable', service_target, runas=runas)", "response": "Enable a launchd service."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef disable(name, runas=None):\n '''\n Disable a launchd service. Raises an error if the service fails to be\n disabled\n\n :param str name: Service label, file name, or full path\n\n :param str runas: User to run launchctl commands\n\n :return: ``True`` if successful or if the service is already disabled\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.disable org.cups.cupsd\n '''\n # Get the service target. enable requires a full \n service_target = _get_domain_target(name, service_target=True)[0]\n\n # disable the service: will raise an error if it fails\n return launchctl('disable', service_target, runas=runas)", "response": "Disable a launchd service."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nstarts a launchd service.", "response": "def start(name, runas=None):\n '''\n Start a launchd service. Raises an error if the service fails to start\n\n .. note::\n To start a service in macOS the service must be enabled first. Use\n ``service.enable`` to enable the service.\n\n :param str name: Service label, file name, or full path\n\n :param str runas: User to run launchctl commands\n\n :return: ``True`` if successful or if the service is already running\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.start org.cups.cupsd\n '''\n # Get the domain target.\n domain_target, path = _get_domain_target(name)\n\n # Load (bootstrap) the service: will raise an error if it fails\n return launchctl('bootstrap', domain_target, path, runas=runas)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nstopping a launchd service.", "response": "def stop(name, runas=None):\n '''\n Stop a launchd service. Raises an error if the service fails to stop\n\n .. note::\n Though ``service.stop`` will unload a service in macOS, the service\n will start on next boot unless it is disabled. Use ``service.disable``\n to disable the service\n\n :param str name: Service label, file name, or full path\n\n :param str runas: User to run launchctl commands\n\n :return: ``True`` if successful or if the service is already stopped\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.stop org.cups.cupsd\n '''\n # Get the domain target.\n domain_target, path = _get_domain_target(name)\n\n # Stop (bootout) the service: will raise an error if it fails\n return launchctl('bootout', domain_target, path, runas=runas)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef restart(name, runas=None):\n '''\n Unloads and reloads a launchd service. Raises an error if the service\n fails to reload\n\n :param str name: Service label, file name, or full path\n\n :param str runas: User to run launchctl commands\n\n :return: ``True`` if successful\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.restart org.cups.cupsd\n '''\n # Restart the service: will raise an error if it fails\n if enabled(name):\n stop(name, runas=runas)\n start(name, runas=runas)\n\n return True", "response": "Unloads and reloads a launchd service."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef status(name, sig=None, runas=None):\n '''\n Return the status for a service.\n\n :param str name: Used to find the service from launchctl. Can be any part\n of the service name or a regex expression.\n\n :param str sig: Find the service with status.pid instead. Note that\n ``name`` must still be provided.\n\n :param str runas: User to run launchctl commands\n\n :return: The PID for the service if it is running, or 'loaded' if the\n service should not always have a PID, or otherwise an empty string\n\n :rtype: str\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.status cups\n '''\n # Find service with ps\n if sig:\n return __salt__['status.pid'](sig)\n\n try:\n _get_service(name)\n except CommandExecutionError as msg:\n log.error(msg)\n return ''\n\n if not runas and _launch_agent(name):\n runas = __utils__['mac_utils.console_user'](username=True)\n\n output = list_(runas=runas)\n\n # Used a string here instead of a list because that's what the linux version\n # of this module does\n pids = ''\n for line in output.splitlines():\n if 'PID' in line:\n continue\n if re.search(name, line.split()[-1]):\n if line.split()[0].isdigit():\n if pids:\n pids += '\\n'\n pids += line.split()[0]\n\n # mac services are a little different than other platforms as they may be\n # set to run on intervals and may not always active with a PID. This will\n # return a string 'loaded' if it shouldn't always be running and is enabled.\n if not _always_running_service(name) and enabled(name) and not pids:\n return 'loaded'\n\n return pids", "response": "Return the status of a service."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if the specified service is enabled", "response": "def enabled(name, runas=None):\n '''\n Check if the specified service is enabled\n\n :param str name: The name of the service to look up\n\n :param str runas: User to run launchctl commands\n\n :return: True if the specified service enabled, otherwise False\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.enabled org.cups.cupsd\n '''\n # Try to list the service. If it can't be listed, it's not enabled\n try:\n list_(name=name, runas=runas)\n return True\n except CommandExecutionError:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef disabled(name, runas=None, domain='system'):\n '''\n Check if the specified service is not enabled. This is the opposite of\n ``service.enabled``\n\n :param str name: The name to look up\n\n :param str runas: User to run launchctl commands\n\n :param str domain: domain to check for disabled services. Default is system.\n\n :return: True if the specified service is NOT enabled, otherwise False\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.disabled org.cups.cupsd\n '''\n\n disabled = launchctl('print-disabled',\n domain,\n return_stdout=True,\n runas=runas)\n for service in disabled.split(\"\\n\"):\n if name in service:\n srv_name = service.split(\"=>\")[0].split(\"\\\"\")[1]\n status = service.split(\"=>\")[1]\n if name != srv_name:\n pass\n else:\n return True if 'true' in status.lower() else False\n\n return False", "response": "Check if the specified service is disabled."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_all(runas=None):\n '''\n Return a list of services that are enabled or available. Can be used to\n find the name of a service.\n\n :param str runas: User to run launchctl commands\n\n :return: A list of all the services available or enabled\n :rtype: list\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.get_all\n '''\n # Get list of enabled services\n enabled = get_enabled(runas=runas)\n\n # Get list of all services\n available = list(__utils__['mac_utils.available_services']().keys())\n\n # Return composite list\n return sorted(set(enabled + available))", "response": "Return a list of all services that are enabled or available. Can be used to get a list of services that are enabled or available. Can be used to get a list of services that are enabled or available."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_enabled(runas=None):\n '''\n Return a list of all services that are enabled. Can be used to find the\n name of a service.\n\n :param str runas: User to run launchctl commands\n\n :return: A list of all the services enabled on the system\n :rtype: list\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.get_enabled\n '''\n # Collect list of enabled services\n stdout = list_(runas=runas)\n service_lines = [line for line in stdout.splitlines()]\n\n # Construct list of enabled services\n enabled = []\n for line in service_lines:\n # Skip header line\n if line.startswith('PID'):\n continue\n\n pid, status, label = line.split('\\t')\n enabled.append(label)\n\n return sorted(set(enabled))", "response": "Return a list of all services that are enabled on the system"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a conn object for the passed VM data", "response": "def get_conn():\n '''\n Return a conn object for the passed VM data\n '''\n vm_ = get_configured_provider()\n\n kwargs = vm_.copy() # pylint: disable=E1103\n\n kwargs['username'] = vm_['user']\n kwargs['project_id'] = vm_['tenant']\n kwargs['auth_url'] = vm_['identity_url']\n kwargs['region_name'] = vm_['compute_region']\n kwargs['use_keystoneauth'] = vm_.get('use_keystoneauth', False)\n\n if 'password' in vm_:\n kwargs['password'] = vm_['password']\n\n if 'verify' in vm_ and vm_['use_keystoneauth'] is True:\n kwargs['verify'] = vm_['verify']\n elif 'verify' in vm_ and vm_['use_keystoneauth'] is False:\n log.warning('SSL Certificate verification option is specified but use_keystoneauth is False or not present')\n conn = nova.SaltNova(**kwargs)\n\n return conn"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of locations for the resource in the cloud provider", "response": "def avail_locations(conn=None, call=None):\n '''\n Return a list of locations\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_locations function must be called with '\n '-f or --function, or with the --list-locations option'\n )\n\n if conn is None:\n conn = get_conn()\n\n endpoints = nova.get_entry(conn.get_catalog(), 'type', 'compute')['endpoints']\n ret = {}\n for endpoint in endpoints:\n ret[endpoint['region']] = endpoint\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_image(conn, vm_):\n '''\n Return the image object to use\n '''\n vm_image = config.get_cloud_config_value('image', vm_, __opts__, default='').encode(\n 'ascii', 'salt-cloud-force-ascii'\n )\n if not vm_image:\n log.debug('No image set, must be boot from volume')\n return None\n\n image_list = conn.image_list()\n\n for img in image_list:\n if vm_image in (image_list[img]['id'], img):\n return image_list[img]['id']\n\n try:\n image = conn.image_show(vm_image)\n return image['id']\n except novaclient.exceptions.NotFound as exc:\n raise SaltCloudNotFound(\n 'The specified image, \\'{0}\\', could not be found: {1}'.format(\n vm_image,\n exc\n )\n )", "response": "Return the image object to use"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nshows the details from the provider concerning an instance", "response": "def show_instance(name, call=None):\n '''\n Show the details from the provider concerning an instance\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The show_instance action must be called with -a or --action.'\n )\n\n conn = get_conn()\n node = conn.show_instance(name).__dict__\n __utils__['cloud.cache_node'](node, __active_provider_name__, __opts__)\n return node"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_size(conn, vm_):\n '''\n Return the VM's size object\n '''\n sizes = conn.list_sizes()\n vm_size = config.get_cloud_config_value('size', vm_, __opts__)\n if not vm_size:\n return sizes[0]\n\n for size in sizes:\n if vm_size and six.text_type(vm_size) in (six.text_type(sizes[size]['id']), six.text_type(size)):\n return sizes[size]['id']\n raise SaltCloudNotFound(\n 'The specified size, \\'{0}\\', could not be found.'.format(vm_size)\n )", "response": "Return the VM s size object"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns True if we are to ignore the specified IP. Compatible with IPv4.", "response": "def ignore_cidr(vm_, ip):\n '''\n Return True if we are to ignore the specified IP. Compatible with IPv4.\n '''\n if HAS_NETADDR is False:\n log.error('Error: netaddr is not installed')\n return 'Error: netaddr is not installed'\n\n cidr = config.get_cloud_config_value(\n 'ignore_cidr', vm_, __opts__, default='', search_global=False\n )\n if cidr != '' and all_matching_cidrs(ip, [cidr]):\n log.warning('IP \"%s\" found within \"%s\"; ignoring it.', ip, cidr)\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns whether we should wait for rackconnect automation before running.", "response": "def rackconnect(vm_):\n '''\n Determine if we should wait for rackconnect automation before running.\n Either 'False' (default) or 'True'.\n '''\n return config.get_cloud_config_value(\n 'rackconnect', vm_, __opts__, default=False,\n search_global=False\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef rackconnectv3(vm_):\n '''\n Determine if server is using rackconnectv3 or not\n Return the rackconnect network name or False\n '''\n return config.get_cloud_config_value(\n 'rackconnectv3', vm_, __opts__, default=False,\n search_global=False\n )", "response": "Return rackconnect network name or False"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndetermines if we should use extra network to bootstrap the current node.", "response": "def cloudnetwork(vm_):\n '''\n Determine if we should use an extra network to bootstrap\n Either 'False' (default) or 'True'.\n '''\n return config.get_cloud_config_value(\n 'cloudnetwork', vm_, __opts__, default=False,\n search_global=False\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns whether managedcloud is enabled for this VM. Either False or True.", "response": "def managedcloud(vm_):\n '''\n Determine if we should wait for the managed cloud automation before\n running. Either 'False' (default) or 'True'.\n '''\n return config.get_cloud_config_value(\n 'managedcloud', vm_, __opts__, default=False,\n search_global=False\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndelete a single VM", "response": "def destroy(name, conn=None, call=None):\n '''\n Delete a single VM\n '''\n if call == 'function':\n raise SaltCloudSystemExit(\n 'The destroy action must be called with -d, --destroy, '\n '-a or --action.'\n )\n\n __utils__['cloud.fire_event'](\n 'event',\n 'destroying instance',\n 'salt/cloud/{0}/destroying'.format(name),\n args={'name': name},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n if not conn:\n conn = get_conn() # pylint: disable=E0602\n\n node = conn.server_by_name(name)\n profiles = get_configured_provider()['profiles'] # pylint: disable=E0602\n if node is None:\n log.error('Unable to find the VM %s', name)\n profile = None\n if 'metadata' in node.extra and 'profile' in node.extra['metadata']:\n profile = node.extra['metadata']['profile']\n\n flush_mine_on_destroy = False\n if profile and profile in profiles and 'flush_mine_on_destroy' in profiles[profile]:\n flush_mine_on_destroy = profiles[profile]['flush_mine_on_destroy']\n\n if flush_mine_on_destroy:\n log.info('Clearing Salt Mine: %s', name)\n salt_client = salt.client.get_local_client(__opts__['conf_file'])\n minions = salt_client.cmd(name, 'mine.flush')\n\n log.info('Clearing Salt Mine: %s, %s', name, flush_mine_on_destroy)\n log.info('Destroying VM: %s', name)\n ret = conn.delete(node.id)\n if ret:\n log.info('Destroyed VM: %s', name)\n # Fire destroy action\n __utils__['cloud.fire_event'](\n 'event',\n 'destroyed instance',\n 'salt/cloud/{0}/destroyed'.format(name),\n args={'name': name},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n if __opts__.get('delete_sshkeys', False) is True:\n salt.utils.cloud.remove_sshkey(getattr(node, __opts__.get('ssh_interface', 'public_ips'))[0])\n if __opts__.get('update_cachedir', False) is True:\n __utils__['cloud.delete_minion_cachedir'](name, __active_provider_name__.split(':')[0], __opts__)\n __utils__['cloud.cachedir_index_del'](name)\n return True\n\n log.error('Failed to Destroy VM: %s', name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrequesting an instance from OpenStack.", "response": "def request_instance(vm_=None, call=None):\n '''\n Put together all of the information necessary to request an instance\n through Novaclient and then fire off the request the instance.\n\n Returns data about the instance\n '''\n if call == 'function':\n # Technically this function may be called other ways too, but it\n # definitely cannot be called with --function.\n raise SaltCloudSystemExit(\n 'The request_instance action must be called with -a or --action.'\n )\n log.info('Creating Cloud VM %s', vm_['name'])\n salt.utils.cloud.check_name(vm_['name'], 'a-zA-Z0-9._-')\n conn = get_conn()\n kwargs = vm_.copy()\n\n try:\n kwargs['image_id'] = get_image(conn, vm_)\n except Exception as exc:\n raise SaltCloudSystemExit(\n 'Error creating {0} on OPENSTACK\\n\\n'\n 'Could not find image {1}: {2}\\n'.format(\n vm_['name'], vm_['image'], exc\n )\n )\n\n try:\n kwargs['flavor_id'] = get_size(conn, vm_)\n except Exception as exc:\n raise SaltCloudSystemExit(\n 'Error creating {0} on OPENSTACK\\n\\n'\n 'Could not find size {1}: {2}\\n'.format(\n vm_['name'], vm_['size'], exc\n )\n )\n\n kwargs['key_name'] = config.get_cloud_config_value(\n 'ssh_key_name', vm_, __opts__, search_global=False\n )\n\n security_groups = config.get_cloud_config_value(\n 'security_groups', vm_, __opts__, search_global=False\n )\n if security_groups is not None:\n vm_groups = security_groups\n avail_groups = conn.secgroup_list()\n group_list = []\n\n for vmg in vm_groups:\n if vmg in [name for name, details in six.iteritems(avail_groups)]:\n group_list.append(vmg)\n else:\n raise SaltCloudNotFound(\n 'No such security group: \\'{0}\\''.format(vmg)\n )\n\n kwargs['security_groups'] = group_list\n\n avz = config.get_cloud_config_value(\n 'availability_zone', vm_, __opts__, default=None, search_global=False\n )\n if avz is not None:\n kwargs['availability_zone'] = avz\n\n kwargs['nics'] = config.get_cloud_config_value(\n 'networks', vm_, __opts__, search_global=False, default=None\n )\n\n files = config.get_cloud_config_value(\n 'files', vm_, __opts__, search_global=False\n )\n if files:\n kwargs['files'] = {}\n for src_path in files:\n if os.path.exists(files[src_path]):\n with salt.utils.files.fopen(files[src_path], 'r') as fp_:\n kwargs['files'][src_path] = fp_.read()\n else:\n kwargs['files'][src_path] = files[src_path]\n\n userdata_file = config.get_cloud_config_value(\n 'userdata_file', vm_, __opts__, search_global=False, default=None\n )\n if userdata_file is not None:\n try:\n with salt.utils.files.fopen(userdata_file, 'r') as fp_:\n kwargs['userdata'] = salt.utils.cloud.userdata_template(\n __opts__, vm_, fp_.read()\n )\n except Exception as exc:\n log.exception(\n 'Failed to read userdata from %s: %s', userdata_file, exc)\n\n kwargs['config_drive'] = config.get_cloud_config_value(\n 'config_drive', vm_, __opts__, search_global=False\n )\n\n kwargs.update(get_block_mapping_opts(vm_))\n\n event_kwargs = {\n 'name': kwargs['name'],\n 'image': kwargs.get('image_id', 'Boot From Volume'),\n 'size': kwargs['flavor_id'],\n }\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n args={\n 'kwargs': __utils__['cloud.filter_event']('requesting', event_kwargs, list(event_kwargs)),\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n try:\n data = conn.boot(**kwargs)\n except Exception as exc:\n raise SaltCloudSystemExit(\n 'Error creating {0} on Nova\\n\\n'\n 'The following exception was thrown by libcloud when trying to '\n 'run the initial deployment: {1}\\n'.format(\n vm_['name'], exc\n )\n )\n if data.extra.get('password', None) is None and vm_.get('key_filename', None) is None:\n raise SaltCloudSystemExit('No password returned. Set ssh_key_file.')\n\n floating_ip_conf = config.get_cloud_config_value('floating_ip',\n vm_,\n __opts__,\n search_global=False,\n default={})\n if floating_ip_conf.get('auto_assign', False):\n floating_ip = None\n if floating_ip_conf.get('ip_address', None) is not None:\n ip_address = floating_ip_conf.get('ip_address', None)\n try:\n fl_ip_dict = conn.floating_ip_show(ip_address)\n floating_ip = fl_ip_dict['ip']\n except Exception as err:\n raise SaltCloudSystemExit(\n 'Error assigning floating_ip for {0} on Nova\\n\\n'\n 'The following exception was thrown by libcloud when trying to '\n 'assign a floating ip: {1}\\n'.format(\n vm_['name'], err\n )\n )\n\n else:\n pool = floating_ip_conf.get('pool', 'public')\n try:\n floating_ip = conn.floating_ip_create(pool)['ip']\n except Exception:\n log.info('A new IP address was unable to be allocated. '\n 'An IP address will be pulled from the already allocated list, '\n 'This will cause a race condition when building in parallel.')\n for fl_ip, opts in six.iteritems(conn.floating_ip_list()):\n if opts['fixed_ip'] is None and opts['pool'] == pool:\n floating_ip = fl_ip\n break\n if floating_ip is None:\n log.error('No IP addresses available to allocate for this server: %s', vm_['name'])\n\n def __query_node_data(vm_):\n try:\n node = show_instance(vm_['name'], 'action')\n log.debug('Loaded node data for %s:\\n%s', vm_['name'], pprint.pformat(node))\n except Exception as err:\n log.error(\n 'Failed to get nodes list: %s', err,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n # Trigger a failure in the wait for IP function\n return False\n return node['state'] == 'ACTIVE' or None\n\n # if we associate the floating ip here,then we will fail.\n # As if we attempt to associate a floating IP before the Nova instance has completed building,\n # it will fail.So we should associate it after the Nova instance has completed building.\n try:\n salt.utils.cloud.wait_for_ip(\n __query_node_data,\n update_args=(vm_,)\n )\n except (SaltCloudExecutionTimeout, SaltCloudExecutionFailure) as exc:\n try:\n # It might be already up, let's destroy it!\n destroy(vm_['name'])\n except SaltCloudSystemExit:\n pass\n finally:\n raise SaltCloudSystemExit(six.text_type(exc))\n\n try:\n conn.floating_ip_associate(vm_['name'], floating_ip)\n vm_['floating_ip'] = floating_ip\n except Exception as exc:\n raise SaltCloudSystemExit(\n 'Error assigning floating_ip for {0} on Nova\\n\\n'\n 'The following exception was thrown by libcloud when trying to '\n 'assign a floating ip: {1}\\n'.format(\n vm_['name'], exc\n )\n )\n\n if not vm_.get('password', None):\n vm_['password'] = data.extra.get('password', '')\n\n return data, vm_"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create(vm_):\n '''\n Create a single VM from a data dict\n '''\n try:\n # Check for required profile parameters before sending any API calls.\n if vm_['profile'] and config.is_profile_configured(__opts__,\n __active_provider_name__ or 'nova',\n vm_['profile'],\n vm_=vm_) is False:\n return False\n except AttributeError:\n pass\n\n deploy = config.get_cloud_config_value('deploy', vm_, __opts__)\n key_filename = config.get_cloud_config_value(\n 'ssh_key_file', vm_, __opts__, search_global=False, default=None\n )\n if key_filename is not None and not os.path.isfile(key_filename):\n raise SaltCloudConfigError(\n 'The defined ssh_key_file \\'{0}\\' does not exist'.format(\n key_filename\n )\n )\n\n vm_['key_filename'] = key_filename\n\n __utils__['cloud.fire_event'](\n 'event',\n 'starting create',\n 'salt/cloud/{0}/creating'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('creating', vm_, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n conn = get_conn()\n\n if 'instance_id' in vm_:\n # This was probably created via another process, and doesn't have\n # things like salt keys created yet, so let's create them now.\n if 'pub_key' not in vm_ and 'priv_key' not in vm_:\n log.debug('Generating minion keys for \\'%s\\'', vm_['name'])\n vm_['priv_key'], vm_['pub_key'] = salt.utils.cloud.gen_keys(\n salt.config.get_cloud_config_value(\n 'keysize',\n vm_,\n __opts__\n )\n )\n data = conn.server_show_libcloud(vm_['instance_id'])\n if vm_['key_filename'] is None and 'change_password' in __opts__ and __opts__['change_password'] is True:\n vm_['password'] = salt.utils.pycrypto.secure_password()\n conn.root_password(vm_['instance_id'], vm_['password'])\n else:\n # Put together all of the information required to request the instance,\n # and then fire off the request for it\n data, vm_ = request_instance(vm_)\n\n # Pull the instance ID, valid for both spot and normal instances\n vm_['instance_id'] = data.id\n\n try:\n data = salt.utils.cloud.wait_for_ip(\n _query_node_data,\n update_args=(vm_, data, conn),\n timeout=config.get_cloud_config_value(\n 'wait_for_ip_timeout', vm_, __opts__, default=10 * 60),\n interval=config.get_cloud_config_value(\n 'wait_for_ip_interval', vm_, __opts__, default=10),\n )\n except (SaltCloudExecutionTimeout, SaltCloudExecutionFailure) as exc:\n try:\n # It might be already up, let's destroy it!\n destroy(vm_['name'])\n except SaltCloudSystemExit:\n pass\n finally:\n raise SaltCloudSystemExit(six.text_type(exc))\n\n log.debug('VM is now running')\n\n if ssh_interface(vm_) == 'private_ips':\n ip_address = preferred_ip(vm_, data.private_ips)\n elif ssh_interface(vm_) == 'fixed_ips':\n ip_address = preferred_ip(vm_, data.fixed_ips)\n elif ssh_interface(vm_) == 'floating_ips':\n ip_address = preferred_ip(vm_, data.floating_ips)\n else:\n ip_address = preferred_ip(vm_, data.public_ips)\n log.debug('Using IP address %s', ip_address)\n\n if salt.utils.cloud.get_salt_interface(vm_, __opts__) == 'private_ips':\n salt_ip_address = preferred_ip(vm_, data.private_ips)\n log.info('Salt interface set to: %s', salt_ip_address)\n elif salt.utils.cloud.get_salt_interface(vm_, __opts__) == 'fixed_ips':\n salt_ip_address = preferred_ip(vm_, data.fixed_ips)\n log.info('Salt interface set to: %s', salt_ip_address)\n elif salt.utils.cloud.get_salt_interface(vm_, __opts__) == 'floating_ips':\n salt_ip_address = preferred_ip(vm_, data.floating_ips)\n log.info('Salt interface set to: %s', salt_ip_address)\n else:\n salt_ip_address = preferred_ip(vm_, data.public_ips)\n log.debug('Salt interface set to: %s', salt_ip_address)\n\n if not ip_address:\n raise SaltCloudSystemExit('A valid IP address was not found')\n\n vm_['ssh_host'] = ip_address\n vm_['salt_host'] = salt_ip_address\n\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n\n ret.update(data.__dict__)\n\n if 'password' in ret['extra']:\n del ret['extra']['password']\n\n log.info('Created Cloud VM \\'%s\\'', vm_['name'])\n log.debug(\n '\\'%s\\' VM creation details:\\n%s',\n vm_['name'], pprint.pformat(data.__dict__)\n )\n\n event_data = {\n 'name': vm_['name'],\n 'profile': vm_['profile'],\n 'provider': vm_['driver'],\n 'instance_id': vm_['instance_id'],\n 'floating_ips': data.floating_ips,\n 'fixed_ips': data.fixed_ips,\n 'private_ips': data.private_ips,\n 'public_ips': data.public_ips\n }\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('created', event_data, list(event_data)),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n __utils__['cloud.cachedir_index_add'](vm_['name'], vm_['profile'], 'nova', vm_['driver'])\n return ret", "response": "Create a single VM from a data dict"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of the VMs that are in this location", "response": "def list_nodes(call=None, **kwargs):\n '''\n Return a list of the VMs that in this location\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called with -f or --function.'\n )\n\n ret = {}\n conn = get_conn()\n server_list = conn.server_list()\n\n if not server_list:\n return {}\n for server in server_list:\n server_tmp = conn.server_show(server_list[server]['id']).get(server)\n\n # If the server is deleted while looking it up, skip\n if server_tmp is None:\n continue\n\n private = []\n public = []\n if 'addresses' not in server_tmp:\n server_tmp['addresses'] = {}\n for network in server_tmp['addresses']:\n for address in server_tmp['addresses'][network]:\n if salt.utils.cloud.is_public_ip(address.get('addr', '')):\n public.append(address['addr'])\n elif ':' in address['addr']:\n public.append(address['addr'])\n elif '.' in address['addr']:\n private.append(address['addr'])\n\n if server_tmp['accessIPv4']:\n if salt.utils.cloud.is_public_ip(server_tmp['accessIPv4']):\n public.append(server_tmp['accessIPv4'])\n else:\n private.append(server_tmp['accessIPv4'])\n if server_tmp['accessIPv6']:\n public.append(server_tmp['accessIPv6'])\n\n ret[server] = {\n 'id': server_tmp['id'],\n 'image': server_tmp['image']['id'],\n 'size': server_tmp['flavor']['id'],\n 'state': server_tmp['state'],\n 'private_ips': private,\n 'public_ips': public,\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of the VMs that are in this location", "response": "def list_nodes_full(call=None, **kwargs):\n '''\n Return a list of the VMs that in this location\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n (\n 'The list_nodes_full function must be called with'\n ' -f or --function.'\n )\n )\n\n ret = {}\n conn = get_conn()\n server_list = conn.server_list()\n\n if not server_list:\n return {}\n for server in server_list:\n try:\n ret[server] = conn.server_show_libcloud(\n server_list[server]['id']\n ).__dict__\n except IndexError as exc:\n ret = {}\n\n __utils__['cloud.cache_node_list'](ret, __active_provider_name__.split(':')[0], __opts__)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of the VMs that are in this location", "response": "def list_nodes_min(call=None, **kwargs):\n '''\n Return a list of the VMs that in this location\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n (\n 'The list_nodes_min function must be called with'\n ' -f or --function.'\n )\n )\n\n conn = get_conn()\n server_list = conn.server_list_min()\n\n if not server_list:\n return {}\n return server_list"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef volume_create(name, size=100, snapshot=None, voltype=None, **kwargs):\n '''\n Create block storage device\n '''\n conn = get_conn()\n create_kwargs = {'name': name,\n 'size': size,\n 'snapshot': snapshot,\n 'voltype': voltype}\n create_kwargs['availability_zone'] = kwargs.get('availability_zone', None)\n return conn.volume_create(**create_kwargs)", "response": "Create block storage device"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef volume_attach(name, server_name, device='/dev/xvdb', **kwargs):\n '''\n Attach block volume\n '''\n conn = get_conn()\n return conn.volume_attach(\n name,\n server_name,\n device,\n timeout=300\n )", "response": "Attach a block volume to a server"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef volume_create_attach(name, call=None, **kwargs):\n '''\n Create and attach volumes to created node\n '''\n if call == 'function':\n raise SaltCloudSystemExit(\n 'The create_attach_volumes action must be called with '\n '-a or --action.'\n )\n\n if type(kwargs['volumes']) is str:\n volumes = salt.utils.yaml.safe_load(kwargs['volumes'])\n else:\n volumes = kwargs['volumes']\n\n ret = []\n for volume in volumes:\n created = False\n\n volume_dict = {\n 'name': volume['name'],\n }\n if 'volume_id' in volume:\n volume_dict['volume_id'] = volume['volume_id']\n elif 'snapshot' in volume:\n volume_dict['snapshot'] = volume['snapshot']\n else:\n volume_dict['size'] = volume['size']\n\n if 'type' in volume:\n volume_dict['type'] = volume['type']\n if 'iops' in volume:\n volume_dict['iops'] = volume['iops']\n\n if 'id' not in volume_dict:\n created_volume = create_volume(**volume_dict)\n created = True\n volume_dict.update(created_volume)\n\n attach = attach_volume(\n name=volume['name'],\n server_name=name,\n device=volume.get('device', None),\n call='action'\n )\n\n if attach:\n msg = (\n '{0} attached to {1} (aka {2})'.format(\n volume_dict['id'],\n name,\n volume_dict['name'],\n )\n )\n log.info(msg)\n ret.append(msg)\n return ret", "response": "Create and attach volumes to a node"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nallocating a floating IP and return the resource", "response": "def floating_ip_create(kwargs, call=None):\n '''\n Allocate a floating IP\n\n .. versionadded:: 2016.3.0\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The floating_ip_create action must be called with -f or --function'\n )\n\n if 'pool' not in kwargs:\n log.error('pool is required')\n return False\n\n conn = get_conn()\n return conn.floating_ip_create(kwargs['pool'])"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nassociate a floating IP address to a server", "response": "def floating_ip_associate(name, kwargs, call=None):\n '''\n Associate a floating IP address to a server\n\n .. versionadded:: 2016.3.0\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The floating_ip_associate action must be called with -a of --action.'\n )\n\n if 'floating_ip' not in kwargs:\n log.error('floating_ip is required')\n return False\n\n conn = get_conn()\n conn.floating_ip_associate(name, kwargs['floating_ip'])\n return list_nodes()[name]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create(vm_):\n '''\n Create a single VM from a data dict\n '''\n try:\n # Check for required profile parameters before sending any API calls.\n if vm_['profile'] and config.is_profile_configured(__opts__,\n __active_provider_name__ or 'gogrid',\n vm_['profile'],\n vm_=vm_) is False:\n return False\n except AttributeError:\n pass\n\n __utils__['cloud.fire_event'](\n 'event',\n 'starting create',\n 'salt/cloud/{0}/creating'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('creating', vm_, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n if len(vm_['name']) > 20:\n raise SaltCloudException('VM names must not be longer than 20 characters')\n\n log.info('Creating Cloud VM %s', vm_['name'])\n image_id = avail_images()[vm_['image']]['id']\n if 'assign_public_ip' in vm_:\n host_ip = vm_['assign_public_ip']\n else:\n public_ips = list_public_ips()\n if not public_ips:\n raise SaltCloudException('No more IPs available')\n host_ip = next(iter(public_ips))\n\n create_kwargs = {\n 'name': vm_['name'],\n 'image': image_id,\n 'ram': vm_['size'],\n 'ip': host_ip,\n }\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n args={\n 'kwargs': __utils__['cloud.filter_event']('requesting', create_kwargs, list(create_kwargs)),\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n try:\n data = _query('grid', 'server/add', args=create_kwargs)\n except Exception:\n log.error(\n 'Error creating %s on GOGRID\\n\\n'\n 'The following exception was thrown when trying to '\n 'run the initial deployment:\\n', vm_['name'],\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n\n ssh_username = config.get_cloud_config_value(\n 'ssh_username', vm_, __opts__, default='root'\n )\n\n def wait_for_apipass():\n '''\n Wait for the password to become available, via the API\n '''\n try:\n passwords = list_passwords()\n return passwords[vm_['name']][0]['password']\n except KeyError:\n pass\n time.sleep(5)\n return False\n\n vm_['password'] = salt.utils.cloud.wait_for_fun(\n wait_for_apipass,\n timeout=config.get_cloud_config_value(\n 'wait_for_fun_timeout', vm_, __opts__, default=15 * 60),\n )\n\n vm_['ssh_host'] = host_ip\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n ret.update(data)\n\n log.info('Created Cloud VM \\'%s\\'', vm_['name'])\n log.debug(\n '\\'%s\\' VM creation details:\\n%s',\n vm_['name'], pprint.pformat(data)\n )\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('created', vm_, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n return ret", "response": "Create a single VM from a data dict"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of nodes in the cloud provider", "response": "def list_nodes(full=False, call=None):\n '''\n List of nodes, keeping only a brief listing\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -Q\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called with -f or --function.'\n )\n\n ret = {}\n nodes = list_nodes_full('function')\n if full:\n return nodes\n\n for node in nodes:\n ret[node] = {}\n for item in ('id', 'image', 'size', 'public_ips', 'private_ips', 'state'):\n ret[node][item] = nodes[node][item]\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_nodes_full(call=None):\n '''\n List nodes, with all available information\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -F\n '''\n response = _query('grid', 'server/list')\n\n ret = {}\n for item in response['list']:\n name = item['name']\n ret[name] = item\n\n ret[name]['image_info'] = item['image']\n ret[name]['image'] = item['image']['friendlyName']\n ret[name]['size'] = item['ram']['name']\n ret[name]['public_ips'] = [item['ip']['ip']]\n ret[name]['private_ips'] = []\n ret[name]['state_info'] = item['state']\n if 'active' in item['state']['description']:\n ret[name]['state'] = 'RUNNING'\n\n return ret", "response": "List all available nodes"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a dict of available locations", "response": "def avail_locations():\n '''\n Available locations\n '''\n response = list_common_lookups(kwargs={'lookup': 'ip.datacenter'})\n\n ret = {}\n for item in response['list']:\n name = item['name']\n ret[name] = item\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef avail_sizes():\n '''\n Available sizes\n '''\n response = list_common_lookups(kwargs={'lookup': 'server.ram'})\n\n ret = {}\n for item in response['list']:\n name = item['name']\n ret[name] = item\n\n return ret", "response": "Returns a dict of available sizes for the current server."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget a list of available images", "response": "def avail_images():\n '''\n Available images\n '''\n response = _query('grid', 'image/list')\n\n ret = {}\n for item in response['list']:\n name = item['friendlyName']\n ret[name] = item\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists all passwords on the account.", "response": "def list_passwords(kwargs=None, call=None):\n '''\n List all password on the account\n\n .. versionadded:: 2015.8.0\n '''\n response = _query('support', 'password/list')\n\n ret = {}\n for item in response['list']:\n if 'server' in item:\n server = item['server']['name']\n if server not in ret:\n ret[server] = []\n ret[server].append(item)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlists all available public IPs.", "response": "def list_public_ips(kwargs=None, call=None):\n '''\n List all available public IPs.\n\n CLI Example:\n .. code-block:: bash\n\n salt-cloud -f list_public_ips \n\n To list unavailable (assigned) IPs, use:\n\n CLI Example:\n .. code-block:: bash\n\n salt-cloud -f list_public_ips state=assigned\n\n .. versionadded:: 2015.8.0\n '''\n if kwargs is None:\n kwargs = {}\n\n args = {}\n if 'state' in kwargs:\n if kwargs['state'] == 'assigned':\n args['ip.state'] = 'Assigned'\n else:\n args['ip.state'] = 'Unassigned'\n else:\n args['ip.state'] = 'Unassigned'\n\n args['ip.type'] = 'Public'\n\n response = _query('grid', 'ip/list', args=args)\n\n ret = {}\n for item in response['list']:\n name = item['ip']\n ret[name] = item\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_common_lookups(kwargs=None, call=None):\n '''\n List common lookups for a particular type of item\n\n .. versionadded:: 2015.8.0\n '''\n if kwargs is None:\n kwargs = {}\n\n args = {}\n if 'lookup' in kwargs:\n args['lookup'] = kwargs['lookup']\n\n response = _query('common', 'lookup/list', args=args)\n\n return response", "response": "List common lookups for a particular type of item."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef show_instance(name, call=None):\n '''\n Start a machine by name\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a show_instance vm_name\n\n .. versionadded:: 2015.8.0\n '''\n response = _query('grid', 'server/get', args={'name': name})\n ret = {}\n for item in response['list']:\n name = item['name']\n ret[name] = item\n\n ret[name]['image_info'] = item['image']\n ret[name]['image'] = item['image']['friendlyName']\n ret[name]['size'] = item['ram']['name']\n ret[name]['public_ips'] = [item['ip']['ip']]\n ret[name]['private_ips'] = []\n ret[name]['state_info'] = item['state']\n if 'active' in item['state']['description']:\n ret[name]['state'] = 'RUNNING'\n return ret", "response": "Show the details of a single virtual machine by name"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _query(action=None,\n command=None,\n args=None,\n method='GET',\n header_dict=None,\n data=None):\n '''\n Make a web call to GoGrid\n\n .. versionadded:: 2015.8.0\n '''\n vm_ = get_configured_provider()\n apikey = config.get_cloud_config_value(\n 'apikey', vm_, __opts__, search_global=False\n )\n sharedsecret = config.get_cloud_config_value(\n 'sharedsecret', vm_, __opts__, search_global=False\n )\n\n path = 'https://api.gogrid.com/api/'\n\n if action:\n path += action\n\n if command:\n path += '/{0}'.format(command)\n\n log.debug('GoGrid URL: %s', path)\n\n if not isinstance(args, dict):\n args = {}\n\n epoch = six.text_type(int(time.time()))\n hashtext = ''.join((apikey, sharedsecret, epoch))\n args['sig'] = salt.utils.hashutils.md5_digest(hashtext)\n args['format'] = 'json'\n args['v'] = '1.0'\n args['api_key'] = apikey\n\n if header_dict is None:\n header_dict = {}\n\n if method != 'POST':\n header_dict['Accept'] = 'application/json'\n\n decode = True\n if method == 'DELETE':\n decode = False\n\n return_content = None\n result = salt.utils.http.query(\n path,\n method,\n params=args,\n data=data,\n header_dict=header_dict,\n decode=decode,\n decode_type='json',\n text=True,\n status=True,\n opts=__opts__,\n )\n log.debug('GoGrid Response Status Code: %s', result['status'])\n\n return result['dict']", "response": "Make a web call to GoGrid s API"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _parse_key(key):\n '''\n split the hive from the key\n '''\n splt = key.split(\"\\\\\")\n hive = splt.pop(0)\n key = '\\\\'.join(splt)\n return hive, key", "response": "parse the hive from the key"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef key_absent(name, use_32bit_registry=False):\n r'''\n .. versionadded:: 2015.5.4\n\n Ensure a registry key is removed. This will remove the key, subkeys, and all\n value entries.\n\n Args:\n\n name (str):\n A string representing the full path to the key to be removed to\n include the hive and the keypath. The hive can be any of the\n following:\n\n - HKEY_LOCAL_MACHINE or HKLM\n - HKEY_CURRENT_USER or HKCU\n - HKEY_USER or HKU\n\n use_32bit_registry (bool):\n Use the 32bit portion of the registry. Applies only to 64bit\n windows. 32bit Windows will ignore this parameter. Default is False.\n\n Returns:\n dict: A dictionary showing the results of the registry operation.\n\n\n CLI Example:\n\n The following example will delete the ``SOFTWARE\\DeleteMe`` key in the\n ``HKEY_LOCAL_MACHINE`` hive including all its subkeys and value pairs.\n\n .. code-block:: yaml\n\n remove_key_demo:\n reg.key_absent:\n - name: HKEY_CURRENT_USER\\SOFTWARE\\DeleteMe\n\n In the above example the path is interpreted as follows:\n\n - ``HKEY_CURRENT_USER`` is the hive\n - ``SOFTWARE\\DeleteMe`` is the key\n '''\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n hive, key = _parse_key(name)\n\n # Determine what to do\n if not __utils__['reg.read_value'](hive=hive,\n key=key,\n use_32bit_registry=use_32bit_registry)['success']:\n ret['comment'] = '{0} is already absent'.format(name)\n return ret\n\n ret['changes'] = {\n 'reg': {\n 'Removed': {\n 'Key': r'{0}\\{1}'.format(hive, key)}}}\n\n # Check for test option\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n # Delete the value\n __utils__['reg.delete_key_recursive'](hive=hive,\n key=key,\n use_32bit_registry=use_32bit_registry)\n if __utils__['reg.read_value'](hive=hive,\n key=key,\n use_32bit_registry=use_32bit_registry)['success']:\n ret['result'] = False\n ret['changes'] = {}\n ret['comment'] = 'Failed to remove registry key {0}'.format(name)\n\n return ret", "response": "r Ensure a key is absent in the registry."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_salt_call(*dirs, **namespaces):\n '''\n Return salt-call source, based on configuration.\n This will include additional namespaces for another versions of Salt,\n if needed (e.g. older interpreters etc).\n\n :dirs: List of directories to include in the system path\n :namespaces: Dictionary of namespace\n :return:\n '''\n template = '''# -*- coding: utf-8 -*-\nimport os\nimport sys\n\n# Namespaces is a map: {namespace: major/minor version}, like {'2016.11.4': [2, 6]}\n# Appears only when configured in Master configuration.\nnamespaces = %namespaces%\n\n# Default system paths alongside the namespaces\nsyspaths = %dirs%\nsyspaths.append('py{0}'.format(sys.version_info[0]))\n\ncurr_ver = (sys.version_info[0], sys.version_info[1],)\n\nnamespace = ''\nfor ns in namespaces:\n if curr_ver == tuple(namespaces[ns]):\n namespace = ns\n break\n\nfor base in syspaths:\n sys.path.insert(0, os.path.join(os.path.dirname(__file__),\n namespace and os.path.join(namespace, base) or base))\n\nif __name__ == '__main__':\n from salt.scripts import salt_call\n salt_call()\n'''\n\n for tgt, cnt in [('%dirs%', dirs), ('%namespaces%', namespaces)]:\n template = template.replace(tgt, salt.utils.json.dumps(cnt))\n\n return salt.utils.stringutils.to_bytes(template)", "response": "Return salt - call source based on configuration."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds a dependency to the top list.", "response": "def _add_dependency(container, obj):\n '''\n Add a dependency to the top list.\n\n :param obj:\n :param is_file:\n :return:\n '''\n if os.path.basename(obj.__file__).split('.')[0] == '__init__':\n container.append(os.path.dirname(obj.__file__))\n else:\n container.append(obj.__file__.replace('.pyc', '.py'))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the alternative Python interpreter from within _get_tops function.", "response": "def gte():\n '''\n This function is called externally from the alternative\n Python interpreter from within _get_tops function.\n\n :param extra_mods:\n :param so_mods:\n :return:\n '''\n extra = salt.utils.json.loads(sys.argv[1])\n tops = get_tops(**extra)\n\n return salt.utils.json.dumps(tops, ensure_ascii=False)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the top directories for the dependencies based on external configuration.", "response": "def get_ext_tops(config):\n '''\n Get top directories for the dependencies, based on external configuration.\n\n :return:\n '''\n config = copy.deepcopy(config)\n alternatives = {}\n required = ['jinja2', 'yaml', 'tornado', 'msgpack']\n tops = []\n for ns, cfg in salt.ext.six.iteritems(config or {}):\n alternatives[ns] = cfg\n locked_py_version = cfg.get('py-version')\n err_msg = None\n if not locked_py_version:\n err_msg = 'Alternative Salt library: missing specific locked Python version'\n elif not isinstance(locked_py_version, (tuple, list)):\n err_msg = ('Alternative Salt library: specific locked Python version '\n 'should be a list of major/minor version')\n if err_msg:\n raise salt.exceptions.SaltSystemExit(err_msg)\n\n if cfg.get('dependencies') == 'inherit':\n # TODO: implement inheritance of the modules from _here_\n raise NotImplementedError('This feature is not yet implemented')\n else:\n for dep in cfg.get('dependencies'):\n mod = cfg['dependencies'][dep] or ''\n if not mod:\n log.warning('Module %s has missing configuration', dep)\n continue\n elif mod.endswith('.py') and not os.path.isfile(mod):\n log.warning('Module %s configured with not a file or does not exist: %s', dep, mod)\n continue\n elif not mod.endswith('.py') and not os.path.isfile(os.path.join(mod, '__init__.py')):\n log.warning('Module %s is not a Python importable module with %s', dep, mod)\n continue\n tops.append(mod)\n\n if dep in required:\n required.pop(required.index(dep))\n\n required = ', '.join(required)\n if required:\n msg = 'Missing dependencies for the alternative version' \\\n ' in the external configuration: {}'.format(required)\n log.error(msg)\n raise salt.exceptions.SaltSystemExit(msg)\n alternatives[ns]['dependencies'] = tops\n return alternatives"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_ext_namespaces(config):\n '''\n Get namespaces from the existing configuration.\n\n :param config:\n :return:\n '''\n namespaces = {}\n if not config:\n return namespaces\n\n for ns in config:\n constraint_version = tuple(config[ns].get('py-version', []))\n if not constraint_version:\n raise salt.exceptions.SaltSystemExit(\"An alternative version is configured, but not defined \"\n \"to what Python's major/minor version it should be constrained.\")\n else:\n namespaces[ns] = constraint_version\n\n return namespaces", "response": "Get the namespaces from the existing configuration."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_tops(extra_mods='', so_mods=''):\n '''\n Get top directories for the dependencies, based on Python interpreter.\n\n :param extra_mods:\n :param so_mods:\n :return:\n '''\n tops = []\n for mod in [salt, jinja2, yaml, tornado, msgpack, certifi, singledispatch, concurrent,\n singledispatch_helpers, ssl_match_hostname, markupsafe, backports_abc]:\n if mod:\n log.debug('Adding module to the tops: \"%s\"', mod.__name__)\n _add_dependency(tops, mod)\n\n for mod in [m for m in extra_mods.split(',') if m]:\n if mod not in locals() and mod not in globals():\n try:\n locals()[mod] = __import__(mod)\n moddir, modname = os.path.split(locals()[mod].__file__)\n base, _ = os.path.splitext(modname)\n if base == '__init__':\n tops.append(moddir)\n else:\n tops.append(os.path.join(moddir, base + '.py'))\n except ImportError as err:\n log.exception(err)\n log.error('Unable to import extra-module \"%s\"', mod)\n\n for mod in [m for m in so_mods.split(',') if m]:\n try:\n locals()[mod] = __import__(mod)\n tops.append(locals()[mod].__file__)\n except ImportError as err:\n log.exception(err)\n log.error('Unable to import so-module \"%s\"', mod)\n\n return tops", "response": "Get the top directories for the dependencies based on Python interpreter."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_supported_py_config(tops, extended_cfg):\n '''\n Based on the Salt SSH configuration, create a YAML configuration\n for the supported Python interpreter versions. This is then written into the thin.tgz\n archive and then verified by salt.client.ssh.ssh_py_shim.get_executable()\n\n Note: Minimum default of 2.x versions is 2.7 and 3.x is 3.0, unless specified in namespaces.\n\n :return:\n '''\n pymap = []\n for py_ver, tops in _six.iteritems(copy.deepcopy(tops)):\n py_ver = int(py_ver)\n if py_ver == 2:\n pymap.append('py2:2:7')\n elif py_ver == 3:\n pymap.append('py3:3:0')\n\n for ns, cfg in _six.iteritems(copy.deepcopy(extended_cfg) or {}):\n pymap.append('{}:{}:{}'.format(ns, *cfg.get('py-version')))\n pymap.append('')\n\n return salt.utils.stringutils.to_bytes(os.linesep.join(pymap))", "response": "Create a YAML configuration for the supported Python interpreter versions."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the tarball name prefixed with. thin -.", "response": "def _get_thintar_prefix(tarname):\n '''\n Make sure thintar temporary name is concurrent and secure.\n\n :param tarname: name of the chosen tarball\n :return: prefixed tarname\n '''\n tfd, tmp_tarname = tempfile.mkstemp(dir=os.path.dirname(tarname), prefix=\".thin-\",\n suffix=\".\" + os.path.basename(tarname).split(\".\", 1)[-1])\n os.close(tfd)\n\n return tmp_tarname"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef gen_thin(cachedir, extra_mods='', overwrite=False, so_mods='',\n python2_bin='python2', python3_bin='python3', absonly=True,\n compress='gzip', extended_cfg=None):\n '''\n Generate the salt-thin tarball and print the location of the tarball\n Optional additional mods to include (e.g. mako) can be supplied as a comma\n delimited string. Permits forcing an overwrite of the output file as well.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run thin.generate\n salt-run thin.generate mako\n salt-run thin.generate mako,wempy 1\n salt-run thin.generate overwrite=1\n '''\n if sys.version_info < (2, 6):\n raise salt.exceptions.SaltSystemExit('The minimum required python version to run salt-ssh is \"2.6\".')\n if compress not in ['gzip', 'zip']:\n log.warning('Unknown compression type: \"%s\". Falling back to \"gzip\" compression.', compress)\n compress = 'gzip'\n\n thindir = os.path.join(cachedir, 'thin')\n if not os.path.isdir(thindir):\n os.makedirs(thindir)\n thintar = os.path.join(thindir, 'thin.' + (compress == 'gzip' and 'tgz' or 'zip'))\n thinver = os.path.join(thindir, 'version')\n pythinver = os.path.join(thindir, '.thin-gen-py-version')\n salt_call = os.path.join(thindir, 'salt-call')\n pymap_cfg = os.path.join(thindir, 'supported-versions')\n code_checksum = os.path.join(thindir, 'code-checksum')\n digest_collector = salt.utils.hashutils.DigestCollector()\n\n with salt.utils.files.fopen(salt_call, 'wb') as fp_:\n fp_.write(_get_salt_call('pyall', **_get_ext_namespaces(extended_cfg)))\n\n if os.path.isfile(thintar):\n if not overwrite:\n if os.path.isfile(thinver):\n with salt.utils.files.fopen(thinver) as fh_:\n overwrite = fh_.read() != salt.version.__version__\n if overwrite is False and os.path.isfile(pythinver):\n with salt.utils.files.fopen(pythinver) as fh_:\n overwrite = fh_.read() != str(sys.version_info[0]) # future lint: disable=blacklisted-function\n else:\n overwrite = True\n\n if overwrite:\n try:\n log.debug('Removing %s archive file', thintar)\n os.remove(thintar)\n except OSError as exc:\n log.error('Error while removing %s file: %s', thintar, exc)\n if os.path.exists(thintar):\n raise salt.exceptions.SaltSystemExit(\n 'Unable to remove {0}. See logs for details.'.format(thintar)\n )\n else:\n return thintar\n if _six.PY3:\n # Let's check for the minimum python 2 version requirement, 2.6\n py_shell_cmd = \"{} -c 'import sys;sys.stdout.write(\\\"%s.%s\\\\n\\\" % sys.version_info[:2]);'\".format(python2_bin)\n cmd = subprocess.Popen(py_shell_cmd, stdout=subprocess.PIPE, shell=True)\n stdout, _ = cmd.communicate()\n if cmd.returncode == 0:\n py2_version = tuple(int(n) for n in stdout.decode('utf-8').strip().split('.'))\n if py2_version < (2, 6):\n raise salt.exceptions.SaltSystemExit(\n 'The minimum required python version to run salt-ssh is \"2.6\".'\n 'The version reported by \"{0}\" is \"{1}\". Please try \"salt-ssh '\n '--python2-bin=\".'.format(python2_bin, stdout.strip()))\n else:\n log.error('Unable to detect Python-2 version')\n log.debug(stdout)\n\n tops_failure_msg = 'Failed %s tops for Python binary %s.'\n tops_py_version_mapping = {}\n tops = get_tops(extra_mods=extra_mods, so_mods=so_mods)\n tops_py_version_mapping[sys.version_info.major] = tops\n\n # Collect tops, alternative to 2.x version\n if _six.PY2 and sys.version_info.major == 2:\n # Get python 3 tops\n py_shell_cmd = \"{0} -c 'import salt.utils.thin as t;print(t.gte())' '{1}'\".format(\n python3_bin, salt.utils.json.dumps({'extra_mods': extra_mods, 'so_mods': so_mods}))\n cmd = subprocess.Popen(py_shell_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\n stdout, stderr = cmd.communicate()\n if cmd.returncode == 0:\n try:\n tops = salt.utils.json.loads(stdout)\n tops_py_version_mapping['3'] = tops\n except ValueError as err:\n log.error(tops_failure_msg, 'parsing', python3_bin)\n log.exception(err)\n else:\n log.error(tops_failure_msg, 'collecting', python3_bin)\n log.debug(stderr)\n\n # Collect tops, alternative to 3.x version\n if _six.PY3 and sys.version_info.major == 3:\n # Get python 2 tops\n py_shell_cmd = \"{0} -c 'import salt.utils.thin as t;print(t.gte())' '{1}'\".format(\n python2_bin, salt.utils.json.dumps({'extra_mods': extra_mods, 'so_mods': so_mods}))\n cmd = subprocess.Popen(py_shell_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\n stdout, stderr = cmd.communicate()\n if cmd.returncode == 0:\n try:\n tops = salt.utils.json.loads(stdout.decode('utf-8'))\n tops_py_version_mapping['2'] = tops\n except ValueError as err:\n log.error(tops_failure_msg, 'parsing', python2_bin)\n log.exception(err)\n else:\n log.error(tops_failure_msg, 'collecting', python2_bin)\n log.debug(stderr)\n\n with salt.utils.files.fopen(pymap_cfg, 'wb') as fp_:\n fp_.write(_get_supported_py_config(tops=tops_py_version_mapping, extended_cfg=extended_cfg))\n\n tmp_thintar = _get_thintar_prefix(thintar)\n if compress == 'gzip':\n tfp = tarfile.open(tmp_thintar, 'w:gz', dereference=True)\n elif compress == 'zip':\n tfp = zipfile.ZipFile(tmp_thintar, 'w', compression=zlib and zipfile.ZIP_DEFLATED or zipfile.ZIP_STORED)\n tfp.add = tfp.write\n\n try: # cwd may not exist if it was removed but salt was run from it\n start_dir = os.getcwd()\n except OSError:\n start_dir = None\n tempdir = None\n\n # Pack default data\n log.debug('Packing default libraries based on current Salt version')\n for py_ver, tops in _six.iteritems(tops_py_version_mapping):\n for top in tops:\n if absonly and not os.path.isabs(top):\n continue\n base = os.path.basename(top)\n top_dirname = os.path.dirname(top)\n if os.path.isdir(top_dirname):\n os.chdir(top_dirname)\n else:\n # This is likely a compressed python .egg\n tempdir = tempfile.mkdtemp()\n egg = zipfile.ZipFile(top_dirname)\n egg.extractall(tempdir)\n top = os.path.join(tempdir, base)\n os.chdir(tempdir)\n\n site_pkg_dir = _is_shareable(base) and 'pyall' or 'py{}'.format(py_ver)\n\n log.debug('Packing \"%s\" to \"%s\" destination', base, site_pkg_dir)\n if not os.path.isdir(top):\n # top is a single file module\n if os.path.exists(os.path.join(top_dirname, base)):\n tfp.add(base, arcname=os.path.join(site_pkg_dir, base))\n continue\n for root, dirs, files in salt.utils.path.os_walk(base, followlinks=True):\n for name in files:\n if not name.endswith(('.pyc', '.pyo')):\n digest_collector.add(os.path.join(root, name))\n arcname = os.path.join(site_pkg_dir, root, name)\n if hasattr(tfp, 'getinfo'):\n try:\n # This is a little slow but there's no clear way to detect duplicates\n tfp.getinfo(os.path.join(site_pkg_dir, root, name))\n arcname = None\n except KeyError:\n log.debug('ZIP: Unable to add \"%s\" with \"getinfo\"', arcname)\n if arcname:\n tfp.add(os.path.join(root, name), arcname=arcname)\n\n if tempdir is not None:\n shutil.rmtree(tempdir)\n tempdir = None\n\n # Pack alternative data\n if extended_cfg:\n log.debug('Packing libraries based on alternative Salt versions')\n for ns, cfg in _six.iteritems(get_ext_tops(extended_cfg)):\n tops = [cfg.get('path')] + cfg.get('dependencies')\n py_ver_major, py_ver_minor = cfg.get('py-version')\n for top in tops:\n base, top_dirname = os.path.basename(top), os.path.dirname(top)\n os.chdir(top_dirname)\n site_pkg_dir = _is_shareable(base) and 'pyall' or 'py{0}'.format(py_ver_major)\n log.debug('Packing alternative \"%s\" to \"%s/%s\" destination', base, ns, site_pkg_dir)\n if not os.path.isdir(top):\n # top is a single file module\n if os.path.exists(os.path.join(top_dirname, base)):\n tfp.add(base, arcname=os.path.join(ns, site_pkg_dir, base))\n continue\n for root, dirs, files in salt.utils.path.os_walk(base, followlinks=True):\n for name in files:\n if not name.endswith(('.pyc', '.pyo')):\n digest_collector.add(os.path.join(root, name))\n arcname = os.path.join(ns, site_pkg_dir, root, name)\n if hasattr(tfp, 'getinfo'):\n try:\n tfp.getinfo(os.path.join(site_pkg_dir, root, name))\n arcname = None\n except KeyError:\n log.debug('ZIP: Unable to add \"%s\" with \"getinfo\"', arcname)\n if arcname:\n tfp.add(os.path.join(root, name), arcname=arcname)\n\n os.chdir(thindir)\n with salt.utils.files.fopen(thinver, 'w+') as fp_:\n fp_.write(salt.version.__version__)\n with salt.utils.files.fopen(pythinver, 'w+') as fp_:\n fp_.write(str(sys.version_info.major)) # future lint: disable=blacklisted-function\n with salt.utils.files.fopen(code_checksum, 'w+') as fp_:\n fp_.write(digest_collector.digest())\n os.chdir(os.path.dirname(thinver))\n\n for fname in ['version', '.thin-gen-py-version', 'salt-call', 'supported-versions', 'code-checksum']:\n tfp.add(fname)\n\n if start_dir:\n os.chdir(start_dir)\n tfp.close()\n\n shutil.move(tmp_thintar, thintar)\n\n return thintar", "response": "Generate a salt - thin tarball and print the location of the tarball."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef thin_sum(cachedir, form='sha1'):\n '''\n Return the checksum of the current thin tarball\n '''\n thintar = gen_thin(cachedir)\n code_checksum_path = os.path.join(cachedir, 'thin', 'code-checksum')\n if os.path.isfile(code_checksum_path):\n with salt.utils.files.fopen(code_checksum_path, 'r') as fh:\n code_checksum = \"'{0}'\".format(fh.read().strip())\n else:\n code_checksum = \"'0'\"\n\n return code_checksum, salt.utils.hashutils.get_hash(thintar, form)", "response": "Return the checksum of the current thin tarball\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngenerate the salt - min tarball and print the location of the tarball.", "response": "def gen_min(cachedir, extra_mods='', overwrite=False, so_mods='',\n python2_bin='python2', python3_bin='python3'):\n '''\n Generate the salt-min tarball and print the location of the tarball\n Optional additional mods to include (e.g. mako) can be supplied as a comma\n delimited string. Permits forcing an overwrite of the output file as well.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run min.generate\n salt-run min.generate mako\n salt-run min.generate mako,wempy 1\n salt-run min.generate overwrite=1\n '''\n mindir = os.path.join(cachedir, 'min')\n if not os.path.isdir(mindir):\n os.makedirs(mindir)\n mintar = os.path.join(mindir, 'min.tgz')\n minver = os.path.join(mindir, 'version')\n pyminver = os.path.join(mindir, '.min-gen-py-version')\n salt_call = os.path.join(mindir, 'salt-call')\n with salt.utils.files.fopen(salt_call, 'wb') as fp_:\n fp_.write(_get_salt_call())\n if os.path.isfile(mintar):\n if not overwrite:\n if os.path.isfile(minver):\n with salt.utils.files.fopen(minver) as fh_:\n overwrite = fh_.read() != salt.version.__version__\n if overwrite is False and os.path.isfile(pyminver):\n with salt.utils.files.fopen(pyminver) as fh_:\n overwrite = fh_.read() != str(sys.version_info[0]) # future lint: disable=blacklisted-function\n else:\n overwrite = True\n\n if overwrite:\n try:\n os.remove(mintar)\n except OSError:\n pass\n else:\n return mintar\n if _six.PY3:\n # Let's check for the minimum python 2 version requirement, 2.6\n py_shell_cmd = (\n python2_bin + ' -c \\'from __future__ import print_function; import sys; '\n 'print(\"{0}.{1}\".format(*(sys.version_info[:2])));\\''\n )\n cmd = subprocess.Popen(py_shell_cmd, stdout=subprocess.PIPE, shell=True)\n stdout, _ = cmd.communicate()\n if cmd.returncode == 0:\n py2_version = tuple(int(n) for n in stdout.decode('utf-8').strip().split('.'))\n if py2_version < (2, 6):\n # Bail!\n raise salt.exceptions.SaltSystemExit(\n 'The minimum required python version to run salt-ssh is \"2.6\".'\n 'The version reported by \"{0}\" is \"{1}\". Please try \"salt-ssh '\n '--python2-bin=\".'.format(python2_bin,\n stdout.strip())\n )\n elif sys.version_info < (2, 6):\n # Bail! Though, how did we reached this far in the first place.\n raise salt.exceptions.SaltSystemExit(\n 'The minimum required python version to run salt-ssh is \"2.6\".'\n )\n\n tops_py_version_mapping = {}\n tops = get_tops(extra_mods=extra_mods, so_mods=so_mods)\n if _six.PY2:\n tops_py_version_mapping['2'] = tops\n else:\n tops_py_version_mapping['3'] = tops\n\n # TODO: Consider putting known py2 and py3 compatible libs in it's own sharable directory.\n # This would reduce the min size.\n if _six.PY2 and sys.version_info[0] == 2:\n # Get python 3 tops\n py_shell_cmd = (\n python3_bin + ' -c \\'import sys; import json; import salt.utils.thin; '\n 'print(json.dumps(salt.utils.thin.get_tops(**(json.loads(sys.argv[1]))), ensure_ascii=False)); exit(0);\\' '\n '\\'{0}\\''.format(salt.utils.json.dumps({'extra_mods': extra_mods, 'so_mods': so_mods}))\n )\n cmd = subprocess.Popen(py_shell_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\n stdout, stderr = cmd.communicate()\n if cmd.returncode == 0:\n try:\n tops = salt.utils.json.loads(stdout)\n tops_py_version_mapping['3'] = tops\n except ValueError:\n pass\n if _six.PY3 and sys.version_info[0] == 3:\n # Get python 2 tops\n py_shell_cmd = (\n python2_bin + ' -c \\'from __future__ import print_function; '\n 'import sys; import json; import salt.utils.thin; '\n 'print(json.dumps(salt.utils.thin.get_tops(**(json.loads(sys.argv[1]))), ensure_ascii=False)); exit(0);\\' '\n '\\'{0}\\''.format(salt.utils.json.dumps({'extra_mods': extra_mods, 'so_mods': so_mods}))\n )\n cmd = subprocess.Popen(py_shell_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\n stdout, stderr = cmd.communicate()\n if cmd.returncode == 0:\n try:\n tops = salt.utils.json.loads(stdout.decode('utf-8'))\n tops_py_version_mapping['2'] = tops\n except ValueError:\n pass\n\n tfp = tarfile.open(mintar, 'w:gz', dereference=True)\n try: # cwd may not exist if it was removed but salt was run from it\n start_dir = os.getcwd()\n except OSError:\n start_dir = None\n tempdir = None\n\n # This is the absolute minimum set of files required to run salt-call\n min_files = (\n 'salt/__init__.py',\n 'salt/utils',\n 'salt/utils/__init__.py',\n 'salt/utils/atomicfile.py',\n 'salt/utils/validate',\n 'salt/utils/validate/__init__.py',\n 'salt/utils/validate/path.py',\n 'salt/utils/decorators',\n 'salt/utils/decorators/__init__.py',\n 'salt/utils/cache.py',\n 'salt/utils/xdg.py',\n 'salt/utils/odict.py',\n 'salt/utils/minions.py',\n 'salt/utils/dicttrim.py',\n 'salt/utils/sdb.py',\n 'salt/utils/migrations.py',\n 'salt/utils/files.py',\n 'salt/utils/parsers.py',\n 'salt/utils/locales.py',\n 'salt/utils/lazy.py',\n 'salt/utils/s3.py',\n 'salt/utils/dictupdate.py',\n 'salt/utils/verify.py',\n 'salt/utils/args.py',\n 'salt/utils/kinds.py',\n 'salt/utils/xmlutil.py',\n 'salt/utils/debug.py',\n 'salt/utils/jid.py',\n 'salt/utils/openstack',\n 'salt/utils/openstack/__init__.py',\n 'salt/utils/openstack/swift.py',\n 'salt/utils/asynchronous.py',\n 'salt/utils/process.py',\n 'salt/utils/jinja.py',\n 'salt/utils/rsax931.py',\n 'salt/utils/context.py',\n 'salt/utils/minion.py',\n 'salt/utils/error.py',\n 'salt/utils/aws.py',\n 'salt/utils/timed_subprocess.py',\n 'salt/utils/zeromq.py',\n 'salt/utils/schedule.py',\n 'salt/utils/url.py',\n 'salt/utils/yamlencoding.py',\n 'salt/utils/network.py',\n 'salt/utils/http.py',\n 'salt/utils/gzip_util.py',\n 'salt/utils/vt.py',\n 'salt/utils/templates.py',\n 'salt/utils/aggregation.py',\n 'salt/utils/yaml.py',\n 'salt/utils/yamldumper.py',\n 'salt/utils/yamlloader.py',\n 'salt/utils/event.py',\n 'salt/utils/state.py',\n 'salt/serializers',\n 'salt/serializers/__init__.py',\n 'salt/serializers/yamlex.py',\n 'salt/template.py',\n 'salt/_compat.py',\n 'salt/loader.py',\n 'salt/client',\n 'salt/client/__init__.py',\n 'salt/ext',\n 'salt/ext/__init__.py',\n 'salt/ext/six.py',\n 'salt/ext/ipaddress.py',\n 'salt/version.py',\n 'salt/syspaths.py',\n 'salt/defaults',\n 'salt/defaults/__init__.py',\n 'salt/defaults/exitcodes.py',\n 'salt/renderers',\n 'salt/renderers/__init__.py',\n 'salt/renderers/jinja.py',\n 'salt/renderers/yaml.py',\n 'salt/modules',\n 'salt/modules/__init__.py',\n 'salt/modules/test.py',\n 'salt/modules/selinux.py',\n 'salt/modules/cmdmod.py',\n 'salt/modules/saltutil.py',\n 'salt/minion.py',\n 'salt/pillar',\n 'salt/pillar/__init__.py',\n 'salt/textformat.py',\n 'salt/log',\n 'salt/log/__init__.py',\n 'salt/log/handlers',\n 'salt/log/handlers/__init__.py',\n 'salt/log/mixins.py',\n 'salt/log/setup.py',\n 'salt/cli',\n 'salt/cli/__init__.py',\n 'salt/cli/caller.py',\n 'salt/cli/daemons.py',\n 'salt/cli/salt.py',\n 'salt/cli/call.py',\n 'salt/fileserver',\n 'salt/fileserver/__init__.py',\n 'salt/transport',\n 'salt/transport/__init__.py',\n 'salt/transport/client.py',\n 'salt/exceptions.py',\n 'salt/grains',\n 'salt/grains/__init__.py',\n 'salt/grains/extra.py',\n 'salt/scripts.py',\n 'salt/state.py',\n 'salt/fileclient.py',\n 'salt/crypt.py',\n 'salt/config.py',\n 'salt/beacons',\n 'salt/beacons/__init__.py',\n 'salt/payload.py',\n 'salt/output',\n 'salt/output/__init__.py',\n 'salt/output/nested.py',\n )\n\n for py_ver, tops in _six.iteritems(tops_py_version_mapping):\n for top in tops:\n base = os.path.basename(top)\n top_dirname = os.path.dirname(top)\n if os.path.isdir(top_dirname):\n os.chdir(top_dirname)\n else:\n # This is likely a compressed python .egg\n tempdir = tempfile.mkdtemp()\n egg = zipfile.ZipFile(top_dirname)\n egg.extractall(tempdir)\n top = os.path.join(tempdir, base)\n os.chdir(tempdir)\n if not os.path.isdir(top):\n # top is a single file module\n tfp.add(base, arcname=os.path.join('py{0}'.format(py_ver), base))\n continue\n for root, dirs, files in salt.utils.path.os_walk(base, followlinks=True):\n for name in files:\n if name.endswith(('.pyc', '.pyo')):\n continue\n if root.startswith('salt') and os.path.join(root, name) not in min_files:\n continue\n tfp.add(os.path.join(root, name),\n arcname=os.path.join('py{0}'.format(py_ver), root, name))\n if tempdir is not None:\n shutil.rmtree(tempdir)\n tempdir = None\n\n os.chdir(mindir)\n tfp.add('salt-call')\n with salt.utils.files.fopen(minver, 'w+') as fp_:\n fp_.write(salt.version.__version__)\n with salt.utils.files.fopen(pyminver, 'w+') as fp_:\n fp_.write(str(sys.version_info[0])) # future lint: disable=blacklisted-function\n os.chdir(os.path.dirname(minver))\n tfp.add('version')\n tfp.add('.min-gen-py-version')\n if start_dir:\n os.chdir(start_dir)\n tfp.close()\n return mintar"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef min_sum(cachedir, form='sha1'):\n '''\n Return the checksum of the current thin tarball\n '''\n mintar = gen_min(cachedir)\n return salt.utils.hashutils.get_hash(mintar, form)", "response": "Return the checksum of the current thin tarball"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrefreshes the remote repos database.", "response": "def refresh_db(full=False, **kwargs):\n '''\n Updates the remote repos database.\n\n full : False\n\n Set to ``True`` to force a refresh of the pkg DB from all publishers,\n regardless of the last refresh time.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.refresh_db\n salt '*' pkg.refresh_db full=True\n '''\n # Remove rtag file to keep multiple refreshes from happening in pkg states\n salt.utils.pkg.clear_rtag(__opts__)\n if full:\n return __salt__['cmd.retcode']('/bin/pkg refresh --full') == 0\n else:\n return __salt__['cmd.retcode']('/bin/pkg refresh') == 0"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef upgrade_available(name, **kwargs):\n '''\n Check if there is an upgrade available for a certain package\n Accepts full or partial FMRI. Returns all matches found.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade_available apache-22\n '''\n version = None\n cmd = ['pkg', 'list', '-Huv', name]\n lines = __salt__['cmd.run_stdout'](cmd).splitlines()\n if not lines:\n return {}\n ret = {}\n for line in lines:\n ret[_ips_get_pkgname(line)] = _ips_get_pkgversion(line)\n return ret", "response": "Check if there is an upgrade available for a certain package. Returns all matches found"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_upgrades(refresh=True, **kwargs): # pylint: disable=W0613\n '''\n Lists all packages available for update.\n\n When run in global zone, it reports only upgradable packages for the global\n zone.\n\n When run in non-global zone, it can report more upgradable packages than\n ``pkg update -vn``, because ``pkg update`` hides packages that require\n newer version of ``pkg://solaris/entire`` (which means that they can be\n upgraded only from the global zone). If ``pkg://solaris/entire`` is found\n in the list of upgrades, then the global zone should be updated to get all\n possible updates. Use ``refresh=True`` to refresh the package database.\n\n refresh : True\n Runs a full package database refresh before listing. Set to ``False`` to\n disable running the refresh.\n\n .. versionchanged:: 2017.7.0\n\n In previous versions of Salt, ``refresh`` defaulted to ``False``. This was\n changed to default to ``True`` in the 2017.7.0 release to make the behavior\n more consistent with the other package modules, which all default to ``True``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_upgrades\n salt '*' pkg.list_upgrades refresh=False\n '''\n if salt.utils.data.is_true(refresh):\n refresh_db(full=True)\n upgrades = {}\n # awk is in core-os package so we can use it without checking\n lines = __salt__['cmd.run_stdout'](\"/bin/pkg list -Huv\").splitlines()\n for line in lines:\n upgrades[_ips_get_pkgname(line)] = _ips_get_pkgversion(line)\n return upgrades", "response": "List all packages available for update."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef upgrade(refresh=False, **kwargs):\n '''\n Upgrade all packages to the latest possible version.\n When run in global zone, it updates also all non-global zones.\n In non-global zones upgrade is limited by dependency constrains linked to\n the version of pkg://solaris/entire.\n\n Returns a dictionary containing the changes:\n\n .. code-block:: python\n\n {'': {'old': '',\n 'new': ''}}\n\n When there is a failure, an explanation is also included in the error\n message, based on the return code of the ``pkg update`` command.\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade\n '''\n if salt.utils.data.is_true(refresh):\n refresh_db()\n\n # Get a list of the packages before install so we can diff after to see\n # what got installed.\n old = list_pkgs()\n\n # Install or upgrade the package\n # If package is already installed\n cmd = ['pkg', 'update', '-v', '--accept']\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret = salt.utils.data.compare_dicts(old, new)\n\n if result['retcode'] != 0:\n raise CommandExecutionError(\n 'Problem encountered upgrading packages',\n info={'changes': ret,\n 'retcode': ips_pkg_return_values[result['retcode']],\n 'result': result}\n )\n\n return ret", "response": "Upgrade all packages to the latest possible version."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist the currently installed packages as a dict.", "response": "def list_pkgs(versions_as_list=False, **kwargs):\n '''\n List the currently installed packages as a dict::\n\n {'': ''}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_pkgs\n '''\n # not yet implemented or not applicable\n if any([salt.utils.data.is_true(kwargs.get(x))\n for x in ('removed', 'purge_desired')]):\n return {}\n\n if 'pkg.list_pkgs' in __context__:\n if versions_as_list:\n return __context__['pkg.list_pkgs']\n else:\n ret = copy.deepcopy(__context__['pkg.list_pkgs'])\n __salt__['pkg_resource.stringify'](ret)\n return ret\n\n ret = {}\n cmd = '/bin/pkg list -Hv'\n lines = __salt__['cmd.run_stdout'](cmd).splitlines()\n # column 1 is full FMRI name in form pkg://publisher/class/name@version\n for line in lines:\n name = _ips_get_pkgname(line)\n version = _ips_get_pkgversion(line)\n __salt__['pkg_resource.add_pkg'](ret, name, version)\n\n __salt__['pkg_resource.sort_pkglist'](ret)\n __context__['pkg.list_pkgs'] = copy.deepcopy(ret)\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the version of the specified packages.", "response": "def version(*names, **kwargs):\n '''\n Common interface for obtaining the version of installed packages.\n Accepts full or partial FMRI. If called using pkg_resource, full FMRI is required.\n Partial FMRI is returned if the package is not installed.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.version vim\n salt '*' pkg.version foo bar baz\n salt '*' pkg_resource.version pkg://solaris/entire\n\n '''\n if not names:\n return ''\n\n cmd = ['/bin/pkg', 'list', '-Hv']\n cmd.extend(names)\n lines = __salt__['cmd.run_stdout'](cmd, ignore_retcode=True).splitlines()\n ret = {}\n for line in lines:\n ret[_ips_get_pkgname(line)] = _ips_get_pkgversion(line)\n\n # Append package names which are not installed/found\n unmatched = list([name for name in names if not reduce(lambda x, y: x or name in y, ret, False)]) # pylint: disable=W0640\n ret.update(zip(unmatched, itertools.cycle(('',))))\n\n # Return a string if only one package name passed\n if len(names) == 1:\n try:\n return next(six.itervalues(ret))\n except StopIteration:\n return ''\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_fmri(name, **kwargs):\n '''\n Returns FMRI from partial name. Returns empty string ('') if not found.\n In case of multiple match, the function returns list of all matched packages.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.get_fmri bash\n '''\n if name.startswith('pkg://'):\n # already full fmri\n return name\n cmd = ['/bin/pkg', 'list', '-aHv', name]\n # there can be more packages matching the name\n lines = __salt__['cmd.run_stdout'](cmd).splitlines()\n if not lines:\n # empty string = package not found\n return ''\n ret = []\n for line in lines:\n ret.append(_ips_get_pkgname(line))\n\n return ret", "response": "Returns FMRI from partial name."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nnormalize pkg name to full FMRI before running it.", "response": "def normalize_name(name, **kwargs):\n '''\n Internal function. Normalizes pkg name to full FMRI before running\n pkg.install. In case of multiple matches or no match, it returns the name\n without modifications.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.normalize_name vim\n '''\n if name.startswith('pkg://'):\n # already full fmri\n return name\n cmd = ['/bin/pkg', 'list', '-aHv', name]\n # there can be more packages matching the name\n lines = __salt__['cmd.run_stdout'](cmd).splitlines()\n # if we get more lines, it's multiple match (name not unique)\n # if we get zero lines, pkg is not installed\n # in both ways it's safer to return original (unmodified) name and let \"pkg install\" to deal with it\n if len(lines) != 1:\n return name\n # return pkg name\n return _ips_get_pkgname(lines[0])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsearch the repository for a given pkg name.", "response": "def search(name, versions_as_list=False, **kwargs):\n '''\n Searches the repository for given pkg name.\n The name can be full or partial FMRI. All matches are printed. Globs are\n also supported.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.search bash\n '''\n\n ret = {}\n cmd = ['/bin/pkg', 'list', '-aHv', name]\n out = __salt__['cmd.run_all'](cmd, ignore_retcode=True)\n if out['retcode'] != 0:\n # error = nothing found\n return {}\n # no error, processing pkg listing\n # column 1 is full FMRI name in form pkg://publisher/pkg/name@version\n for line in out['stdout'].splitlines():\n name = _ips_get_pkgname(line)\n version = _ips_get_pkgversion(line)\n __salt__['pkg_resource.add_pkg'](ret, name, version)\n\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninstalling a named package using the IPS pkg command.", "response": "def install(name=None, refresh=False, pkgs=None, version=None, test=False, **kwargs):\n '''\n Install the named package using the IPS pkg command.\n Accepts full or partial FMRI.\n\n Returns a dict containing the new package names and versions::\n\n {'': {'old': '',\n 'new': ''}}\n\n\n Multiple Package Installation Options:\n\n pkgs\n A list of packages to install. Must be passed as a python list.\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install vim\n salt '*' pkg.install pkg://solaris/editor/vim\n salt '*' pkg.install pkg://solaris/editor/vim refresh=True\n salt '*' pkg.install pkgs='[\"foo\", \"bar\"]'\n '''\n if not pkgs:\n if is_installed(name):\n return {}\n\n if refresh:\n refresh_db(full=True)\n\n pkg2inst = ''\n if pkgs: # multiple packages specified\n pkg2inst = []\n for pkg in pkgs:\n if getattr(pkg, 'items', False):\n if list(pkg.items())[0][1]: # version specified\n pkg2inst.append('{0}@{1}'.format(list(pkg.items())[0][0],\n list(pkg.items())[0][1]))\n else:\n pkg2inst.append(list(pkg.items())[0][0])\n else:\n pkg2inst.append(\"{0}\".format(pkg))\n log.debug('Installing these packages instead of %s: %s',\n name, pkg2inst)\n\n else: # install single package\n if version:\n pkg2inst = \"{0}@{1}\".format(name, version)\n else:\n pkg2inst = \"{0}\".format(name)\n\n cmd = ['pkg', 'install', '-v', '--accept']\n if test:\n cmd.append('-n')\n\n # Get a list of the packages before install so we can diff after to see\n # what got installed.\n old = list_pkgs()\n\n # Install or upgrade the package\n # If package is already installed\n if isinstance(pkg2inst, string_types):\n cmd.append(pkg2inst)\n elif isinstance(pkg2inst, list):\n cmd = cmd + pkg2inst\n\n out = __salt__['cmd.run_all'](cmd, output_loglevel='trace')\n\n # Get a list of the packages again, including newly installed ones.\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret = salt.utils.data.compare_dicts(old, new)\n\n if out['retcode'] != 0:\n raise CommandExecutionError(\n 'Error occurred installing package(s)',\n info={\n 'changes': ret,\n 'retcode': ips_pkg_return_values[out['retcode']],\n 'errors': [out['stderr']]\n }\n )\n\n # No error occurred\n if test:\n return 'Test succeeded.'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves a package from the system.", "response": "def remove(name=None, pkgs=None, **kwargs):\n '''\n Remove specified package. Accepts full or partial FMRI.\n In case of multiple match, the command fails and won't modify the OS.\n\n name\n The name of the package to be deleted.\n\n\n Multiple Package Options:\n\n pkgs\n A list of packages to delete. Must be passed as a python list. The\n ``name`` parameter will be ignored if this option is passed.\n\n\n Returns a list containing the removed packages.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.remove \n salt '*' pkg.remove tcsh\n salt '*' pkg.remove pkg://solaris/shell/tcsh\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n '''\n targets = salt.utils.args.split_input(pkgs) if pkgs else [name]\n if not targets:\n return {}\n\n if pkgs:\n log.debug('Removing these packages instead of %s: %s', name, targets)\n\n # Get a list of the currently installed pkgs.\n old = list_pkgs()\n\n # Remove the package(s)\n cmd = ['/bin/pkg', 'uninstall', '-v'] + targets\n out = __salt__['cmd.run_all'](cmd, output_loglevel='trace')\n\n # Get a list of the packages after the uninstall\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret = salt.utils.data.compare_dicts(old, new)\n\n if out['retcode'] != 0:\n raise CommandExecutionError(\n 'Error occurred removing package(s)',\n info={\n 'changes': ret,\n 'retcode': ips_pkg_return_values[out['retcode']],\n 'errors': [out['stderr']]\n }\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nexecuting a command and read the output as JSON", "response": "def ext_pillar(minion_id, # pylint: disable=W0613\n pillar, # pylint: disable=W0613\n command):\n '''\n Execute a command and read the output as JSON\n '''\n try:\n command = command.replace('%s', minion_id)\n return salt.utils.json.loads(__salt__['cmd.run'](command))\n except Exception:\n log.critical('JSON data from %s failed to parse', command)\n return {}"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _tap(tap, runas=None):\n '''\n Add unofficial GitHub repos to the list of formulas that brew tracks,\n updates, and installs from.\n '''\n if tap in _list_taps():\n return True\n\n cmd = 'tap {0}'.format(tap)\n try:\n _call_brew(cmd)\n except CommandExecutionError:\n log.error('Failed to tap \"%s\"', tap)\n return False\n\n return True", "response": "Return True if the given command is successful False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncalls the brew command with the user account of brew", "response": "def _call_brew(cmd, failhard=True):\n '''\n Calls the brew command with the user account of brew\n '''\n user = __salt__['file.get_user'](_homebrew_bin())\n runas = user if user != __opts__['user'] else None\n cmd = '{} {}'.format(salt.utils.path.which('brew'), cmd)\n result = __salt__['cmd.run_all'](cmd,\n runas=runas,\n output_loglevel='trace',\n python_shell=False)\n if failhard and result['retcode'] != 0:\n raise CommandExecutionError('Brew command failed',\n info={'result': result})\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_pkgs(versions_as_list=False, **kwargs):\n '''\n List the packages currently installed in a dict::\n\n {'': ''}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_pkgs\n '''\n versions_as_list = salt.utils.data.is_true(versions_as_list)\n # not yet implemented or not applicable\n if any([salt.utils.data.is_true(kwargs.get(x))\n for x in ('removed', 'purge_desired')]):\n return {}\n\n if 'pkg.list_pkgs' in __context__:\n if versions_as_list:\n return __context__['pkg.list_pkgs']\n else:\n ret = copy.deepcopy(__context__['pkg.list_pkgs'])\n __salt__['pkg_resource.stringify'](ret)\n return ret\n\n ret = {}\n cmd = 'info --json=v1 --installed'\n package_info = salt.utils.json.loads(_call_brew(cmd)['stdout'])\n\n for package in package_info:\n # Brew allows multiple versions of the same package to be installed.\n # Salt allows for this, so it must be accounted for.\n versions = [v['version'] for v in package['installed']]\n # Brew allows for aliasing of packages, all of which will be\n # installable from a Salt call, so all names must be accounted for.\n names = package['aliases'] + [package['name'], package['full_name']]\n # Create a list of tuples containing all possible combinations of\n # names and versions, because all are valid.\n combinations = [(n, v) for n in names for v in versions]\n\n for name, version in combinations:\n __salt__['pkg_resource.add_pkg'](ret, name, version)\n\n # Grab packages from brew cask, if available.\n # Brew Cask doesn't provide a JSON interface, must be parsed the old way.\n try:\n cask_cmd = 'cask list --versions'\n out = _call_brew(cask_cmd)['stdout']\n\n for line in out.splitlines():\n try:\n name_and_versions = line.split(' ')\n name = '/'.join(('caskroom/cask', name_and_versions[0]))\n installed_versions = name_and_versions[1:]\n key_func = functools.cmp_to_key(salt.utils.versions.version_cmp)\n newest_version = sorted(installed_versions, key=key_func).pop()\n except ValueError:\n continue\n __salt__['pkg_resource.add_pkg'](ret, name, newest_version)\n except CommandExecutionError:\n pass\n\n __salt__['pkg_resource.sort_pkglist'](ret)\n __context__['pkg.list_pkgs'] = copy.deepcopy(ret)\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n return ret", "response": "List the packages currently installed in a dict."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef remove(name=None, pkgs=None, **kwargs):\n '''\n Removes packages with ``brew uninstall``.\n\n name\n The name of the package to be deleted.\n\n\n Multiple Package Options:\n\n pkgs\n A list of packages to delete. Must be passed as a python list. The\n ``name`` parameter will be ignored if this option is passed.\n\n .. versionadded:: 0.16.0\n\n\n Returns a dict containing the changes.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.remove \n salt '*' pkg.remove ,,\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n '''\n try:\n pkg_params = __salt__['pkg_resource.parse_targets'](\n name, pkgs, **kwargs\n )[0]\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n old = list_pkgs()\n targets = [x for x in pkg_params if x in old]\n if not targets:\n return {}\n cmd = 'uninstall {0}'.format(' '.join(targets))\n\n out = _call_brew(cmd)\n if out['retcode'] != 0 and out['stderr']:\n errors = [out['stderr']]\n else:\n errors = []\n\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret = salt.utils.data.compare_dicts(old, new)\n\n if errors:\n raise CommandExecutionError(\n 'Problem encountered removing package(s)',\n info={'errors': errors, 'changes': ret}\n )\n\n return ret", "response": "Removes packages with brew uninstall."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef refresh_db(**kwargs):\n '''\n Update the homebrew package repository.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.refresh_db\n '''\n # Remove rtag file to keep multiple refreshes from happening in pkg states\n salt.utils.pkg.clear_rtag(__opts__)\n cmd = 'update'\n if _call_brew(cmd)['retcode']:\n log.error('Failed to update')\n return False\n\n return True", "response": "Update the homebrew package repository."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _info(*pkgs):\n '''\n Get all info brew can provide about a list of packages.\n\n Does not do any kind of processing, so the format depends entirely on\n the output brew gives. This may change if a new version of the format is\n requested.\n\n On failure, returns an empty dict and logs failure.\n On success, returns a dict mapping each item in pkgs to its corresponding\n object in the output of 'brew info'.\n\n Caveat: If one of the packages does not exist, no packages will be\n included in the output.\n '''\n cmd = 'info --json=v1 {0}'.format(' '.join(pkgs))\n brew_result = _call_brew(cmd)\n if brew_result['retcode']:\n log.error('Failed to get info about packages: %s',\n ' '.join(pkgs))\n return {}\n output = salt.utils.json.loads(brew_result['stdout'])\n return dict(zip(pkgs, output))", "response": "Return info about a list of packages."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef install(name=None, pkgs=None, taps=None, options=None, **kwargs):\n '''\n Install the passed package(s) with ``brew install``\n\n name\n The name of the formula to be installed. Note that this parameter is\n ignored if \"pkgs\" is passed.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install \n\n taps\n Unofficial GitHub repos to use when updating and installing formulas.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install tap=''\n salt '*' pkg.install zlib taps='homebrew/dupes'\n salt '*' pkg.install php54 taps='[\"josegonzalez/php\", \"homebrew/dupes\"]'\n\n options\n Options to pass to brew. Only applies to initial install. Due to how brew\n works, modifying chosen options requires a full uninstall followed by a\n fresh install. Note that if \"pkgs\" is used, all options will be passed\n to all packages. Unrecognized options for a package will be silently\n ignored by brew.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install tap=''\n salt '*' pkg.install php54 taps='[\"josegonzalez/php\", \"homebrew/dupes\"]' options='[\"--with-fpm\"]'\n\n Multiple Package Installation Options:\n\n pkgs\n A list of formulas to install. Must be passed as a python list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install pkgs='[\"foo\",\"bar\"]'\n\n\n Returns a dict containing the new package names and versions::\n\n {'': {'old': '',\n 'new': ''}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install 'package package package'\n '''\n try:\n pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](\n name, pkgs, kwargs.get('sources', {})\n )\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n if not pkg_params:\n return {}\n\n formulas = ' '.join(pkg_params)\n old = list_pkgs()\n\n # Ensure we've tapped the repo if necessary\n if taps:\n if not isinstance(taps, list):\n # Feels like there is a better way to allow for tap being\n # specified as both a string and a list\n taps = [taps]\n\n for tap in taps:\n _tap(tap)\n\n if options:\n cmd = 'install {0} {1}'.format(formulas, ' '.join(options))\n else:\n cmd = 'install {0}'.format(formulas)\n\n out = _call_brew(cmd)\n if out['retcode'] != 0 and out['stderr']:\n errors = [out['stderr']]\n else:\n errors = []\n\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret = salt.utils.data.compare_dicts(old, new)\n\n if errors:\n raise CommandExecutionError(\n 'Problem encountered installing package(s)',\n info={'errors': errors, 'changes': ret}\n )\n\n return ret", "response": "Returns a dict containing the new packages and versions of the passed package."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlists the available upgrade packages for all packages", "response": "def list_upgrades(refresh=True, **kwargs): # pylint: disable=W0613\n '''\n Check whether or not an upgrade is available for all packages\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_upgrades\n '''\n if refresh:\n refresh_db()\n\n res = _call_brew('outdated --json=v1')\n ret = {}\n\n try:\n data = salt.utils.json.loads(res['stdout'])\n except ValueError as err:\n msg = 'unable to interpret output from \"brew outdated\": {0}'.format(err)\n log.error(msg)\n raise CommandExecutionError(msg)\n\n for pkg in data:\n # current means latest available to brew\n ret[pkg['name']] = pkg['current_version']\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upgrade(refresh=True, **kwargs):\n '''\n Upgrade outdated, unpinned brews.\n\n refresh\n Fetch the newest version of Homebrew and all formulae from GitHub before installing.\n\n Returns a dictionary containing the changes:\n\n .. code-block:: python\n\n {'': {'old': '',\n 'new': ''}}\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade\n '''\n ret = {'changes': {},\n 'result': True,\n 'comment': '',\n }\n\n old = list_pkgs()\n\n if salt.utils.data.is_true(refresh):\n refresh_db()\n\n result = _call_brew('upgrade', failhard=False)\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret = salt.utils.data.compare_dicts(old, new)\n\n if result['retcode'] != 0:\n raise CommandExecutionError(\n 'Problem encountered upgrading packages',\n info={'changes': ret, 'result': result}\n )\n\n return ret", "response": "Upgrade outdated unpinned brews."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef config(name, config):\n '''\n Ensure that the chronos job with the given name is present and is configured\n to match the given config values.\n\n :param name: The job name\n :param config: The configuration to apply (dict)\n :return: A standard Salt changes dictionary\n '''\n # setup return structure\n ret = {\n 'name': name,\n 'changes': {},\n 'result': False,\n 'comment': '',\n }\n\n # get existing config if job is present\n existing_config = None\n if __salt__['chronos.has_job'](name):\n existing_config = __salt__['chronos.job'](name)['job']\n\n # compare existing config with defined config\n if existing_config:\n update_config = copy.deepcopy(existing_config)\n salt.utils.configcomparer.compare_and_update_config(\n config,\n update_config,\n ret['changes'],\n )\n else:\n # the job is not configured--we need to create it from scratch\n ret['changes']['job'] = {\n 'new': config,\n 'old': None,\n }\n update_config = config\n\n if ret['changes']:\n # if the only change is in schedule, check to see if patterns are equivalent\n if 'schedule' in ret['changes'] and len(ret['changes']) == 1:\n if 'new' in ret['changes']['schedule'] and 'old' in ret['changes']['schedule']:\n new = ret['changes']['schedule']['new']\n log.debug('new schedule: %s', new)\n old = ret['changes']['schedule']['old']\n log.debug('old schedule: %s', old)\n if new and old:\n _new = new.split('/')\n log.debug('_new schedule: %s', _new)\n _old = old.split('/')\n log.debug('_old schedule: %s', _old)\n if len(_new) == 3 and len(_old) == 3:\n log.debug('_new[0] == _old[0]: %s',\n six.text_type(_new[0]) == six.text_type(_old[0]))\n log.debug('_new[2] == _old[2]: %s',\n six.text_type(_new[2]) == six.text_type(_old[2]))\n if six.text_type(_new[0]) == six.text_type(_old[0]) and \\\n six.text_type(_new[2]) == six.text_type(_old[2]):\n log.debug('schedules match--no need for changes')\n ret['changes'] = {}\n\n # update the config if we registered any changes\n log.debug('schedules match--no need for changes')\n if ret['changes']:\n # if test report there will be an update\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Chronos job {0} is set to be updated'.format(\n name\n )\n return ret\n\n update_result = __salt__['chronos.update_job'](name, update_config)\n if 'exception' in update_result:\n ret['result'] = False\n ret['comment'] = 'Failed to update job config for {0}: {1}'.format(\n name,\n update_result['exception'],\n )\n return ret\n else:\n ret['result'] = True\n ret['comment'] = 'Updated job config for {0}'.format(name)\n return ret\n ret['result'] = True\n ret['comment'] = 'Chronos job {0} configured correctly'.format(name)\n return ret", "response": "Ensure that the chronos job with the given name is configured with the given config values."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures that a range record is present.", "response": "def present(name=None, start_addr=None, end_addr=None, data=None, **api_opts):\n '''\n Ensure range record is present.\n\n infoblox_range.present:\n start_addr: '129.97.150.160',\n end_addr: '129.97.150.170',\n\n Verbose state example:\n\n .. code-block:: yaml\n\n infoblox_range.present:\n data: {\n 'always_update_dns': False,\n 'authority': False,\n 'comment': 'range of IP addresses used for salt.. was used for ghost images deployment',\n 'ddns_generate_hostname': True,\n 'deny_all_clients': False,\n 'deny_bootp': False,\n 'disable': False,\n 'email_list': [],\n 'enable_ddns': False,\n 'enable_dhcp_thresholds': False,\n 'enable_email_warnings': False,\n 'enable_ifmap_publishing': False,\n 'enable_snmp_warnings': False,\n 'end_addr': '129.97.150.169',\n 'exclude': [],\n 'extattrs': {},\n 'fingerprint_filter_rules': [],\n 'high_water_mark': 95,\n 'high_water_mark_reset': 85,\n 'ignore_dhcp_option_list_request': False,\n 'lease_scavenge_time': -1,\n 'logic_filter_rules': [],\n 'low_water_mark': 0,\n 'low_water_mark_reset': 10,\n 'mac_filter_rules': [],\n 'member': {'_struct': 'dhcpmember',\n 'ipv4addr': '129.97.128.9',\n 'name': 'cn-dhcp-mc.example.ca'},\n 'ms_options': [],\n 'nac_filter_rules': [],\n 'name': 'ghost-range',\n 'network': '129.97.150.0/24',\n 'network_view': 'default',\n 'option_filter_rules': [],\n 'options': [{'name': 'dhcp-lease-time',\n 'num': 51,\n 'use_option': False,\n 'value': '43200',\n 'vendor_class': 'DHCP'}],\n 'recycle_leases': True,\n 'relay_agent_filter_rules': [],\n 'server_association_type': 'MEMBER',\n 'start_addr': '129.97.150.160',\n 'update_dns_on_lease_renewal': False,\n 'use_authority': False,\n 'use_bootfile': False,\n 'use_bootserver': False,\n 'use_ddns_domainname': False,\n 'use_ddns_generate_hostname': True,\n 'use_deny_bootp': False,\n 'use_email_list': False,\n 'use_enable_ddns': False,\n 'use_enable_dhcp_thresholds': False,\n 'use_enable_ifmap_publishing': False,\n 'use_ignore_dhcp_option_list_request': False,\n 'use_known_clients': False,\n 'use_lease_scavenge_time': False,\n 'use_nextserver': False,\n 'use_options': False,\n 'use_recycle_leases': False,\n 'use_unknown_clients': False,\n 'use_update_dns_on_lease_renewal': False\n }\n '''\n ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}\n\n if not data:\n data = {}\n if 'name' not in data:\n data.update({'name': name})\n if 'start_addr' not in data:\n data.update({'start_addr': start_addr})\n if 'end_addr' not in data:\n data.update({'end_addr': end_addr})\n\n obj = __salt__['infoblox.get_ipv4_range'](data['start_addr'], data['end_addr'], **api_opts)\n if obj is None:\n obj = __salt__['infoblox.get_ipv4_range'](start_addr=data['start_addr'], end_addr=None, **api_opts)\n if obj is None:\n obj = __salt__['infoblox.get_ipv4_range'](start_addr=None, end_addr=data['end_addr'], **api_opts)\n\n if obj:\n diff = __salt__['infoblox.diff_objects'](data, obj)\n if not diff:\n ret['result'] = True\n ret['comment'] = 'supplied fields in correct state'\n return ret\n if diff:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'would attempt to update record'\n return ret\n new_obj = __salt__['infoblox.update_object'](obj['_ref'], data=data, **api_opts)\n ret['result'] = True\n ret['comment'] = 'record fields updated'\n ret['changes'] = {'diff': diff}\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'would attempt to create record {0}'.format(name)\n return ret\n\n new_obj_ref = __salt__['infoblox.create_ipv4_range'](data, **api_opts)\n new_obj = __salt__['infoblox.get_ipv4_range'](data['start_addr'], data['end_addr'], **api_opts)\n\n ret['result'] = True\n ret['comment'] = 'record created'\n ret['changes'] = {'old': 'None', 'new': {'_ref': new_obj_ref, 'data': new_obj}}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nensure the specified resource is absent", "response": "def absent(name=None, start_addr=None, end_addr=None, data=None, **api_opts):\n '''\n Ensure the range is removed\n\n Supplying the end of the range is optional.\n\n State example:\n\n .. code-block:: yaml\n\n infoblox_range.absent:\n - name: 'vlan10'\n\n infoblox_range.absent:\n - name:\n - start_addr: 127.0.1.20\n '''\n ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}\n\n if not data:\n data = {}\n if 'name' not in data:\n data.update({'name': name})\n if 'start_addr' not in data:\n data.update({'start_addr': start_addr})\n if 'end_addr' not in data:\n data.update({'end_addr': end_addr})\n\n obj = __salt__['infoblox.get_ipv4_range'](data['start_addr'], data['end_addr'], **api_opts)\n if obj is None:\n obj = __salt__['infoblox.get_ipv4_range'](start_addr=data['start_addr'], end_addr=None, **api_opts)\n if obj is None:\n obj = __salt__['infoblox.get_ipv4_range'](start_addr=None, end_addr=data['end_addr'], **api_opts)\n\n if not obj:\n ret['result'] = True\n ret['comment'] = 'already deleted'\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'would attempt to delete range'\n return ret\n\n if __salt__['infoblox.delete_object'](objref=obj['_ref']):\n ret['result'] = True\n ret['changes'] = {'old': 'Found {0} - {1}'.format(start_addr, end_addr),\n 'new': 'Removed'}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nsend this command to the server and get the output and stderr.", "response": "def sendline(self, cmd):\n '''\n Send this command to the server and\n return a tuple of the output and the stderr.\n\n The format for parameters is:\n\n cmd (string): The command to send to the sever.\n '''\n self.conn.sendline(cmd, self.linesep)\n\n # saw_prompt = False\n ret_stdout = []\n ret_stderr = []\n while self.conn.has_unread_data:\n stdout, stderr = self.conn.recv()\n\n if stdout:\n ret_stdout.append(stdout)\n if stderr:\n log.debug('Error while executing command.')\n ret_stderr.append(stderr)\n\n if stdout and self.prompt_re.search(stdout):\n break\n\n return ''.join(ret_stdout), ''.join(ret_stderr)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nshell out and call the specified ESXCLI commmand parse the result and return something sane.", "response": "def esxcli(host, user, pwd, cmd, protocol=None, port=None, esxi_host=None, credstore=None):\n '''\n Shell out and call the specified esxcli commmand, parse the result\n and return something sane.\n\n :param host: ESXi or vCenter host to connect to\n :param user: User to connect as, usually root\n :param pwd: Password to connect with\n :param port: TCP port\n :param cmd: esxcli command and arguments\n :param esxi_host: If `host` is a vCenter host, then esxi_host is the\n ESXi machine on which to execute this command\n :param credstore: Optional path to the credential store file\n\n :return: Dictionary\n '''\n\n esx_cmd = salt.utils.path.which('esxcli')\n if not esx_cmd:\n log.error('Missing dependency: The salt.utils.vmware.esxcli function requires ESXCLI.')\n return False\n\n # Set default port and protocol if none are provided.\n if port is None:\n port = 443\n if protocol is None:\n protocol = 'https'\n\n if credstore:\n esx_cmd += ' --credstore \\'{0}\\''.format(credstore)\n\n if not esxi_host:\n # Then we are connecting directly to an ESXi server,\n # 'host' points at that server, and esxi_host is a reference to the\n # ESXi instance we are manipulating\n esx_cmd += ' -s {0} -u {1} -p \\'{2}\\' ' \\\n '--protocol={3} --portnumber={4} {5}'.format(host,\n user,\n pwd,\n protocol,\n port,\n cmd)\n else:\n esx_cmd += ' -s {0} -h {1} -u {2} -p \\'{3}\\' ' \\\n '--protocol={4} --portnumber={5} {6}'.format(host,\n esxi_host,\n user,\n pwd,\n protocol,\n port,\n cmd)\n\n ret = salt.modules.cmdmod.run_all(esx_cmd, output_loglevel='quiet')\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_service_instance(host, username, password, protocol,\n port, mechanism, principal, domain):\n '''\n Internal method to authenticate with a vCenter server or ESX/ESXi host\n and return the service instance object.\n '''\n log.trace('Retrieving new service instance')\n token = None\n if mechanism == 'userpass':\n if username is None:\n raise salt.exceptions.CommandExecutionError(\n 'Login mechanism userpass was specified but the mandatory '\n 'parameter \\'username\\' is missing')\n if password is None:\n raise salt.exceptions.CommandExecutionError(\n 'Login mechanism userpass was specified but the mandatory '\n 'parameter \\'password\\' is missing')\n elif mechanism == 'sspi':\n if principal is not None and domain is not None:\n try:\n token = get_gssapi_token(principal, host, domain)\n except Exception as exc:\n raise salt.exceptions.VMwareConnectionError(six.text_type(exc))\n else:\n err_msg = 'Login mechanism \\'{0}\\' was specified but the' \\\n ' mandatory parameters are missing'.format(mechanism)\n raise salt.exceptions.CommandExecutionError(err_msg)\n else:\n raise salt.exceptions.CommandExecutionError(\n 'Unsupported mechanism: \\'{0}\\''.format(mechanism))\n try:\n log.trace('Connecting using the \\'%s\\' mechanism, with username \\'%s\\'',\n mechanism, username)\n service_instance = SmartConnect(\n host=host,\n user=username,\n pwd=password,\n protocol=protocol,\n port=port,\n b64token=token,\n mechanism=mechanism)\n except TypeError as exc:\n if 'unexpected keyword argument' in exc.message:\n log.error('Initial connect to the VMware endpoint failed with %s', exc.message)\n log.error('This may mean that a version of PyVmomi EARLIER than 6.0.0.2016.6 is installed.')\n log.error('We recommend updating to that version or later.')\n raise\n except Exception as exc: # pylint: disable=broad-except\n # pyVmomi's SmartConnect() actually raises Exception in some cases.\n default_msg = 'Could not connect to host \\'{0}\\'. ' \\\n 'Please check the debug log for more information.'.format(host)\n\n try:\n if (isinstance(exc, vim.fault.HostConnectFault) and\n '[SSL: CERTIFICATE_VERIFY_FAILED]' in exc.msg) or \\\n '[SSL: CERTIFICATE_VERIFY_FAILED]' in six.text_type(exc):\n service_instance = SmartConnect(\n host=host,\n user=username,\n pwd=password,\n protocol=protocol,\n port=port,\n sslContext=getattr(ssl, '_create_unverified_context', getattr(ssl, '_create_stdlib_context'))(),\n b64token=token,\n mechanism=mechanism)\n else:\n log.exception(exc)\n err_msg = exc.msg if hasattr(exc, 'msg') else default_msg\n raise salt.exceptions.VMwareConnectionError(err_msg)\n except Exception as exc: # pylint: disable=broad-except\n # pyVmomi's SmartConnect() actually raises Exception in some cases.\n if 'certificate verify failed' in six.text_type(exc):\n context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)\n context.verify_mode = ssl.CERT_NONE\n try:\n service_instance = SmartConnect(\n host=host,\n user=username,\n pwd=password,\n protocol=protocol,\n port=port,\n sslContext=context,\n b64token=token,\n mechanism=mechanism\n )\n except Exception as exc:\n log.exception(exc)\n err_msg = exc.msg if hasattr(exc, 'msg') else six.text_type(exc)\n raise salt.exceptions.VMwareConnectionError(\n 'Could not connect to host \\'{0}\\': '\n '{1}'.format(host, err_msg))\n else:\n err_msg = exc.msg if hasattr(exc, 'msg') else default_msg\n log.trace(exc)\n raise salt.exceptions.VMwareConnectionError(err_msg)\n atexit.register(Disconnect, service_instance)\n return service_instance", "response": "Internal method to authenticate with a vCenter server or ESXi server and return the service instance object."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets a reference to a VMware customization spec", "response": "def get_customizationspec_ref(si, customization_spec_name):\n '''\n Get a reference to a VMware customization spec for the purposes of customizing a clone\n\n si\n ServiceInstance for the vSphere or ESXi server (see get_service_instance)\n\n customization_spec_name\n Name of the customization spec\n\n '''\n customization_spec_name = si.content.customizationSpecManager.GetCustomizationSpec(name=customization_spec_name)\n return customization_spec_name"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_service_instance(host, username=None, password=None, protocol=None,\n port=None, mechanism='userpass', principal=None,\n domain=None):\n '''\n Authenticate with a vCenter server or ESX/ESXi host and return the service instance object.\n\n host\n The location of the vCenter server or ESX/ESXi host.\n\n username\n The username used to login to the vCenter server or ESX/ESXi host.\n Required if mechanism is ``userpass``\n\n password\n The password used to login to the vCenter server or ESX/ESXi host.\n Required if mechanism is ``userpass``\n\n protocol\n Optionally set to alternate protocol if the vCenter server or ESX/ESXi host is not\n using the default protocol. Default protocol is ``https``.\n\n port\n Optionally set to alternate port if the vCenter server or ESX/ESXi host is not\n using the default port. Default port is ``443``.\n\n mechanism\n pyVmomi connection mechanism. Can either be ``userpass`` or ``sspi``.\n Default mechanism is ``userpass``.\n\n principal\n Kerberos service principal. Required if mechanism is ``sspi``\n\n domain\n Kerberos user domain. Required if mechanism is ``sspi``\n '''\n\n if protocol is None:\n protocol = 'https'\n if port is None:\n port = 443\n\n service_instance = GetSi()\n if service_instance:\n stub = GetStub()\n if (salt.utils.platform.is_proxy() or\n (hasattr(stub, 'host') and\n stub.host != ':'.join([host, six.text_type(port)]))):\n # Proxies will fork and mess up the cached service instance.\n # If this is a proxy or we are connecting to a different host\n # invalidate the service instance to avoid a potential memory leak\n # and reconnect\n Disconnect(service_instance)\n service_instance = None\n else:\n return service_instance\n\n if not service_instance:\n service_instance = _get_service_instance(host,\n username,\n password,\n protocol,\n port,\n mechanism,\n principal,\n domain)\n\n # Test if data can actually be retrieved or connection has gone stale\n log.trace('Checking connection is still authenticated')\n try:\n service_instance.CurrentTime()\n except vim.fault.NotAuthenticated:\n log.trace('Session no longer authenticating. Reconnecting')\n Disconnect(service_instance)\n service_instance = _get_service_instance(host,\n username,\n password,\n protocol,\n port,\n mechanism,\n principal,\n domain)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n\n return service_instance", "response": "Authenticate with a vCenter server or ESXi host and return the service instance object."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_new_service_instance_stub(service_instance, path, ns=None,\n version=None):\n '''\n Returns a stub that points to a different path,\n created from an existing connection.\n\n service_instance\n The Service Instance.\n\n path\n Path of the new stub.\n\n ns\n Namespace of the new stub.\n Default value is None\n\n version\n Version of the new stub.\n Default value is None.\n '''\n # For python 2.7.9 and later, the default SSL context has more strict\n # connection handshaking rule. We may need turn off the hostname checking\n # and the client side cert verification.\n context = None\n if sys.version_info[:3] > (2, 7, 8):\n context = ssl.create_default_context()\n context.check_hostname = False\n context.verify_mode = ssl.CERT_NONE\n\n stub = service_instance._stub\n hostname = stub.host.split(':')[0]\n session_cookie = stub.cookie.split('\"')[1]\n VmomiSupport.GetRequestContext()['vcSessionCookie'] = session_cookie\n new_stub = SoapStubAdapter(host=hostname,\n ns=ns,\n path=path,\n version=version,\n poolSize=0,\n sslContext=context)\n new_stub.cookie = stub.cookie\n return new_stub", "response": "Returns a new SOAP stub that points to a different path ns and version."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the service instance from a managed object.", "response": "def get_service_instance_from_managed_object(mo_ref, name=''):\n '''\n Retrieves the service instance from a managed object.\n\n me_ref\n Reference to a managed object (of type vim.ManagedEntity).\n\n name\n Name of managed object. This field is optional.\n '''\n if not name:\n name = mo_ref.name\n log.trace('[%s] Retrieving service instance from managed object', name)\n si = vim.ServiceInstance('ServiceInstance')\n si._stub = mo_ref._stub\n return si"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndisconnects from the vCenter server or ESXi host or ESXi managed object references.", "response": "def disconnect(service_instance):\n '''\n Function that disconnects from the vCenter server or ESXi host\n\n service_instance\n The Service Instance from which to obtain managed object references.\n '''\n log.trace('Disconnecting')\n try:\n Disconnect(service_instance)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns information of the vCenter or ESXi host managed object references.", "response": "def get_service_info(service_instance):\n '''\n Returns information of the vCenter or ESXi host\n\n service_instance\n The Service Instance from which to obtain managed object references.\n '''\n try:\n return service_instance.content.about\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a reference to a Distributed Virtual Switch object.", "response": "def _get_dvs(service_instance, dvs_name):\n '''\n Return a reference to a Distributed Virtual Switch object.\n\n :param service_instance: PyVmomi service instance\n :param dvs_name: Name of DVS to return\n :return: A PyVmomi DVS object\n '''\n switches = list_dvs(service_instance)\n if dvs_name in switches:\n inventory = get_inventory(service_instance)\n container = inventory.viewManager.CreateContainerView(inventory.rootFolder, [vim.DistributedVirtualSwitch], True)\n for item in container.view:\n if item.name == dvs_name:\n return item\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a portgroup object corresponding to the portgroup name on the dvs object", "response": "def _get_dvs_portgroup(dvs, portgroup_name):\n '''\n Return a portgroup object corresponding to the portgroup name on the dvs\n\n :param dvs: DVS object\n :param portgroup_name: Name of portgroup to return\n :return: Portgroup object\n '''\n for portgroup in dvs.portgroup:\n if portgroup.name == portgroup_name:\n return portgroup\n\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_dvs_uplink_portgroup(dvs, portgroup_name):\n '''\n Return a portgroup object corresponding to the portgroup name on the dvs\n\n :param dvs: DVS object\n :param portgroup_name: Name of portgroup to return\n :return: Portgroup object\n '''\n for portgroup in dvs.portgroup:\n if portgroup.name == portgroup_name:\n return portgroup\n\n return None", "response": "Returns a portgroup object corresponding to the portgroup name on the dvs object"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget the gssapi token for a Kerberos connection", "response": "def get_gssapi_token(principal, host, domain):\n '''\n Get the gssapi token for Kerberos connection\n\n principal\n The service principal\n host\n Host url where we would like to authenticate\n domain\n Kerberos user domain\n '''\n\n if not HAS_GSSAPI:\n raise ImportError('The gssapi library is not imported.')\n\n service = '{0}/{1}@{2}'.format(principal, host, domain)\n log.debug('Retrieving gsspi token for service %s', service)\n service_name = gssapi.Name(service, gssapi.C_NT_USER_NAME)\n ctx = gssapi.InitContext(service_name)\n in_token = None\n while not ctx.established:\n out_token = ctx.step(in_token)\n if out_token:\n if six.PY2:\n return base64.b64encode(out_token)\n return base64.b64encode(salt.utils.stringutils.to_bytes(out_token))\n if ctx.established:\n break\n if not in_token:\n raise salt.exceptions.CommandExecutionError(\n 'Can\\'t receive token, no response from server')\n raise salt.exceptions.CommandExecutionError(\n 'Context established, but didn\\'t receive token')"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the hardware grains for the specified service instance.", "response": "def get_hardware_grains(service_instance):\n '''\n Return hardware info for standard minion grains if the service_instance is a HostAgent type\n\n service_instance\n The service instance object to get hardware info for\n\n .. versionadded:: 2016.11.0\n '''\n hw_grain_data = {}\n if get_inventory(service_instance).about.apiType == 'HostAgent':\n view = service_instance.content.viewManager.CreateContainerView(service_instance.RetrieveContent().rootFolder,\n [vim.HostSystem], True)\n if view and view.view:\n hw_grain_data['manufacturer'] = view.view[0].hardware.systemInfo.vendor\n hw_grain_data['productname'] = view.view[0].hardware.systemInfo.model\n\n for _data in view.view[0].hardware.systemInfo.otherIdentifyingInfo:\n if _data.identifierType.key == 'ServiceTag':\n hw_grain_data['serialnumber'] = _data.identifierValue\n\n hw_grain_data['osfullname'] = view.view[0].summary.config.product.fullName\n hw_grain_data['osmanufacturer'] = view.view[0].summary.config.product.vendor\n hw_grain_data['osrelease'] = view.view[0].summary.config.product.version\n hw_grain_data['osbuild'] = view.view[0].summary.config.product.build\n hw_grain_data['os_family'] = view.view[0].summary.config.product.name\n hw_grain_data['os'] = view.view[0].summary.config.product.name\n hw_grain_data['mem_total'] = view.view[0].hardware.memorySize /1024/1024\n hw_grain_data['biosversion'] = view.view[0].hardware.biosInfo.biosVersion\n hw_grain_data['biosreleasedate'] = view.view[0].hardware.biosInfo.releaseDate.date().strftime('%m/%d/%Y')\n hw_grain_data['cpu_model'] = view.view[0].hardware.cpuPkg[0].description\n hw_grain_data['kernel'] = view.view[0].summary.config.product.productLineId\n hw_grain_data['num_cpu_sockets'] = view.view[0].hardware.cpuInfo.numCpuPackages\n hw_grain_data['num_cpu_cores'] = view.view[0].hardware.cpuInfo.numCpuCores\n hw_grain_data['num_cpus'] = hw_grain_data['num_cpu_sockets'] * hw_grain_data['num_cpu_cores']\n hw_grain_data['ip_interfaces'] = {}\n hw_grain_data['ip4_interfaces'] = {}\n hw_grain_data['ip6_interfaces'] = {}\n hw_grain_data['hwaddr_interfaces'] = {}\n for _vnic in view.view[0].configManager.networkSystem.networkConfig.vnic:\n hw_grain_data['ip_interfaces'][_vnic.device] = []\n hw_grain_data['ip4_interfaces'][_vnic.device] = []\n hw_grain_data['ip6_interfaces'][_vnic.device] = []\n\n hw_grain_data['ip_interfaces'][_vnic.device].append(_vnic.spec.ip.ipAddress)\n hw_grain_data['ip4_interfaces'][_vnic.device].append(_vnic.spec.ip.ipAddress)\n if _vnic.spec.ip.ipV6Config:\n hw_grain_data['ip6_interfaces'][_vnic.device].append(_vnic.spec.ip.ipV6Config.ipV6Address)\n hw_grain_data['hwaddr_interfaces'][_vnic.device] = _vnic.spec.mac\n hw_grain_data['host'] = view.view[0].configManager.networkSystem.dnsConfig.hostName\n hw_grain_data['domain'] = view.view[0].configManager.networkSystem.dnsConfig.domainName\n hw_grain_data['fqdn'] = '{0}{1}{2}'.format(\n view.view[0].configManager.networkSystem.dnsConfig.hostName,\n ('.' if view.view[0].configManager.networkSystem.dnsConfig.domainName else ''),\n view.view[0].configManager.networkSystem.dnsConfig.domainName)\n\n for _pnic in view.view[0].configManager.networkSystem.networkInfo.pnic:\n hw_grain_data['hwaddr_interfaces'][_pnic.device] = _pnic.mac\n\n hw_grain_data['timezone'] = view.view[0].configManager.dateTimeSystem.dateTimeInfo.timeZone.name\n view = None\n return hw_grain_data"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the root folder of a vCenter.", "response": "def get_root_folder(service_instance):\n '''\n Returns the root folder of a vCenter.\n\n service_instance\n The Service Instance Object for which to obtain the root folder.\n '''\n try:\n log.trace('Retrieving root folder')\n return service_instance.RetrieveContent().rootFolder\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_content(service_instance, obj_type, property_list=None,\n container_ref=None, traversal_spec=None,\n local_properties=False):\n '''\n Returns the content of the specified type of object for a Service Instance.\n\n For more information, please see:\n http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.wssdk.pg.doc_50%2FPG_Ch5_PropertyCollector.7.6.html\n\n service_instance\n The Service Instance from which to obtain content.\n\n obj_type\n The type of content to obtain.\n\n property_list\n An optional list of object properties to used to return even more filtered content results.\n\n container_ref\n An optional reference to the managed object to search under. Can either be an object of type Folder, Datacenter,\n ComputeResource, Resource Pool or HostSystem. If not specified, default behaviour is to search under the inventory\n rootFolder.\n\n traversal_spec\n An optional TraversalSpec to be used instead of the standard\n ``Traverse All`` spec.\n\n local_properties\n Flag specifying whether the properties to be retrieved are local to the\n container. If that is the case, the traversal spec needs to be None.\n '''\n # Start at the rootFolder if container starting point not specified\n if not container_ref:\n container_ref = get_root_folder(service_instance)\n\n # By default, the object reference used as the starting poing for the filter\n # is the container_ref passed in the function\n obj_ref = container_ref\n local_traversal_spec = False\n if not traversal_spec and not local_properties:\n local_traversal_spec = True\n # We don't have a specific traversal spec override so we are going to\n # get everything using a container view\n try:\n obj_ref = service_instance.content.viewManager.CreateContainerView(\n container_ref, [obj_type], True)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n\n # Create 'Traverse All' traversal spec to determine the path for\n # collection\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n name='traverseEntities',\n path='view',\n skip=False,\n type=vim.view.ContainerView\n )\n\n # Create property spec to determine properties to be retrieved\n property_spec = vmodl.query.PropertyCollector.PropertySpec(\n type=obj_type,\n all=True if not property_list else False,\n pathSet=property_list\n )\n\n # Create object spec to navigate content\n obj_spec = vmodl.query.PropertyCollector.ObjectSpec(\n obj=obj_ref,\n skip=True if not local_properties else False,\n selectSet=[traversal_spec] if not local_properties else None\n )\n\n # Create a filter spec and specify object, property spec in it\n filter_spec = vmodl.query.PropertyCollector.FilterSpec(\n objectSet=[obj_spec],\n propSet=[property_spec],\n reportMissingObjectsInResults=False\n )\n\n # Retrieve the contents\n try:\n content = service_instance.content.propertyCollector.RetrieveContents([filter_spec])\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n\n # Destroy the object view\n if local_traversal_spec:\n try:\n obj_ref.Destroy()\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n\n return content", "response": "Returns the content of the specified type of object for a Service Instance."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_mor_by_property(service_instance, object_type, property_value, property_name='name', container_ref=None):\n '''\n Returns the first managed object reference having the specified property value.\n\n service_instance\n The Service Instance from which to obtain managed object references.\n\n object_type\n The type of content for which to obtain managed object references.\n\n property_value\n The name of the property for which to obtain the managed object reference.\n\n property_name\n An object property used to return the specified object reference results. Defaults to ``name``.\n\n container_ref\n An optional reference to the managed object to search under. Can either be an object of type Folder, Datacenter,\n ComputeResource, Resource Pool or HostSystem. If not specified, default behaviour is to search under the inventory\n rootFolder.\n '''\n # Get list of all managed object references with specified property\n object_list = get_mors_with_properties(service_instance, object_type, property_list=[property_name], container_ref=container_ref)\n\n for obj in object_list:\n obj_id = six.text_type(obj.get('object', '')).strip('\\'\"')\n if obj[property_name] == property_value or property_value == obj_id:\n return obj['object']\n\n return None", "response": "Returns the first managed object reference having the specified property value."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list containing properties and managed object references for the given service instance object type and property list.", "response": "def get_mors_with_properties(service_instance, object_type, property_list=None,\n container_ref=None, traversal_spec=None,\n local_properties=False):\n '''\n Returns a list containing properties and managed object references for the managed object.\n\n service_instance\n The Service Instance from which to obtain managed object references.\n\n object_type\n The type of content for which to obtain managed object references.\n\n property_list\n An optional list of object properties used to return even more filtered managed object reference results.\n\n container_ref\n An optional reference to the managed object to search under. Can either be an object of type Folder, Datacenter,\n ComputeResource, Resource Pool or HostSystem. If not specified, default behaviour is to search under the inventory\n rootFolder.\n\n traversal_spec\n An optional TraversalSpec to be used instead of the standard\n ``Traverse All`` spec\n\n local_properties\n Flag specigying whether the properties to be retrieved are local to the\n container. If that is the case, the traversal spec needs to be None.\n '''\n # Get all the content\n content_args = [service_instance, object_type]\n content_kwargs = {'property_list': property_list,\n 'container_ref': container_ref,\n 'traversal_spec': traversal_spec,\n 'local_properties': local_properties}\n try:\n content = get_content(*content_args, **content_kwargs)\n except BadStatusLine:\n content = get_content(*content_args, **content_kwargs)\n except IOError as exc:\n if exc.errno != errno.EPIPE:\n raise exc\n content = get_content(*content_args, **content_kwargs)\n\n object_list = []\n for obj in content:\n properties = {}\n for prop in obj.propSet:\n properties[prop.name] = prop.val\n properties['object'] = obj.obj\n object_list.append(properties)\n log.trace('Retrieved %s objects', len(object_list))\n return object_list"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_properties_of_managed_object(mo_ref, properties):\n '''\n Returns specific properties of a managed object, retrieved in an\n optimally.\n\n mo_ref\n The managed object reference.\n\n properties\n List of properties of the managed object to retrieve.\n '''\n service_instance = get_service_instance_from_managed_object(mo_ref)\n log.trace('Retrieving name of %s', type(mo_ref).__name__)\n try:\n items = get_mors_with_properties(service_instance,\n type(mo_ref),\n container_ref=mo_ref,\n property_list=['name'],\n local_properties=True)\n mo_name = items[0]['name']\n except vmodl.query.InvalidProperty:\n mo_name = ''\n log.trace('Retrieving properties \\'%s\\' of %s \\'%s\\'',\n properties, type(mo_ref).__name__, mo_name)\n items = get_mors_with_properties(service_instance,\n type(mo_ref),\n container_ref=mo_ref,\n property_list=properties,\n local_properties=True)\n if not items:\n raise salt.exceptions.VMwareApiError(\n 'Properties of managed object \\'{0}\\' weren\\'t '\n 'retrieved'.format(mo_name))\n return items[0]", "response": "Returns specific properties of a managed object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the network adapter type.", "response": "def get_network_adapter_type(adapter_type):\n '''\n Return the network adapter type.\n\n adpater_type\n The adapter type from which to obtain the network adapter type.\n '''\n if adapter_type == 'vmxnet':\n return vim.vm.device.VirtualVmxnet()\n elif adapter_type == 'vmxnet2':\n return vim.vm.device.VirtualVmxnet2()\n elif adapter_type == 'vmxnet3':\n return vim.vm.device.VirtualVmxnet3()\n elif adapter_type == 'e1000':\n return vim.vm.device.VirtualE1000()\n elif adapter_type == 'e1000e':\n return vim.vm.device.VirtualE1000e()\n\n raise ValueError('An unknown network adapter object type name.')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the network adapter type.", "response": "def get_network_adapter_object_type(adapter_object):\n '''\n Returns the network adapter type.\n\n adapter_object\n The adapter object from which to obtain the network adapter type.\n '''\n if isinstance(adapter_object, vim.vm.device.VirtualVmxnet2):\n return 'vmxnet2'\n if isinstance(adapter_object, vim.vm.device.VirtualVmxnet3):\n return 'vmxnet3'\n if isinstance(adapter_object, vim.vm.device.VirtualVmxnet):\n return 'vmxnet'\n if isinstance(adapter_object, vim.vm.device.VirtualE1000e):\n return 'e1000e'\n if isinstance(adapter_object, vim.vm.device.VirtualE1000):\n return 'e1000'\n\n raise ValueError('An unknown network adapter object type.')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_dvss(dc_ref, dvs_names=None, get_all_dvss=False):\n '''\n Returns distributed virtual switches (DVSs) in a datacenter.\n\n dc_ref\n The parent datacenter reference.\n\n dvs_names\n The names of the DVSs to return. Default is None.\n\n get_all_dvss\n Return all DVSs in the datacenter. Default is False.\n '''\n dc_name = get_managed_object_name(dc_ref)\n log.trace(\n 'Retrieving DVSs in datacenter \\'%s\\', dvs_names=\\'%s\\', get_all_dvss=%s',\n dc_name,\n ','.join(dvs_names) if dvs_names else None,\n get_all_dvss\n )\n properties = ['name']\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='networkFolder',\n skip=True,\n type=vim.Datacenter,\n selectSet=[vmodl.query.PropertyCollector.TraversalSpec(\n path='childEntity',\n skip=False,\n type=vim.Folder)])\n service_instance = get_service_instance_from_managed_object(dc_ref)\n items = [i['object'] for i in\n get_mors_with_properties(service_instance,\n vim.DistributedVirtualSwitch,\n container_ref=dc_ref,\n property_list=properties,\n traversal_spec=traversal_spec)\n if get_all_dvss or (dvs_names and i['name'] in dvs_names)]\n return items", "response": "Returns a list of distributed virtual switches in a datacenter."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nretrieve the network folder of a datacenter", "response": "def get_network_folder(dc_ref):\n '''\n Retrieves the network folder of a datacenter\n '''\n dc_name = get_managed_object_name(dc_ref)\n log.trace('Retrieving network folder in datacenter \\'%s\\'', dc_name)\n service_instance = get_service_instance_from_managed_object(dc_ref)\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='networkFolder',\n skip=False,\n type=vim.Datacenter)\n entries = get_mors_with_properties(service_instance,\n vim.Folder,\n container_ref=dc_ref,\n property_list=['name'],\n traversal_spec=traversal_spec)\n if not entries:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Network folder in datacenter \\'{0}\\' wasn\\'t retrieved'\n ''.format(dc_name))\n return entries[0]['object']"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a distributed virtual switch in a datacenter.", "response": "def create_dvs(dc_ref, dvs_name, dvs_create_spec=None):\n '''\n Creates a distributed virtual switches (DVS) in a datacenter.\n Returns the reference to the newly created distributed virtual switch.\n\n dc_ref\n The parent datacenter reference.\n\n dvs_name\n The name of the DVS to create.\n\n dvs_create_spec\n The DVS spec (vim.DVSCreateSpec) to use when creating the DVS.\n Default is None.\n '''\n dc_name = get_managed_object_name(dc_ref)\n log.trace('Creating DVS \\'%s\\' in datacenter \\'%s\\'', dvs_name, dc_name)\n if not dvs_create_spec:\n dvs_create_spec = vim.DVSCreateSpec()\n if not dvs_create_spec.configSpec:\n dvs_create_spec.configSpec = vim.VMwareDVSConfigSpec()\n dvs_create_spec.configSpec.name = dvs_name\n netw_folder_ref = get_network_folder(dc_ref)\n try:\n task = netw_folder_ref.CreateDVS_Task(dvs_create_spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n wait_for_task(task, dvs_name, six.text_type(task.__class__))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_dvs(dvs_ref, dvs_config_spec):\n '''\n Updates a distributed virtual switch with the config_spec.\n\n dvs_ref\n The DVS reference.\n\n dvs_config_spec\n The updated config spec (vim.VMwareDVSConfigSpec) to be applied to\n the DVS.\n '''\n dvs_name = get_managed_object_name(dvs_ref)\n log.trace('Updating dvs \\'%s\\'', dvs_name)\n try:\n task = dvs_ref.ReconfigureDvs_Task(dvs_config_spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n wait_for_task(task, dvs_name, six.text_type(task.__class__))", "response": "Updates a distributed virtual switch with the config_spec."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_dvs_network_resource_management_enabled(dvs_ref, enabled):\n '''\n Sets whether NIOC is enabled on a DVS.\n\n dvs_ref\n The DVS reference.\n\n enabled\n Flag specifying whether NIOC is enabled.\n '''\n dvs_name = get_managed_object_name(dvs_ref)\n log.trace('Setting network resource management enable to %s on '\n 'dvs \\'%s\\'', enabled, dvs_name)\n try:\n dvs_ref.EnableNetworkResourceManagement(enable=enabled)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)", "response": "Sets whether NIOC is enabled on a DVS."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_dvportgroups(parent_ref, portgroup_names=None,\n get_all_portgroups=False):\n '''\n Returns distributed virtual porgroups (dvportgroups).\n The parent object can be either a datacenter or a dvs.\n\n parent_ref\n The parent object reference. Can be either a datacenter or a dvs.\n\n portgroup_names\n The names of the dvss to return. Default is None.\n\n get_all_portgroups\n Return all portgroups in the parent. Default is False.\n '''\n if not (isinstance(parent_ref,\n (vim.Datacenter, vim.DistributedVirtualSwitch))):\n raise salt.exceptions.ArgumentValueError(\n 'Parent has to be either a datacenter, '\n 'or a distributed virtual switch')\n parent_name = get_managed_object_name(parent_ref)\n log.trace('Retrieving portgroup in %s \\'%s\\', portgroups_names=\\'%s\\', '\n 'get_all_portgroups=%s',\n type(parent_ref).__name__,\n parent_name,\n ','.join(portgroup_names) if portgroup_names else None,\n get_all_portgroups)\n properties = ['name']\n if isinstance(parent_ref, vim.Datacenter):\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='networkFolder',\n skip=True,\n type=vim.Datacenter,\n selectSet=[vmodl.query.PropertyCollector.TraversalSpec(\n path='childEntity',\n skip=False,\n type=vim.Folder)])\n else: # parent is distributed virtual switch\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='portgroup',\n skip=False,\n type=vim.DistributedVirtualSwitch)\n\n service_instance = get_service_instance_from_managed_object(parent_ref)\n items = [i['object'] for i in\n get_mors_with_properties(service_instance,\n vim.DistributedVirtualPortgroup,\n container_ref=parent_ref,\n property_list=properties,\n traversal_spec=traversal_spec)\n if get_all_portgroups or\n (portgroup_names and i['name'] in portgroup_names)]\n return items", "response": "Returns a list of distributed virtual porgroups in a distributed virtual switch."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the uplink distributed virtual portgroup of a distributed virtual switch", "response": "def get_uplink_dvportgroup(dvs_ref):\n '''\n Returns the uplink distributed virtual portgroup of a distributed virtual\n switch (dvs)\n\n dvs_ref\n The dvs reference\n '''\n dvs_name = get_managed_object_name(dvs_ref)\n log.trace('Retrieving uplink portgroup of dvs \\'%s\\'', dvs_name)\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='portgroup',\n skip=False,\n type=vim.DistributedVirtualSwitch)\n service_instance = get_service_instance_from_managed_object(dvs_ref)\n items = [entry['object'] for entry in\n get_mors_with_properties(service_instance,\n vim.DistributedVirtualPortgroup,\n container_ref=dvs_ref,\n property_list=['tag'],\n traversal_spec=traversal_spec)\n if entry['tag'] and\n [t for t in entry['tag'] if t.key == 'SYSTEM/DVS.UPLINKPG']]\n if not items:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Uplink portgroup of DVS \\'{0}\\' wasn\\'t found'.format(dvs_name))\n return items[0]"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates a distributed virtual portgroup on a distributed virtual switch", "response": "def create_dvportgroup(dvs_ref, spec):\n '''\n Creates a distributed virtual portgroup on a distributed virtual switch\n (dvs)\n\n dvs_ref\n The dvs reference\n\n spec\n Portgroup spec (vim.DVPortgroupConfigSpec)\n '''\n dvs_name = get_managed_object_name(dvs_ref)\n log.trace('Adding portgroup %s to dvs \\'%s\\'', spec.name, dvs_name)\n log.trace('spec = %s', spec)\n try:\n task = dvs_ref.CreateDVPortgroup_Task(spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n wait_for_task(task, dvs_name, six.text_type(task.__class__))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef update_dvportgroup(portgroup_ref, spec):\n '''\n Updates a distributed virtual portgroup\n\n portgroup_ref\n The portgroup reference\n\n spec\n Portgroup spec (vim.DVPortgroupConfigSpec)\n '''\n pg_name = get_managed_object_name(portgroup_ref)\n log.trace('Updating portgrouo %s', pg_name)\n try:\n task = portgroup_ref.ReconfigureDVPortgroup_Task(spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n wait_for_task(task, pg_name, six.text_type(task.__class__))", "response": "Updates a distributed virtual portgroup with the given portgroup."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a list of standard switch networks.", "response": "def get_networks(parent_ref, network_names=None, get_all_networks=False):\n '''\n Returns networks of standard switches.\n The parent object can be a datacenter.\n\n parent_ref\n The parent object reference. A datacenter object.\n\n network_names\n The name of the standard switch networks. Default is None.\n\n get_all_networks\n Boolean indicates whether to return all networks in the parent.\n Default is False.\n '''\n\n if not isinstance(parent_ref, vim.Datacenter):\n raise salt.exceptions.ArgumentValueError(\n 'Parent has to be a datacenter.')\n parent_name = get_managed_object_name(parent_ref)\n log.trace('Retrieving network from %s \\'%s\\', network_names=\\'%s\\', '\n 'get_all_networks=%s',\n type(parent_ref).__name__,\n parent_name,\n ','.join(network_names) if network_names else None,\n get_all_networks)\n properties = ['name']\n service_instance = get_service_instance_from_managed_object(parent_ref)\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='networkFolder',\n skip=True,\n type=vim.Datacenter,\n selectSet=[vmodl.query.PropertyCollector.TraversalSpec(\n path='childEntity',\n skip=False,\n type=vim.Folder)])\n items = [i['object'] for i in\n get_mors_with_properties(service_instance,\n vim.Network,\n container_ref=parent_ref,\n property_list=properties,\n traversal_spec=traversal_spec)\n if get_all_networks or\n (network_names and i['name'] in network_names)]\n return items"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a simple list of objects from a given service instance and vim_object.", "response": "def list_objects(service_instance, vim_object, properties=None):\n '''\n Returns a simple list of objects from a given service instance.\n\n service_instance\n The Service Instance for which to obtain a list of objects.\n\n object_type\n The type of content for which to obtain information.\n\n properties\n An optional list of object properties used to return reference results.\n If not provided, defaults to ``name``.\n '''\n if properties is None:\n properties = ['name']\n\n items = []\n item_list = get_mors_with_properties(service_instance, vim_object, properties)\n for item in item_list:\n items.append(item['name'])\n return items"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the license manager.", "response": "def get_license_manager(service_instance):\n '''\n Returns the license manager.\n\n service_instance\n The Service Instance Object from which to obrain the license manager.\n '''\n\n log.debug('Retrieving license manager')\n try:\n lic_manager = service_instance.content.licenseManager\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n return lic_manager"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the license assignment manager.", "response": "def get_license_assignment_manager(service_instance):\n '''\n Returns the license assignment manager.\n\n service_instance\n The Service Instance Object from which to obrain the license manager.\n '''\n\n log.debug('Retrieving license assignment manager')\n try:\n lic_assignment_manager = \\\n service_instance.content.licenseManager.licenseAssignmentManager\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n if not lic_assignment_manager:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'License assignment manager was not retrieved')\n return lic_assignment_manager"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the licenses on a specific instance.", "response": "def get_licenses(service_instance, license_manager=None):\n '''\n Returns the licenses on a specific instance.\n\n service_instance\n The Service Instance Object from which to obrain the licenses.\n\n license_manager\n The License Manager object of the service instance. If not provided it\n will be retrieved.\n '''\n\n if not license_manager:\n license_manager = get_license_manager(service_instance)\n log.debug('Retrieving licenses')\n try:\n return license_manager.licenses\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_license(service_instance, key, description, license_manager=None):\n '''\n Adds a license.\n\n service_instance\n The Service Instance Object.\n\n key\n The key of the license to add.\n\n description\n The description of the license to add.\n\n license_manager\n The License Manager object of the service instance. If not provided it\n will be retrieved.\n '''\n if not license_manager:\n license_manager = get_license_manager(service_instance)\n label = vim.KeyValue()\n label.key = 'VpxClientLicenseLabel'\n label.value = description\n log.debug('Adding license \\'%s\\'', description)\n try:\n vmware_license = license_manager.AddLicense(key, [label])\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n return vmware_license", "response": "Adds a license to the virtual machine."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_assigned_licenses(service_instance, entity_ref=None, entity_name=None,\n license_assignment_manager=None):\n '''\n Returns the licenses assigned to an entity. If entity ref is not provided,\n then entity_name is assumed to be the vcenter. This is later checked if\n the entity name is provided.\n\n service_instance\n The Service Instance Object from which to obtain the licenses.\n\n entity_ref\n VMware entity to get the assigned licenses for.\n If None, the entity is the vCenter itself.\n Default is None.\n\n entity_name\n Entity name used in logging.\n Default is None.\n\n license_assignment_manager\n The LicenseAssignmentManager object of the service instance.\n If not provided it will be retrieved.\n Default is None.\n '''\n if not license_assignment_manager:\n license_assignment_manager = \\\n get_license_assignment_manager(service_instance)\n if not entity_name:\n raise salt.exceptions.ArgumentValueError('No entity_name passed')\n # If entity_ref is not defined, then interested in the vcenter\n entity_id = None\n entity_type = 'moid'\n check_name = False\n if not entity_ref:\n if entity_name:\n check_name = True\n entity_type = 'uuid'\n try:\n entity_id = service_instance.content.about.instanceUuid\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n else:\n entity_id = entity_ref._moId\n\n log.trace('Retrieving licenses assigned to \\'%s\\'', entity_name)\n try:\n assignments = \\\n license_assignment_manager.QueryAssignedLicenses(entity_id)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n\n if entity_type == 'uuid' and len(assignments) > 1:\n log.trace('Unexpectectedly retrieved more than one'\n ' VCenter license assignment.')\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Unexpected return. Expect only a single assignment')\n\n if check_name:\n if entity_name != assignments[0].entityDisplayName:\n log.trace('Getting license info for wrong vcenter: %s != %s',\n entity_name, assignments[0].entityDisplayName)\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Got license assignment info for a different vcenter')\n\n return [a.assignedLicense for a in assignments]", "response": "Returns the licenses assigned to an entity."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nassigning a license to an entity.", "response": "def assign_license(service_instance, license_key, license_name,\n entity_ref=None, entity_name=None,\n license_assignment_manager=None):\n '''\n Assigns a license to an entity.\n\n service_instance\n The Service Instance Object from which to obrain the licenses.\n\n license_key\n The key of the license to add.\n\n license_name\n The description of the license to add.\n\n entity_ref\n VMware entity to assign the license to.\n If None, the entity is the vCenter itself.\n Default is None.\n\n entity_name\n Entity name used in logging.\n Default is None.\n\n license_assignment_manager\n The LicenseAssignmentManager object of the service instance.\n If not provided it will be retrieved\n Default is None.\n '''\n if not license_assignment_manager:\n license_assignment_manager = \\\n get_license_assignment_manager(service_instance)\n entity_id = None\n\n if not entity_ref:\n # vcenter\n try:\n entity_id = service_instance.content.about.instanceUuid\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n if not entity_name:\n entity_name = 'vCenter'\n else:\n # e.g. vsan cluster or host\n entity_id = entity_ref._moId\n\n log.trace('Assigning license to \\'%s\\'', entity_name)\n try:\n vmware_license = license_assignment_manager.UpdateAssignedLicense(\n entity_id,\n license_key,\n license_name)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n return vmware_license"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_datacenters(service_instance, datacenter_names=None,\n get_all_datacenters=False):\n '''\n Returns all datacenters in a vCenter.\n\n service_instance\n The Service Instance Object from which to obtain cluster.\n\n datacenter_names\n List of datacenter names to filter by. Default value is None.\n\n get_all_datacenters\n Flag specifying whether to retrieve all datacenters.\n Default value is None.\n '''\n items = [i['object'] for i in\n get_mors_with_properties(service_instance,\n vim.Datacenter,\n property_list=['name'])\n if get_all_datacenters or\n (datacenter_names and i['name'] in datacenter_names)]\n return items", "response": "Returns a list of datacenters in a vCenter."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a vim. Datacenter managed object.", "response": "def get_datacenter(service_instance, datacenter_name):\n '''\n Returns a vim.Datacenter managed object.\n\n service_instance\n The Service Instance Object from which to obtain datacenter.\n\n datacenter_name\n The datacenter name\n '''\n items = get_datacenters(service_instance,\n datacenter_names=[datacenter_name])\n if not items:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Datacenter \\'{0}\\' was not found'.format(datacenter_name))\n return items[0]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a datacenter. .. versionadded:: 2017.7.0 service_instance The Service Instance Object datacenter_name The datacenter name", "response": "def create_datacenter(service_instance, datacenter_name):\n '''\n Creates a datacenter.\n\n .. versionadded:: 2017.7.0\n\n service_instance\n The Service Instance Object\n\n datacenter_name\n The datacenter name\n '''\n root_folder = get_root_folder(service_instance)\n log.trace('Creating datacenter \\'%s\\'', datacenter_name)\n try:\n dc_obj = root_folder.CreateDatacenter(datacenter_name)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n return dc_obj"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a cluster in a datacenter.", "response": "def get_cluster(dc_ref, cluster):\n '''\n Returns a cluster in a datacenter.\n\n dc_ref\n The datacenter reference\n\n cluster\n The cluster to be retrieved\n '''\n dc_name = get_managed_object_name(dc_ref)\n log.trace('Retrieving cluster \\'%s\\' from datacenter \\'%s\\'',\n cluster, dc_name)\n si = get_service_instance_from_managed_object(dc_ref, name=dc_name)\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='hostFolder',\n skip=True,\n type=vim.Datacenter,\n selectSet=[vmodl.query.PropertyCollector.TraversalSpec(\n path='childEntity',\n skip=False,\n type=vim.Folder)])\n items = [i['object'] for i in\n get_mors_with_properties(si,\n vim.ClusterComputeResource,\n container_ref=dc_ref,\n property_list=['name'],\n traversal_spec=traversal_spec)\n if i['name'] == cluster]\n if not items:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Cluster \\'{0}\\' was not found in datacenter '\n '\\'{1}\\''. format(cluster, dc_name))\n return items[0]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a cluster in a datacenter.", "response": "def create_cluster(dc_ref, cluster_name, cluster_spec):\n '''\n Creates a cluster in a datacenter.\n\n dc_ref\n The parent datacenter reference.\n\n cluster_name\n The cluster name.\n\n cluster_spec\n The cluster spec (vim.ClusterConfigSpecEx).\n Defaults to None.\n '''\n dc_name = get_managed_object_name(dc_ref)\n log.trace('Creating cluster \\'%s\\' in datacenter \\'%s\\'',\n cluster_name, dc_name)\n try:\n dc_ref.hostFolder.CreateClusterEx(cluster_name, cluster_spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating a cluster in a datacenter", "response": "def update_cluster(cluster_ref, cluster_spec):\n '''\n Updates a cluster in a datacenter.\n\n cluster_ref\n The cluster reference.\n\n cluster_spec\n The cluster spec (vim.ClusterConfigSpecEx).\n Defaults to None.\n '''\n cluster_name = get_managed_object_name(cluster_ref)\n log.trace('Updating cluster \\'%s\\'', cluster_name)\n try:\n task = cluster_ref.ReconfigureComputeResource_Task(cluster_spec,\n modify=True)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n wait_for_task(task, cluster_name, 'ClusterUpdateTask')"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of datastores associated with a given service instance.", "response": "def list_datastores_full(service_instance):\n '''\n Returns a list of datastores associated with a given service instance.\n The list contains basic information about the datastore:\n name, type, url, capacity, free, used, usage, hosts\n\n service_instance\n The Service Instance Object from which to obtain datastores.\n '''\n datastores_list = list_objects(service_instance, vim.Datastore)\n\n datastores = {}\n for datastore in datastores_list:\n datastores[datastore] = list_datastore_full(service_instance, datastore)\n\n return datastores"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef list_datastore_full(service_instance, datastore):\n '''\n Returns a dictionary with the basic information for the given datastore:\n name, type, url, capacity, free, used, usage, hosts\n\n service_instance\n The Service Instance Object from which to obtain datastores.\n\n datastore\n Name of the datastore.\n '''\n datastore_object = get_mor_by_name(service_instance, vim.Datastore, datastore)\n\n if not datastore_object:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Datastore \\'{0}\\' does not exist.'.format(datastore)\n )\n\n items = {}\n items['name'] = str(datastore_object.summary.name).replace(\"'\", \"\")\n items['type'] = str(datastore_object.summary.type).replace(\"'\", \"\")\n items['url'] = str(datastore_object.summary.url).replace(\"'\", \"\")\n items['capacity'] = datastore_object.summary.capacity / 1024 / 1024\n items['free'] = datastore_object.summary.freeSpace / 1024 / 1024\n items['used'] = items['capacity'] - items['free']\n items['usage'] = (float(items['used']) / float(items['capacity'])) * 100\n items['hosts'] = []\n\n for host in datastore_object.host:\n host_key = str(host.key).replace(\"'\", \"\").split(\":\", 1)[1]\n host_object = get_mor_by_moid(service_instance, vim.HostSystem, host_key)\n items['hosts'].append(host_object.name)\n\n return items", "response": "Returns a dictionary with the basic information for the given datastore."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets reference to an object of specified object type and name", "response": "def get_mor_by_name(si, obj_type, obj_name):\n '''\n Get reference to an object of specified object type and name\n\n si\n ServiceInstance for the vSphere or ESXi server (see get_service_instance)\n\n obj_type\n Type of the object (vim.StoragePod, vim.Datastore, etc)\n\n obj_name\n Name of the object\n '''\n inventory = get_inventory(si)\n container = inventory.viewManager.CreateContainerView(inventory.rootFolder, [obj_type], True)\n for item in container.view:\n if item.name == obj_name:\n return item\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget reference to an object of specified object type and id", "response": "def get_mor_by_moid(si, obj_type, obj_moid):\n '''\n Get reference to an object of specified object type and id\n\n si\n ServiceInstance for the vSphere or ESXi server (see get_service_instance)\n\n obj_type\n Type of the object (vim.StoragePod, vim.Datastore, etc)\n\n obj_moid\n ID of the object\n '''\n inventory = get_inventory(si)\n container = inventory.viewManager.CreateContainerView(inventory.rootFolder, [obj_type], True)\n for item in container.view:\n if item._moId == obj_moid:\n return item\n return None"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget the files from the datastore and return them as a list of vim. host. DatastoreBrowser. SearchResults objects.", "response": "def get_datastore_files(service_instance, directory, datastores, container_object, browser_spec):\n '''\n Get the files with a given browser specification from the datastore.\n\n service_instance\n The Service Instance Object from which to obtain datastores.\n\n directory\n The name of the directory where we would like to search\n\n datastores\n Name of the datastores\n\n container_object\n The base object for searches\n\n browser_spec\n BrowserSpec object which defines the search criteria\n\n return\n list of vim.host.DatastoreBrowser.SearchResults objects\n '''\n\n files = []\n datastore_objects = get_datastores(service_instance, container_object, datastore_names=datastores)\n for datobj in datastore_objects:\n try:\n task = datobj.browser.SearchDatastore_Task(datastorePath='[{}] {}'.format(datobj.name, directory),\n searchSpec=browser_spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n try:\n files.append(salt.utils.vmware.wait_for_task(task, directory, 'query virtual machine files'))\n except salt.exceptions.VMwareFileNotFoundError:\n pass\n return files"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_datastores(service_instance, reference, datastore_names=None,\n backing_disk_ids=None, get_all_datastores=False):\n '''\n Returns a list of vim.Datastore objects representing the datastores visible\n from a VMware object, filtered by their names, or the backing disk\n cannonical name or scsi_addresses\n\n service_instance\n The Service Instance Object from which to obtain datastores.\n\n reference\n The VMware object from which the datastores are visible.\n\n datastore_names\n The list of datastore names to be retrieved. Default value is None.\n\n backing_disk_ids\n The list of canonical names of the disks backing the datastores\n to be retrieved. Only supported if reference is a vim.HostSystem.\n Default value is None\n\n get_all_datastores\n Specifies whether to retrieve all disks in the host.\n Default value is False.\n '''\n obj_name = get_managed_object_name(reference)\n if get_all_datastores:\n log.trace('Retrieving all datastores visible to \\'%s\\'', obj_name)\n else:\n log.trace('Retrieving datastores visible to \\'%s\\': names = (%s); '\n 'backing disk ids = (%s)',\n obj_name, datastore_names, backing_disk_ids)\n if backing_disk_ids and not isinstance(reference, vim.HostSystem):\n\n raise salt.exceptions.ArgumentValueError(\n 'Unsupported reference type \\'{0}\\' when backing disk filter '\n 'is set'.format(reference.__class__.__name__))\n if (not get_all_datastores) and backing_disk_ids:\n # At this point we know the reference is a vim.HostSystem\n log.trace('Filtering datastores with backing disk ids: %s',\n backing_disk_ids)\n storage_system = get_storage_system(service_instance, reference,\n obj_name)\n props = salt.utils.vmware.get_properties_of_managed_object(\n storage_system, ['fileSystemVolumeInfo.mountInfo'])\n mount_infos = props.get('fileSystemVolumeInfo.mountInfo', [])\n disk_datastores = []\n # Non vmfs volumes aren't backed by a disk\n for vol in [i.volume for i in mount_infos if\n isinstance(i.volume, vim.HostVmfsVolume)]:\n\n if not [e for e in vol.extent if e.diskName in backing_disk_ids]:\n # Skip volume if it doesn't contain an extent with a\n # canonical name of interest\n continue\n log.trace('Found datastore \\'%s\\' for disk id(s) \\'%s\\'',\n vol.name, [e.diskName for e in vol.extent])\n disk_datastores.append(vol.name)\n log.trace('Datastore found for disk filter: %s', disk_datastores)\n if datastore_names:\n datastore_names.extend(disk_datastores)\n else:\n datastore_names = disk_datastores\n\n if (not get_all_datastores) and (not datastore_names):\n log.trace('No datastore to be filtered after retrieving the datastores '\n 'backed by the disk id(s) \\'%s\\'', backing_disk_ids)\n return []\n\n log.trace('datastore_names = %s', datastore_names)\n\n # Use the default traversal spec\n if isinstance(reference, vim.HostSystem):\n # Create a different traversal spec for hosts because it looks like the\n # default doesn't retrieve the datastores\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n name='host_datastore_traversal',\n path='datastore',\n skip=False,\n type=vim.HostSystem)\n elif isinstance(reference, vim.ClusterComputeResource):\n # Traversal spec for clusters\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n name='cluster_datastore_traversal',\n path='datastore',\n skip=False,\n type=vim.ClusterComputeResource)\n elif isinstance(reference, vim.Datacenter):\n # Traversal spec for datacenter\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n name='datacenter_datastore_traversal',\n path='datastore',\n skip=False,\n type=vim.Datacenter)\n elif isinstance(reference, vim.StoragePod):\n # Traversal spec for datastore clusters\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n name='datastore_cluster_traversal',\n path='childEntity',\n skip=False,\n type=vim.StoragePod)\n elif isinstance(reference, vim.Folder) and \\\n get_managed_object_name(reference) == 'Datacenters':\n # Traversal of root folder (doesn't support multiple levels of Folders)\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='childEntity',\n selectSet=[\n vmodl.query.PropertyCollector.TraversalSpec(\n path='datastore',\n skip=False,\n type=vim.Datacenter)],\n skip=False,\n type=vim.Folder)\n else:\n raise salt.exceptions.ArgumentValueError(\n 'Unsupported reference type \\'{0}\\''\n ''.format(reference.__class__.__name__))\n\n items = get_mors_with_properties(service_instance,\n object_type=vim.Datastore,\n property_list=['name'],\n container_ref=reference,\n traversal_spec=traversal_spec)\n log.trace('Retrieved %s datastores', len(items))\n items = [i for i in items if get_all_datastores or i['name'] in\n datastore_names]\n log.trace('Filtered datastores: %s', [i['name'] for i in items])\n return [i['object'] for i in items]", "response": "Returns a list of vim. Datastore objects representing the datastores visible to a service instance."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rename_datastore(datastore_ref, new_datastore_name):\n '''\n Renames a datastore\n\n datastore_ref\n vim.Datastore reference to the datastore object to be changed\n\n new_datastore_name\n New datastore name\n '''\n ds_name = get_managed_object_name(datastore_ref)\n log.trace(\"Renaming datastore '%s' to '%s'\", ds_name, new_datastore_name)\n try:\n datastore_ref.RenameDatastore(new_datastore_name)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)", "response": "Renames a datastore object to be changed\nTaxonomy"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn partition informations for a device path of type", "response": "def _get_partition_info(storage_system, device_path):\n '''\n Returns partition informations for a device path, of type\n vim.HostDiskPartitionInfo\n '''\n try:\n partition_infos = \\\n storage_system.RetrieveDiskPartitionInfo(\n devicePath=[device_path])\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n log.trace('partition_info = %s', partition_infos[0])\n return partition_infos[0]"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the new computed partition spec for the given object.", "response": "def _get_new_computed_partition_spec(storage_system,\n device_path,\n partition_info):\n '''\n Computes the new disk partition info when adding a new vmfs partition that\n uses up the remainder of the disk; returns a tuple\n (new_partition_number, vim.HostDiskPartitionSpec\n '''\n log.trace('Adding a partition at the end of the disk and getting the new '\n 'computed partition spec')\n # TODO implement support for multiple partitions\n # We support adding a partition add the end of the disk with partitions\n free_partitions = [p for p in partition_info.layout.partition\n if p.type == 'none']\n if not free_partitions:\n raise salt.exceptions.VMwareObjectNotFoundError(\n 'Free partition was not found on device \\'{0}\\''\n ''.format(partition_info.deviceName))\n free_partition = free_partitions[0]\n\n # Create a layout object that copies the existing one\n layout = vim.HostDiskPartitionLayout(\n total=partition_info.layout.total,\n partition=partition_info.layout.partition)\n # Create a partition with the free space on the disk\n # Change the free partition type to vmfs\n free_partition.type = 'vmfs'\n try:\n computed_partition_info = storage_system.ComputeDiskPartitionInfo(\n devicePath=device_path,\n partitionFormat=vim.HostDiskPartitionInfoPartitionFormat.gpt,\n layout=layout)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n log.trace('computed partition info = {0}', computed_partition_info)\n log.trace('Retrieving new partition number')\n partition_numbers = [p.partition for p in\n computed_partition_info.layout.partition\n if (p.start.block == free_partition.start.block or\n # XXX If the entire disk is free (i.e. the free\n # disk partition starts at block 0) the newily\n # created partition is created from block 1\n (free_partition.start.block == 0 and\n p.start.block == 1)) and\n p.end.block == free_partition.end.block and\n p.type == 'vmfs']\n if not partition_numbers:\n raise salt.exceptions.VMwareNotFoundError(\n 'New partition was not found in computed partitions of device '\n '\\'{0}\\''.format(partition_info.deviceName))\n log.trace('new partition number = %s', partition_numbers[0])\n return (partition_numbers[0], computed_partition_info.spec)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_vmfs_datastore(host_ref, datastore_name, disk_ref,\n vmfs_major_version, storage_system=None):\n '''\n Creates a VMFS datastore from a disk_id\n\n host_ref\n vim.HostSystem object referencing a host to create the datastore on\n\n datastore_name\n Name of the datastore\n\n disk_ref\n vim.HostScsiDislk on which the datastore is created\n\n vmfs_major_version\n VMFS major version to use\n '''\n # TODO Support variable sized partitions\n hostname = get_managed_object_name(host_ref)\n disk_id = disk_ref.canonicalName\n log.debug('Creating datastore \\'%s\\' on host \\'%s\\', scsi disk \\'%s\\', '\n 'vmfs v%s', datastore_name, hostname, disk_id, vmfs_major_version)\n if not storage_system:\n si = get_service_instance_from_managed_object(host_ref, name=hostname)\n storage_system = get_storage_system(si, host_ref, hostname)\n\n target_disk = disk_ref\n partition_info = _get_partition_info(storage_system,\n target_disk.devicePath)\n log.trace('partition_info = %s', partition_info)\n new_partition_number, partition_spec = _get_new_computed_partition_spec(\n storage_system,\n target_disk.devicePath,\n partition_info\n )\n spec = vim.VmfsDatastoreCreateSpec(\n vmfs=vim.HostVmfsSpec(\n majorVersion=vmfs_major_version,\n volumeName=datastore_name,\n extent=vim.HostScsiDiskPartition(\n diskName=disk_id,\n partition=new_partition_number)),\n diskUuid=target_disk.uuid,\n partition=partition_spec)\n try:\n ds_ref = \\\n host_ref.configManager.datastoreSystem.CreateVmfsDatastore(spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n log.debug('Created datastore \\'%s\\' on host \\'%s\\'', datastore_name, hostname)\n return ds_ref", "response": "Creates a VMFS datastore from a disk and a VMFS major version."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a host s datastore system", "response": "def get_host_datastore_system(host_ref, hostname=None):\n '''\n Returns a host's datastore system\n\n host_ref\n Reference to the ESXi host\n\n hostname\n Name of the host. This argument is optional.\n '''\n\n if not hostname:\n hostname = get_managed_object_name(host_ref)\n service_instance = get_service_instance_from_managed_object(host_ref)\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='configManager.datastoreSystem',\n type=vim.HostSystem,\n skip=False)\n objs = get_mors_with_properties(service_instance,\n vim.HostDatastoreSystem,\n property_list=['datastore'],\n container_ref=host_ref,\n traversal_spec=traversal_spec)\n if not objs:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Host\\'s \\'{0}\\' datastore system was not retrieved'\n ''.format(hostname))\n log.trace('[%s] Retrieved datastore system', hostname)\n return objs[0]['object']"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves a datastore from a service instance", "response": "def remove_datastore(service_instance, datastore_ref):\n '''\n Creates a VMFS datastore from a disk_id\n\n service_instance\n The Service Instance Object containing the datastore\n\n datastore_ref\n The reference to the datastore to remove\n '''\n ds_props = get_properties_of_managed_object(\n datastore_ref, ['host', 'info', 'name'])\n ds_name = ds_props['name']\n log.debug('Removing datastore \\'%s\\'', ds_name)\n ds_hosts = ds_props.get('host')\n if not ds_hosts:\n raise salt.exceptions.VMwareApiError(\n 'Datastore \\'{0}\\' can\\'t be removed. No '\n 'attached hosts found'.format(ds_name))\n hostname = get_managed_object_name(ds_hosts[0].key)\n host_ds_system = get_host_datastore_system(ds_hosts[0].key,\n hostname=hostname)\n try:\n host_ds_system.RemoveDatastore(datastore_ref)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n log.trace('[%s] Removed datastore \\'%s\\'', hostname, ds_name)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of vim. HostSystem objects representing ESXi hosts in a vcenter filtered by their names and cluster membership.", "response": "def get_hosts(service_instance, datacenter_name=None, host_names=None,\n cluster_name=None, get_all_hosts=False):\n '''\n Returns a list of vim.HostSystem objects representing ESXi hosts\n in a vcenter filtered by their names and/or datacenter, cluster membership.\n\n service_instance\n The Service Instance Object from which to obtain the hosts.\n\n datacenter_name\n The datacenter name. Default is None.\n\n host_names\n The host_names to be retrieved. Default is None.\n\n cluster_name\n The cluster name - used to restrict the hosts retrieved. Only used if\n the datacenter is set. This argument is optional.\n\n get_all_hosts\n Specifies whether to retrieve all hosts in the container.\n Default value is False.\n '''\n properties = ['name']\n if cluster_name and not datacenter_name:\n raise salt.exceptions.ArgumentValueError(\n 'Must specify the datacenter when specifying the cluster')\n if not host_names:\n host_names = []\n if not datacenter_name:\n # Assume the root folder is the starting point\n start_point = get_root_folder(service_instance)\n else:\n start_point = get_datacenter(service_instance, datacenter_name)\n if cluster_name:\n # Retrieval to test if cluster exists. Cluster existence only makes\n # sense if the datacenter has been specified\n properties.append('parent')\n\n # Search for the objects\n hosts = get_mors_with_properties(service_instance,\n vim.HostSystem,\n container_ref=start_point,\n property_list=properties)\n log.trace('Retrieved hosts: %s', [h['name'] for h in hosts])\n filtered_hosts = []\n for h in hosts:\n # Complex conditions checking if a host should be added to the\n # filtered list (either due to its name and/or cluster membership)\n\n if cluster_name:\n if not isinstance(h['parent'], vim.ClusterComputeResource):\n continue\n parent_name = get_managed_object_name(h['parent'])\n if parent_name != cluster_name:\n continue\n\n if get_all_hosts:\n filtered_hosts.append(h['object'])\n continue\n\n if h['name'] in host_names:\n filtered_hosts.append(h['object'])\n return filtered_hosts"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a map between the scsi addresses and the keys of all luns on an ESXi ArcGIS host.", "response": "def _get_scsi_address_to_lun_key_map(service_instance,\n host_ref,\n storage_system=None,\n hostname=None):\n '''\n Returns a map between the scsi addresses and the keys of all luns on an ESXi\n host.\n map[] = \n\n service_instance\n The Service Instance Object from which to obtain the hosts\n\n host_ref\n The vim.HostSystem object representing the host that contains the\n requested disks.\n\n storage_system\n The host's storage system. Default is None.\n\n hostname\n Name of the host. Default is None.\n '''\n if not hostname:\n hostname = get_managed_object_name(host_ref)\n if not storage_system:\n storage_system = get_storage_system(service_instance, host_ref,\n hostname)\n try:\n device_info = storage_system.storageDeviceInfo\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n if not device_info:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Host\\'s \\'{0}\\' storage device '\n 'info was not retrieved'.format(hostname))\n multipath_info = device_info.multipathInfo\n if not multipath_info:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Host\\'s \\'{0}\\' multipath info was not retrieved'\n ''.format(hostname))\n if multipath_info.lun is None:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'No luns were retrieved from host \\'{0}\\''.format(hostname))\n lun_key_by_scsi_addr = {}\n for l in multipath_info.lun:\n # The vmware scsi_address may have multiple comma separated values\n # The first one is the actual scsi address\n lun_key_by_scsi_addr.update({p.name.split(',')[0]: l.lun\n for p in l.path})\n log.trace('Scsi address to lun id map on host \\'%s\\': %s',\n hostname, lun_key_by_scsi_addr)\n return lun_key_by_scsi_addr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_all_luns(host_ref, storage_system=None, hostname=None):\n '''\n Returns a list of all vim.HostScsiDisk objects in a disk\n\n host_ref\n The vim.HostSystem object representing the host that contains the\n requested disks.\n\n storage_system\n The host's storage system. Default is None.\n\n hostname\n Name of the host. This argument is optional.\n '''\n if not hostname:\n hostname = get_managed_object_name(host_ref)\n if not storage_system:\n si = get_service_instance_from_managed_object(host_ref, name=hostname)\n storage_system = get_storage_system(si, host_ref, hostname)\n if not storage_system:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Host\\'s \\'{0}\\' storage system was not retrieved'\n ''.format(hostname))\n try:\n device_info = storage_system.storageDeviceInfo\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n if not device_info:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Host\\'s \\'{0}\\' storage device info was not retrieved'\n ''.format(hostname))\n\n scsi_luns = device_info.scsiLun\n if scsi_luns:\n log.trace('Retrieved scsi luns in host \\'%s\\': %s',\n hostname, [l.canonicalName for l in scsi_luns])\n return scsi_luns\n log.trace('Retrieved no scsi_luns in host \\'%s\\'', hostname)\n return []", "response": "Returns a list of all scsi luns in a disk."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_scsi_address_to_lun_map(host_ref, storage_system=None, hostname=None):\n '''\n Returns a map of all vim.ScsiLun objects on a ESXi host keyed by their\n scsi address\n\n host_ref\n The vim.HostSystem object representing the host that contains the\n requested disks.\n\n storage_system\n The host's storage system. Default is None.\n\n hostname\n Name of the host. This argument is optional.\n '''\n if not hostname:\n hostname = get_managed_object_name(host_ref)\n si = get_service_instance_from_managed_object(host_ref, name=hostname)\n if not storage_system:\n storage_system = get_storage_system(si, host_ref, hostname)\n lun_ids_to_scsi_addr_map = \\\n _get_scsi_address_to_lun_key_map(si, host_ref, storage_system,\n hostname)\n luns_to_key_map = {d.key: d for d in\n get_all_luns(host_ref, storage_system, hostname)}\n return {scsi_addr: luns_to_key_map[lun_key] for scsi_addr, lun_key in\n six.iteritems(lun_ids_to_scsi_addr_map)}", "response": "Returns a map of all vim. ScsiLun objects on a ESXi host keyed by their scsi address."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_disks(host_ref, disk_ids=None, scsi_addresses=None,\n get_all_disks=False):\n '''\n Returns a list of vim.HostScsiDisk objects representing disks\n in a ESXi host, filtered by their cannonical names and scsi_addresses\n\n host_ref\n The vim.HostSystem object representing the host that contains the\n requested disks.\n\n disk_ids\n The list of canonical names of the disks to be retrieved. Default value\n is None\n\n scsi_addresses\n The list of scsi addresses of the disks to be retrieved. Default value\n is None\n\n get_all_disks\n Specifies whether to retrieve all disks in the host.\n Default value is False.\n '''\n hostname = get_managed_object_name(host_ref)\n if get_all_disks:\n log.trace('Retrieving all disks in host \\'%s\\'', hostname)\n else:\n log.trace('Retrieving disks in host \\'%s\\': ids = (%s); scsi '\n 'addresses = (%s)', hostname, disk_ids, scsi_addresses)\n if not (disk_ids or scsi_addresses):\n return []\n si = get_service_instance_from_managed_object(host_ref, name=hostname)\n storage_system = get_storage_system(si, host_ref, hostname)\n disk_keys = []\n if scsi_addresses:\n # convert the scsi addresses to disk keys\n lun_key_by_scsi_addr = _get_scsi_address_to_lun_key_map(si, host_ref,\n storage_system,\n hostname)\n disk_keys = [key for scsi_addr, key\n in six.iteritems(lun_key_by_scsi_addr)\n if scsi_addr in scsi_addresses]\n log.trace('disk_keys based on scsi_addresses = %s', disk_keys)\n\n scsi_luns = get_all_luns(host_ref, storage_system)\n scsi_disks = [disk for disk in scsi_luns\n if isinstance(disk, vim.HostScsiDisk) and (\n get_all_disks or\n # Filter by canonical name\n (disk_ids and (disk.canonicalName in disk_ids)) or\n # Filter by disk keys from scsi addresses\n (disk.key in disk_keys))]\n log.trace('Retrieved disks in host \\'%s\\': %s',\n hostname, [d.canonicalName for d in scsi_disks])\n return scsi_disks", "response": "Returns a list of vim. HostScsiDisk objects representing the disks in a ESXi host filtered by canonical names and scsi_addresses."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_disk_partition_info(host_ref, disk_id, storage_system=None):\n '''\n Returns all partitions on a disk\n\n host_ref\n The reference of the ESXi host containing the disk\n\n disk_id\n The canonical name of the disk whose partitions are to be removed\n\n storage_system\n The ESXi host's storage system. Default is None.\n '''\n hostname = get_managed_object_name(host_ref)\n service_instance = get_service_instance_from_managed_object(host_ref)\n if not storage_system:\n storage_system = get_storage_system(service_instance, host_ref,\n hostname)\n\n props = get_properties_of_managed_object(storage_system,\n ['storageDeviceInfo.scsiLun'])\n if not props.get('storageDeviceInfo.scsiLun'):\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'No devices were retrieved in host \\'{0}\\''.format(hostname))\n log.trace(\n '[%s] Retrieved %s devices: %s',\n hostname,\n len(props['storageDeviceInfo.scsiLun']),\n ', '.join([l.canonicalName\n for l in props['storageDeviceInfo.scsiLun']])\n )\n disks = [l for l in props['storageDeviceInfo.scsiLun']\n if isinstance(l, vim.HostScsiDisk) and\n l.canonicalName == disk_id]\n if not disks:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Disk \\'{0}\\' was not found in host \\'{1}\\''\n ''.format(disk_id, hostname))\n log.trace('[%s] device_path = %s', hostname, disks[0].devicePath)\n partition_info = _get_partition_info(storage_system, disks[0].devicePath)\n log.trace('[%s] Retrieved %s partition(s) on disk \\'%s\\'',\n hostname, len(partition_info.spec.partition), disk_id)\n return partition_info", "response": "Returns all partitions on a disk"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef erase_disk_partitions(service_instance, host_ref, disk_id,\n hostname=None, storage_system=None):\n '''\n Erases all partitions on a disk\n\n in a vcenter filtered by their names and/or datacenter, cluster membership\n\n service_instance\n The Service Instance Object from which to obtain all information\n\n host_ref\n The reference of the ESXi host containing the disk\n\n disk_id\n The canonical name of the disk whose partitions are to be removed\n\n hostname\n The ESXi hostname. Default is None.\n\n storage_system\n The ESXi host's storage system. Default is None.\n '''\n\n if not hostname:\n hostname = get_managed_object_name(host_ref)\n if not storage_system:\n storage_system = get_storage_system(service_instance, host_ref,\n hostname)\n\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='configManager.storageSystem',\n type=vim.HostSystem,\n skip=False)\n results = get_mors_with_properties(service_instance,\n vim.HostStorageSystem,\n ['storageDeviceInfo.scsiLun'],\n container_ref=host_ref,\n traversal_spec=traversal_spec)\n if not results:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Host\\'s \\'{0}\\' devices were not retrieved'.format(hostname))\n log.trace(\n '[%s] Retrieved %s devices: %s',\n hostname,\n len(results[0].get('storageDeviceInfo.scsiLun', [])),\n ', '.join([l.canonicalName for l in\n results[0].get('storageDeviceInfo.scsiLun', [])])\n )\n disks = [l for l in results[0].get('storageDeviceInfo.scsiLun', [])\n if isinstance(l, vim.HostScsiDisk) and\n l.canonicalName == disk_id]\n if not disks:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Disk \\'{0}\\' was not found in host \\'{1}\\''\n ''.format(disk_id, hostname))\n log.trace('[%s] device_path = %s', hostname, disks[0].devicePath)\n # Erase the partitions by setting an empty partition spec\n try:\n storage_system.UpdateDiskPartitions(disks[0].devicePath,\n vim.HostDiskPartitionSpec())\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n log.trace('[%s] Erased partitions on disk \\'%s\\'', hostname, disk_id)", "response": "Erases all partitions on a disk in a vcenter filtered by their names and or datacenter"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of vim. VsanHostDiskMapping objects representing the disks in a ESXi host filtered by their cannonical names.", "response": "def get_diskgroups(host_ref, cache_disk_ids=None, get_all_disk_groups=False):\n '''\n Returns a list of vim.VsanHostDiskMapping objects representing disks\n in a ESXi host, filtered by their cannonical names.\n\n host_ref\n The vim.HostSystem object representing the host that contains the\n requested disks.\n\n cache_disk_ids\n The list of cannonical names of the cache disks to be retrieved. The\n canonical name of the cache disk is enough to identify the disk group\n because it is guaranteed to have one and only one cache disk.\n Default is None.\n\n get_all_disk_groups\n Specifies whether to retrieve all disks groups in the host.\n Default value is False.\n '''\n hostname = get_managed_object_name(host_ref)\n if get_all_disk_groups:\n log.trace('Retrieving all disk groups on host \\'%s\\'', hostname)\n else:\n log.trace('Retrieving disk groups from host \\'%s\\', with cache disk '\n 'ids : (%s)', hostname, cache_disk_ids)\n if not cache_disk_ids:\n return []\n try:\n vsan_host_config = host_ref.config.vsanHostConfig\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n if not vsan_host_config:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'No host config found on host \\'{0}\\''.format(hostname))\n vsan_storage_info = vsan_host_config.storageInfo\n if not vsan_storage_info:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'No vsan storage info found on host \\'{0}\\''.format(hostname))\n vsan_disk_mappings = vsan_storage_info.diskMapping\n if not vsan_disk_mappings:\n return []\n disk_groups = [dm for dm in vsan_disk_mappings if\n (get_all_disk_groups or\n (dm.ssd.canonicalName in cache_disk_ids))]\n log.trace(\n 'Retrieved disk groups on host \\'%s\\', with cache disk ids : %s',\n hostname, [d.ssd.canonicalName for d in disk_groups]\n )\n return disk_groups"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _check_disks_in_diskgroup(disk_group, cache_disk_id, capacity_disk_ids):\n '''\n Checks that the disks in a disk group are as expected and raises\n CheckError exceptions if the check fails\n '''\n if not disk_group.ssd.canonicalName == cache_disk_id:\n raise salt.exceptions.ArgumentValueError(\n 'Incorrect diskgroup cache disk; got id: \\'{0}\\'; expected id: '\n '\\'{1}\\''.format(disk_group.ssd.canonicalName, cache_disk_id))\n non_ssd_disks = [d.canonicalName for d in disk_group.nonSsd]\n if sorted(non_ssd_disks) != sorted(capacity_disk_ids):\n raise salt.exceptions.ArgumentValueError(\n 'Incorrect capacity disks; got ids: \\'{0}\\'; expected ids: \\'{1}\\''\n ''.format(sorted(non_ssd_disks),\n sorted(capacity_disk_ids)))\n log.trace('Checked disks in diskgroup with cache disk id \\'%s\\'',\n cache_disk_id)\n return True", "response": "Checks that the disks in a disk group are as expected and raises a CheckError exception if the check fails"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_host_cache(host_ref, host_cache_manager=None):\n '''\n Returns a vim.HostScsiDisk if the host cache is configured on the specified\n host, other wise returns None\n\n host_ref\n The vim.HostSystem object representing the host that contains the\n requested disks.\n\n host_cache_manager\n The vim.HostCacheConfigurationManager object representing the cache\n configuration manager on the specified host. Default is None. If None,\n it will be retrieved in the method\n '''\n hostname = get_managed_object_name(host_ref)\n service_instance = get_service_instance_from_managed_object(host_ref)\n log.trace('Retrieving the host cache on host \\'%s\\'', hostname)\n if not host_cache_manager:\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='configManager.cacheConfigurationManager',\n type=vim.HostSystem,\n skip=False)\n results = get_mors_with_properties(service_instance,\n vim.HostCacheConfigurationManager,\n ['cacheConfigurationInfo'],\n container_ref=host_ref,\n traversal_spec=traversal_spec)\n if not results or not results[0].get('cacheConfigurationInfo'):\n log.trace('Host \\'%s\\' has no host cache', hostname)\n return None\n return results[0]['cacheConfigurationInfo'][0]\n else:\n results = get_properties_of_managed_object(host_cache_manager,\n ['cacheConfigurationInfo'])\n if not results:\n log.trace('Host \\'%s\\' has no host cache', hostname)\n return None\n return results['cacheConfigurationInfo'][0]", "response": "Returns a vim. HostScsiDisk object representing the host that contains the requested disks."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconfigure the host cache on the specified host.", "response": "def configure_host_cache(host_ref, datastore_ref, swap_size_MiB,\n host_cache_manager=None):\n '''\n Configures the host cahe of the specified host\n\n host_ref\n The vim.HostSystem object representing the host that contains the\n requested disks.\n\n datastore_ref\n The vim.Datastore opject representing the datastore the host cache will\n be configured on.\n\n swap_size_MiB\n The size in Mibibytes of the swap.\n\n host_cache_manager\n The vim.HostCacheConfigurationManager object representing the cache\n configuration manager on the specified host. Default is None. If None,\n it will be retrieved in the method\n '''\n hostname = get_managed_object_name(host_ref)\n if not host_cache_manager:\n props = get_properties_of_managed_object(\n host_ref, ['configManager.cacheConfigurationManager'])\n if not props.get('configManager.cacheConfigurationManager'):\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'Host \\'{0}\\' has no host cache'.format(hostname))\n host_cache_manager = props['configManager.cacheConfigurationManager']\n log.trace('Configuring the host cache on host \\'%s\\', datastore \\'%s\\', '\n 'swap size=%s MiB', hostname, datastore_ref.name, swap_size_MiB)\n\n spec = vim.HostCacheConfigurationSpec(\n datastore=datastore_ref,\n swapSize=swap_size_MiB)\n log.trace('host_cache_spec=%s', spec)\n try:\n task = host_cache_manager.ConfigureHostCache_Task(spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{0}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n wait_for_task(task, hostname, 'HostCacheConfigurationTask')\n log.trace('Configured host cache on host \\'%s\\'', hostname)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a list of resource pools that are available in the service instance.", "response": "def get_resource_pools(service_instance, resource_pool_names, datacenter_name=None,\n get_all_resource_pools=False):\n '''\n Retrieves resource pool objects\n\n service_instance\n The service instance object to query the vCenter\n\n resource_pool_names\n Resource pool names\n\n datacenter_name\n Name of the datacenter where the resource pool is available\n\n get_all_resource_pools\n Boolean\n\n return\n Resourcepool managed object reference\n '''\n\n properties = ['name']\n if not resource_pool_names:\n resource_pool_names = []\n if datacenter_name:\n container_ref = get_datacenter(service_instance, datacenter_name)\n else:\n container_ref = get_root_folder(service_instance)\n\n resource_pools = get_mors_with_properties(service_instance,\n vim.ResourcePool,\n container_ref=container_ref,\n property_list=properties)\n\n selected_pools = []\n for pool in resource_pools:\n if get_all_resource_pools or (pool['name'] in resource_pool_names):\n selected_pools.append(pool['object'])\n if not selected_pools:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'The resource pools with properties '\n 'names={} get_all={} could not be found'.format(selected_pools,\n get_all_resource_pools))\n\n return selected_pools"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef wait_for_task(task, instance_name, task_type, sleep_seconds=1, log_level='debug'):\n '''\n Waits for a task to be completed.\n\n task\n The task to wait for.\n\n instance_name\n The name of the ESXi host, vCenter Server, or Virtual Machine that\n the task is being run on.\n\n task_type\n The type of task being performed. Useful information for debugging purposes.\n\n sleep_seconds\n The number of seconds to wait before querying the task again.\n Defaults to ``1`` second.\n\n log_level\n The level at which to log task information. Default is ``debug``,\n but ``info`` is also supported.\n '''\n time_counter = 0\n start_time = time.time()\n log.trace('task = %s, task_type = %s', task, task.__class__.__name__)\n try:\n task_info = task.info\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.FileNotFound as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareFileNotFoundError(exc.msg)\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n while task_info.state == 'running' or task_info.state == 'queued':\n if time_counter % sleep_seconds == 0:\n msg = '[ {0} ] Waiting for {1} task to finish [{2} s]'.format(\n instance_name, task_type, time_counter)\n if log_level == 'info':\n log.info(msg)\n else:\n log.debug(msg)\n time.sleep(1.0 - ((time.time() - start_time) % 1.0))\n time_counter += 1\n try:\n task_info = task.info\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.FileNotFound as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareFileNotFoundError(exc.msg)\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n if task_info.state == 'success':\n msg = '[ {0} ] Successfully completed {1} task in {2} seconds'.format(\n instance_name, task_type, time_counter)\n if log_level == 'info':\n log.info(msg)\n else:\n log.debug(msg)\n # task is in a successful state\n return task_info.result\n else:\n # task is in an error state\n try:\n raise task_info.error\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.FileNotFound as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareFileNotFoundError(exc.msg)\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.fault.SystemError as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareSystemError(exc.msg)\n except vmodl.fault.InvalidArgument as exc:\n log.exception(exc)\n exc_message = exc.msg\n if exc.faultMessage:\n exc_message = '{0} ({1})'.format(exc_message,\n exc.faultMessage[0].message)\n raise salt.exceptions.VMwareApiError(exc_message)", "response": "Waits for a task to finish."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_vm_by_property(service_instance, name, datacenter=None, vm_properties=None,\n traversal_spec=None, parent_ref=None):\n '''\n Get virtual machine properties based on the traversal specs and properties list,\n returns Virtual Machine object with properties.\n\n service_instance\n Service instance object to access vCenter\n\n name\n Name of the virtual machine.\n\n datacenter\n Datacenter name\n\n vm_properties\n List of vm properties.\n\n traversal_spec\n Traversal Spec object(s) for searching.\n\n parent_ref\n Container Reference object for searching under a given object.\n '''\n if datacenter and not parent_ref:\n parent_ref = salt.utils.vmware.get_datacenter(service_instance, datacenter)\n if not vm_properties:\n vm_properties = ['name',\n 'config.hardware.device',\n 'summary.storage.committed',\n 'summary.storage.uncommitted',\n 'summary.storage.unshared',\n 'layoutEx.file',\n 'config.guestFullName',\n 'config.guestId',\n 'guest.net',\n 'config.hardware.memoryMB',\n 'config.hardware.numCPU',\n 'config.files.vmPathName',\n 'summary.runtime.powerState',\n 'guest.toolsStatus']\n vm_list = salt.utils.vmware.get_mors_with_properties(service_instance,\n vim.VirtualMachine,\n vm_properties,\n container_ref=parent_ref,\n traversal_spec=traversal_spec)\n vm_formatted = [vm for vm in vm_list if vm['name'] == name]\n if not vm_formatted:\n raise salt.exceptions.VMwareObjectRetrievalError('The virtual machine was not found.')\n elif len(vm_formatted) > 1:\n raise salt.exceptions.VMwareMultipleObjectsError(' '.join([\n 'Multiple virtual machines were found with the'\n 'same name, please specify a container.']))\n return vm_formatted[0]", "response": "Get virtual machine properties based on the properties list and the properties list."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_folder(service_instance, datacenter, placement, base_vm_name=None):\n '''\n Returns a Folder Object\n\n service_instance\n Service instance object\n\n datacenter\n Name of the datacenter\n\n placement\n Placement dictionary\n\n base_vm_name\n Existing virtual machine name (for cloning)\n '''\n log.trace('Retrieving folder information')\n if base_vm_name:\n vm_object = get_vm_by_property(service_instance, base_vm_name, vm_properties=['name'])\n vm_props = salt.utils.vmware.get_properties_of_managed_object(vm_object, properties=['parent'])\n if 'parent' in vm_props:\n folder_object = vm_props['parent']\n else:\n raise salt.exceptions.VMwareObjectRetrievalError(' '.join([\n 'The virtual machine parent',\n 'object is not defined']))\n elif 'folder' in placement:\n folder_objects = salt.utils.vmware.get_folders(service_instance, [placement['folder']], datacenter)\n if len(folder_objects) > 1:\n raise salt.exceptions.VMwareMultipleObjectsError(' '.join([\n 'Multiple instances are available of the',\n 'specified folder {0}'.format(placement['folder'])]))\n folder_object = folder_objects[0]\n elif datacenter:\n datacenter_object = salt.utils.vmware.get_datacenter(service_instance, datacenter)\n dc_props = salt.utils.vmware.get_properties_of_managed_object(datacenter_object, properties=['vmFolder'])\n if 'vmFolder' in dc_props:\n folder_object = dc_props['vmFolder']\n else:\n raise salt.exceptions.VMwareObjectRetrievalError('The datacenter vm folder object is not defined')\n return folder_object", "response": "Returns a Folder Object containing the virtual machine and the virtual machine s virtual machine folders."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a virtual machine with the specified placement information.", "response": "def get_placement(service_instance, datacenter, placement=None):\n '''\n To create a virtual machine a resource pool needs to be supplied, we would like to use the strictest as possible.\n\n datacenter\n Name of the datacenter\n\n placement\n Dictionary with the placement info, cluster, host resource pool name\n\n return\n Resource pool, cluster and host object if any applies\n '''\n log.trace('Retrieving placement information')\n resourcepool_object, placement_object = None, None\n if 'host' in placement:\n host_objects = get_hosts(service_instance, datacenter_name=datacenter, host_names=[placement['host']])\n if not host_objects:\n raise salt.exceptions.VMwareObjectRetrievalError(' '.join([\n 'The specified host',\n '{0} cannot be found.'.format(placement['host'])]))\n try:\n host_props = \\\n get_properties_of_managed_object(host_objects[0],\n properties=['resourcePool'])\n resourcepool_object = host_props['resourcePool']\n except vmodl.query.InvalidProperty:\n traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(\n path='parent',\n skip=True,\n type=vim.HostSystem,\n selectSet=[vmodl.query.PropertyCollector.TraversalSpec(\n path='resourcePool',\n skip=False,\n type=vim.ClusterComputeResource)])\n resourcepools = get_mors_with_properties(service_instance,\n vim.ResourcePool,\n container_ref=host_objects[0],\n property_list=['name'],\n traversal_spec=traversal_spec)\n if resourcepools:\n resourcepool_object = resourcepools[0]['object']\n else:\n raise salt.exceptions.VMwareObjectRetrievalError(\n 'The resource pool of host {0} cannot be found.'.format(placement['host']))\n placement_object = host_objects[0]\n elif 'resourcepool' in placement:\n resourcepool_objects = get_resource_pools(service_instance,\n [placement['resourcepool']],\n datacenter_name=datacenter)\n if len(resourcepool_objects) > 1:\n raise salt.exceptions.VMwareMultipleObjectsError(' '.join([\n 'Multiple instances are available of the',\n 'specified host {}.'.format(placement['host'])]))\n resourcepool_object = resourcepool_objects[0]\n res_props = get_properties_of_managed_object(resourcepool_object,\n properties=['parent'])\n if 'parent' in res_props:\n placement_object = res_props['parent']\n else:\n raise salt.exceptions.VMwareObjectRetrievalError(' '.join([\n 'The resource pool\\'s parent',\n 'object is not defined']))\n elif 'cluster' in placement:\n datacenter_object = get_datacenter(service_instance, datacenter)\n cluster_object = get_cluster(datacenter_object, placement['cluster'])\n clus_props = get_properties_of_managed_object(cluster_object,\n properties=['resourcePool'])\n if 'resourcePool' in clus_props:\n resourcepool_object = clus_props['resourcePool']\n else:\n raise salt.exceptions.VMwareObjectRetrievalError(' '.join([\n 'The cluster\\'s resource pool',\n 'object is not defined']))\n placement_object = cluster_object\n else:\n # We are checking the schema for this object, this exception should never be raised\n raise salt.exceptions.VMwareObjectRetrievalError(' '.join([\n 'Placement is not defined.']))\n return (resourcepool_object, placement_object)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nconvert the given size to KB based on the unit returns a long integer.", "response": "def convert_to_kb(unit, size):\n '''\n Converts the given size to KB based on the unit, returns a long integer.\n\n unit\n Unit of the size eg. GB; Note: to VMware a GB is the same as GiB = 1024MiB\n size\n Number which represents the size\n '''\n if unit.lower() == 'gb':\n # vCenter needs long value\n target_size = int(size * 1024 * 1024)\n elif unit.lower() == 'mb':\n target_size = int(size * 1024)\n elif unit.lower() == 'kb':\n target_size = int(size)\n else:\n raise salt.exceptions.ArgumentValueError('The unit is not specified')\n return {'size': target_size, 'unit': 'KB'}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef power_cycle_vm(virtual_machine, action='on'):\n '''\n Powers on/off a virtual machine specified by it's name.\n\n virtual_machine\n vim.VirtualMachine object to power on/off virtual machine\n\n action\n Operation option to power on/off the machine\n '''\n if action == 'on':\n try:\n task = virtual_machine.PowerOn()\n task_name = 'power on'\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n elif action == 'off':\n try:\n task = virtual_machine.PowerOff()\n task_name = 'power off'\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n else:\n raise salt.exceptions.ArgumentValueError('The given action is not supported')\n try:\n wait_for_task(task, get_managed_object_name(virtual_machine), task_name)\n except salt.exceptions.VMwareFileNotFoundError as exc:\n raise salt.exceptions.VMwarePowerOnError(' '.join([\n 'An error occurred during power',\n 'operation, a file was not found: {0}'.format(exc)]))\n return virtual_machine", "response": "Power cycle a virtual machine."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate virtual machine from config spec", "response": "def create_vm(vm_name, vm_config_spec, folder_object, resourcepool_object, host_object=None):\n '''\n Creates virtual machine from config spec\n\n vm_name\n Virtual machine name to be created\n\n vm_config_spec\n Virtual Machine Config Spec object\n\n folder_object\n vm Folder managed object reference\n\n resourcepool_object\n Resource pool object where the machine will be created\n\n host_object\n Host object where the machine will ne placed (optional)\n\n return\n Virtual Machine managed object reference\n '''\n try:\n if host_object and isinstance(host_object, vim.HostSystem):\n task = folder_object.CreateVM_Task(vm_config_spec,\n pool=resourcepool_object,\n host=host_object)\n else:\n task = folder_object.CreateVM_Task(vm_config_spec,\n pool=resourcepool_object)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n vm_object = wait_for_task(task, vm_name, 'CreateVM Task', 10, 'info')\n return vm_object"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef register_vm(datacenter, name, vmx_path, resourcepool_object, host_object=None):\n '''\n Registers a virtual machine to the inventory with the given vmx file, on success\n it returns the vim.VirtualMachine managed object reference\n\n datacenter\n Datacenter object of the virtual machine, vim.Datacenter object\n\n name\n Name of the virtual machine\n\n vmx_path:\n Full path to the vmx file, datastore name should be included\n\n resourcepool\n Placement resource pool of the virtual machine, vim.ResourcePool object\n\n host\n Placement host of the virtual machine, vim.HostSystem object\n '''\n try:\n if host_object:\n task = datacenter.vmFolder.RegisterVM_Task(path=vmx_path, name=name,\n asTemplate=False,\n host=host_object,\n pool=resourcepool_object)\n else:\n task = datacenter.vmFolder.RegisterVM_Task(path=vmx_path, name=name,\n asTemplate=False,\n pool=resourcepool_object)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n try:\n vm_ref = wait_for_task(task, name, 'RegisterVM Task')\n except salt.exceptions.VMwareFileNotFoundError as exc:\n raise salt.exceptions.VMwareVmRegisterError(\n 'An error occurred during registration operation, the '\n 'configuration file was not found: {0}'.format(exc))\n return vm_ref", "response": "Registers a virtual machine to the inventory with the given vmx file."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates the virtual machine configuration with the given object", "response": "def update_vm(vm_ref, vm_config_spec):\n '''\n Updates the virtual machine configuration with the given object\n\n vm_ref\n Virtual machine managed object reference\n\n vm_config_spec\n Virtual machine config spec object to update\n '''\n vm_name = get_managed_object_name(vm_ref)\n log.trace('Updating vm \\'%s\\'', vm_name)\n try:\n task = vm_ref.ReconfigVM_Task(vm_config_spec)\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareRuntimeError(exc.msg)\n vm_ref = wait_for_task(task, vm_name, 'ReconfigureVM Task')\n return vm_ref"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef unregister_vm(vm_ref):\n '''\n Destroys the virtual machine\n\n vm_ref\n Managed object reference of a virtual machine object\n '''\n vm_name = get_managed_object_name(vm_ref)\n log.trace('Destroying vm \\'%s\\'', vm_name)\n try:\n vm_ref.UnregisterVM()\n except vim.fault.NoPermission as exc:\n log.exception(exc)\n raise salt.exceptions.VMwareApiError(\n 'Not enough permissions. Required privilege: '\n '{}'.format(exc.privilegeId))\n except vim.fault.VimFault as exc:\n raise salt.exceptions.VMwareApiError(exc.msg)\n except vmodl.RuntimeFault as exc:\n raise salt.exceptions.VMwareRuntimeError(exc.msg)", "response": "Destroys the virtual machine object holding the specified virtual machine object reference."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef auth(username, password, **kwargs):\n '''\n Returns True if the given user cert (password is the cert contents)\n was issued by the CA and if cert's Common Name is equal to username.\n\n Returns False otherwise.\n\n ``username``: we need it to run the auth function from CLI/API;\n it should be in master config auth/acl\n ``password``: contents of user certificate (pem-encoded user public key);\n why \"password\"? For CLI, it's the only available name\n\n Configure the CA cert in the master config file:\n\n .. code-block:: yaml\n\n external_auth:\n pki:\n ca_file: /etc/pki/tls/ca_certs/trusted-ca.crt\n your_user:\n - .*\n '''\n pem = password\n cacert_file = __salt__['config.get']('external_auth:pki:ca_file')\n\n log.debug('Attempting to authenticate via pki.')\n log.debug('Using CA file: %s', cacert_file)\n log.debug('Certificate contents: %s', pem)\n\n if HAS_M2:\n cert = X509.load_cert_string(pem, X509.FORMAT_PEM)\n cacert = X509.load_cert(cacert_file, X509.FORMAT_PEM)\n if cert.verify(cacert.get_pubkey()):\n log.info('Successfully authenticated certificate: %s', pem)\n return True\n else:\n log.info('Failed to authenticate certificate: %s', pem)\n return False\n\n c = OpenSSL.crypto\n cert = c.load_certificate(c.FILETYPE_PEM, pem)\n\n with salt.utils.files.fopen(cacert_file) as f:\n cacert = c.load_certificate(c.FILETYPE_PEM, f.read())\n\n # Get the signing algorithm\n algo = cert.get_signature_algorithm()\n\n # Get the ASN1 format of the certificate\n cert_asn1 = c.dump_certificate(c.FILETYPE_ASN1, cert)\n\n # Decode the certificate\n der = asn1.DerSequence()\n der.decode(cert_asn1)\n\n # The certificate has three parts:\n # - certificate\n # - signature algorithm\n # - signature\n # http://usefulfor.com/nothing/2009/06/10/x509-certificate-basics/\n der_cert = der[0]\n #der_algo = der[1]\n der_sig = der[2]\n\n # The signature is a BIT STRING (Type 3)\n # Decode that as well\n der_sig_in = asn1.DerObject()\n der_sig_in.decode(der_sig)\n\n # Get the payload\n sig0 = der_sig_in.payload\n\n # Do the following to see a validation error for tests\n # der_cert=der_cert[:20]+'1'+der_cert[21:]\n\n # First byte is the number of unused bits. This should be 0\n # http://msdn.microsoft.com/en-us/library/windows/desktop/bb540792(v=vs.85).aspx\n if sig0[0] != '\\x00':\n raise Exception('Number of unused bits is strange')\n # Now get the signature itself\n sig = sig0[1:]\n\n # And verify the certificate\n try:\n c.verify(cacert, sig, der_cert, algo)\n assert dict(cert.get_subject().get_components())['CN'] == username, \"Certificate's CN should match the username\"\n log.info('Successfully authenticated certificate: %s', pem)\n return True\n except (OpenSSL.crypto.Error, AssertionError):\n log.info('Failed to authenticate certificate: %s', pem)\n return False", "response": "Authenticate the user with the given username and password."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a dict with the differences between dict1 and dict2", "response": "def _get_diffs(cls, dict1, dict2, ignore_missing_keys):\n '''\n Returns a dict with the differences between dict1 and dict2\n\n Notes:\n Keys that only exist in dict2 are not included in the diff if\n ignore_missing_keys is True, otherwise they are\n Simple compares are done on lists\n '''\n ret_dict = {}\n for p in dict1.keys():\n if p not in dict2:\n ret_dict.update({p: {'new': dict1[p], 'old': cls.NONE_VALUE}})\n elif dict1[p] != dict2[p]:\n if isinstance(dict1[p], dict) and isinstance(dict2[p], dict):\n sub_diff_dict = cls._get_diffs(dict1[p], dict2[p],\n ignore_missing_keys)\n if sub_diff_dict:\n ret_dict.update({p: sub_diff_dict})\n else:\n ret_dict.update({p: {'new': dict1[p], 'old': dict2[p]}})\n if not ignore_missing_keys:\n for p in dict2.keys():\n if p not in dict1.keys():\n ret_dict.update({p: {'new': cls.NONE_VALUE,\n 'old': dict2[p]}})\n return ret_dict"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_values(cls, diff_dict, type='new'):\n '''\n Returns a dictionaries with the 'new' values in a diff dict.\n\n type\n Which values to return, 'new' or 'old'\n '''\n ret_dict = {}\n for p in diff_dict.keys():\n if type in diff_dict[p].keys():\n ret_dict.update({p: diff_dict[p][type]})\n else:\n ret_dict.update(\n {p: cls._get_values(diff_dict[p], type=type)})\n return ret_dict", "response": "Returns a dictionary with the new values in a diff dict."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_changes(cls, diff_dict):\n '''\n Returns a list of string message with the differences in a diff dict.\n\n Each inner difference is tabulated two space deeper\n '''\n changes_strings = []\n for p in sorted(diff_dict.keys()):\n if sorted(diff_dict[p].keys()) == ['new', 'old']:\n # Some string formatting\n old_value = diff_dict[p]['old']\n if diff_dict[p]['old'] == cls.NONE_VALUE:\n old_value = 'nothing'\n elif isinstance(diff_dict[p]['old'], six.string_types):\n old_value = '\\'{0}\\''.format(diff_dict[p]['old'])\n elif isinstance(diff_dict[p]['old'], list):\n old_value = '\\'{0}\\''.format(\n ', '.join(diff_dict[p]['old']))\n new_value = diff_dict[p]['new']\n if diff_dict[p]['new'] == cls.NONE_VALUE:\n new_value = 'nothing'\n elif isinstance(diff_dict[p]['new'], six.string_types):\n new_value = '\\'{0}\\''.format(diff_dict[p]['new'])\n elif isinstance(diff_dict[p]['new'], list):\n new_value = '\\'{0}\\''.format(', '.join(diff_dict[p]['new']))\n changes_strings.append('{0} from {1} to {2}'.format(\n p, old_value, new_value))\n else:\n sub_changes = cls._get_changes(diff_dict[p])\n if sub_changes:\n changes_strings.append('{0}:'.format(p))\n changes_strings.extend([' {0}'.format(c)\n for c in sub_changes])\n return changes_strings", "response": "Returns a list of string message with the differences in a diff dict. Each element in the list is a tabulated two space deeper. Each element in the list is a tabulated two space deeper."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns all keys that have been added.", "response": "def added(self):\n '''\n Returns all keys that have been added.\n\n If the keys are in child dictionaries they will be represented with\n . notation\n '''\n def _added(diffs, prefix):\n keys = []\n for key in diffs.keys():\n if isinstance(diffs[key], dict) and 'old' not in diffs[key]:\n keys.extend(_added(diffs[key],\n prefix='{0}{1}.'.format(prefix, key)))\n elif diffs[key]['old'] == self.NONE_VALUE:\n if isinstance(diffs[key]['new'], dict):\n keys.extend(\n _added(diffs[key]['new'],\n prefix='{0}{1}.'.format(prefix, key)))\n else:\n keys.append('{0}{1}'.format(prefix, key))\n return keys\n\n return sorted(_added(self._diffs, prefix=''))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns all keys that have been removed.", "response": "def removed(self):\n '''\n Returns all keys that have been removed.\n\n If the keys are in child dictionaries they will be represented with\n . notation\n '''\n def _removed(diffs, prefix):\n keys = []\n for key in diffs.keys():\n if isinstance(diffs[key], dict) and 'old' not in diffs[key]:\n keys.extend(_removed(diffs[key],\n prefix='{0}{1}.'.format(prefix, key)))\n elif diffs[key]['new'] == self.NONE_VALUE:\n keys.append('{0}{1}'.format(prefix, key))\n elif isinstance(diffs[key]['new'], dict):\n keys.extend(\n _removed(diffs[key]['new'],\n prefix='{0}{1}.'.format(prefix, key)))\n return keys\n\n return sorted(_removed(self._diffs, prefix=''))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef changed(self):\n '''\n Returns all keys that have been changed.\n\n If the keys are in child dictionaries they will be represented with\n . notation\n '''\n def _changed(diffs, prefix):\n keys = []\n for key in diffs.keys():\n if not isinstance(diffs[key], dict):\n continue\n\n if isinstance(diffs[key], dict) and 'old' not in diffs[key]:\n keys.extend(_changed(diffs[key],\n prefix='{0}{1}.'.format(prefix, key)))\n continue\n if self.ignore_unset_values:\n if 'old' in diffs[key] and 'new' in diffs[key] and \\\n diffs[key]['old'] != self.NONE_VALUE and \\\n diffs[key]['new'] != self.NONE_VALUE:\n if isinstance(diffs[key]['new'], dict):\n keys.extend(\n _changed(diffs[key]['new'],\n prefix='{0}{1}.'.format(prefix, key)))\n else:\n keys.append('{0}{1}'.format(prefix, key))\n elif isinstance(diffs[key], dict):\n keys.extend(\n _changed(diffs[key],\n prefix='{0}{1}.'.format(prefix, key)))\n else:\n if 'old' in diffs[key] and 'new' in diffs[key]:\n if isinstance(diffs[key]['new'], dict):\n keys.extend(\n _changed(diffs[key]['new'],\n prefix='{0}{1}.'.format(prefix, key)))\n else:\n keys.append('{0}{1}'.format(prefix, key))\n elif isinstance(diffs[key], dict):\n keys.extend(\n _changed(diffs[key],\n prefix='{0}{1}.'.format(prefix, key)))\n\n return keys\n\n return sorted(_changed(self._diffs, prefix=''))", "response": "Returns all keys that have been changed."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning all keys that have been unchanged.", "response": "def unchanged(self):\n '''\n Returns all keys that have been unchanged.\n\n If the keys are in child dictionaries they will be represented with\n . notation\n '''\n def _unchanged(current_dict, diffs, prefix):\n keys = []\n for key in current_dict.keys():\n if key not in diffs:\n keys.append('{0}{1}'.format(prefix, key))\n elif isinstance(current_dict[key], dict):\n if 'new' in diffs[key]:\n # There is a diff\n continue\n else:\n keys.extend(\n _unchanged(current_dict[key],\n diffs[key],\n prefix='{0}{1}.'.format(prefix, key)))\n\n return keys\n return sorted(_unchanged(self.current_dict, self._diffs, prefix=''))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_computer_object():\n '''\n A helper function to get the object for the local machine\n\n Returns:\n object: Returns the computer object for the local machine\n '''\n with salt.utils.winapi.Com():\n nt = win32com.client.Dispatch('AdsNameSpaces')\n return nt.GetObject('', 'WinNT://.,computer')", "response": "A helper function to get the object for the local machine\n Returns the object for the local machine\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_group_object(name):\n '''\n A helper function to get a specified group object\n\n Args:\n\n name (str): The name of the object\n\n Returns:\n object: The specified group object\n '''\n with salt.utils.winapi.Com():\n nt = win32com.client.Dispatch('AdsNameSpaces')\n return nt.GetObject('', 'WinNT://./' + name + ',group')", "response": "A helper function to get a specified group object"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_all_groups():\n '''\n A helper function that gets a list of group objects for all groups on the\n machine\n\n Returns:\n iter: A list of objects for all groups on the machine\n '''\n with salt.utils.winapi.Com():\n nt = win32com.client.Dispatch('AdsNameSpaces')\n results = nt.GetObject('', 'WinNT://.')\n results.Filter = ['group']\n return results", "response": "A helper function that gets a list of group objects for all groups on the\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nadd the specified group to the specified group.", "response": "def add(name, **kwargs):\n '''\n Add the specified group\n\n Args:\n\n name (str):\n The name of the group to add\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.add foo\n '''\n if not info(name):\n comp_obj = _get_computer_object()\n try:\n new_group = comp_obj.Create('group', name)\n new_group.SetInfo()\n log.info('Successfully created group %s', name)\n except pywintypes.com_error as exc:\n msg = 'Failed to create group {0}. {1}'.format(\n name, win32api.FormatMessage(exc.excepinfo[5]))\n log.error(msg)\n return False\n else:\n log.warning('The group %s already exists.', name)\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nremoves the named group from the database", "response": "def delete(name, **kwargs):\n '''\n Remove the named group\n\n Args:\n\n name (str):\n The name of the group to remove\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.delete foo\n '''\n if info(name):\n comp_obj = _get_computer_object()\n try:\n comp_obj.Delete('group', name)\n log.info('Successfully removed group %s', name)\n except pywintypes.com_error as exc:\n msg = 'Failed to remove group {0}. {1}'.format(\n name, win32api.FormatMessage(exc.excepinfo[5]))\n log.error(msg)\n return False\n else:\n log.warning('The group %s does not exists.', name)\n return False\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns information about a group Taxonomy", "response": "def info(name):\n '''\n Return information about a group\n\n Args:\n\n name (str):\n The name of the group for which to get information\n\n Returns:\n dict: A dictionary of information about the group\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.info foo\n '''\n try:\n groupObj = _get_group_object(name)\n gr_name = groupObj.Name\n gr_mem = [_get_username(x) for x in groupObj.members()]\n except pywintypes.com_error as exc:\n msg = 'Failed to access group {0}. {1}'.format(\n name, win32api.FormatMessage(exc.excepinfo[5]))\n log.debug(msg)\n return False\n\n if not gr_name:\n return False\n\n return {'name': gr_name,\n 'passwd': None,\n 'gid': None,\n 'members': gr_mem}"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef getent(refresh=False):\n '''\n Return info on all groups\n\n Args:\n\n refresh (bool):\n Refresh the info for all groups in ``__context__``. If False only\n the groups in ``__context__`` will be returned. If True the\n ``__context__`` will be refreshed with current data and returned.\n Default is False\n\n Returns:\n A list of groups and their information\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.getent\n '''\n if 'group.getent' in __context__ and not refresh:\n return __context__['group.getent']\n\n ret = []\n\n results = _get_all_groups()\n\n for result in results:\n group = {'gid': __salt__['file.group_to_gid'](result.Name),\n 'members': [_get_username(x) for x in result.members()],\n 'name': result.Name,\n 'passwd': 'x'}\n ret.append(group)\n __context__['group.getent'] = ret\n return ret", "response": "Return info on all groups and their information"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef adduser(name, username, **kwargs):\n '''\n Add a user to a group\n\n Args:\n\n name (str):\n The name of the group to modify\n\n username (str):\n The name of the user to add to the group\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.adduser foo username\n '''\n try:\n group_obj = _get_group_object(name)\n except pywintypes.com_error as exc:\n msg = 'Failed to access group {0}. {1}'.format(\n name, win32api.FormatMessage(exc.excepinfo[5]))\n log.error(msg)\n return False\n\n existing_members = [_get_username(x) for x in group_obj.members()]\n username = salt.utils.win_functions.get_sam_name(username)\n\n try:\n if username not in existing_members:\n group_obj.Add('WinNT://' + username.replace('\\\\', '/'))\n log.info('Added user %s', username)\n else:\n log.warning('User %s is already a member of %s', username, name)\n return False\n except pywintypes.com_error as exc:\n msg = 'Failed to add {0} to group {1}. {2}'.format(\n username, name, win32api.FormatMessage(exc.excepinfo[5]))\n log.error(msg)\n return False\n\n return True", "response": "Adds a user to a group"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef members(name, members_list, **kwargs):\n '''\n Ensure a group contains only the members in the list\n\n Args:\n\n name (str):\n The name of the group to modify\n\n members_list (str):\n A single user or a comma separated list of users. The group will\n contain only the users specified in this list.\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.members foo 'user1,user2,user3'\n '''\n members_list = [salt.utils.win_functions.get_sam_name(m) for m in members_list.split(\",\")]\n if not isinstance(members_list, list):\n log.debug('member_list is not a list')\n return False\n\n try:\n obj_group = _get_group_object(name)\n except pywintypes.com_error as exc:\n # Group probably doesn't exist, but we'll log the error\n msg = 'Failed to access group {0}. {1}'.format(\n name, win32api.FormatMessage(exc.excepinfo[5]))\n log.error(msg)\n return False\n\n existing_members = [_get_username(x) for x in obj_group.members()]\n existing_members.sort()\n members_list.sort()\n\n if existing_members == members_list:\n log.info('%s membership is correct', name)\n return True\n\n # add users\n success = True\n for member in members_list:\n if member not in existing_members:\n try:\n obj_group.Add('WinNT://' + member.replace('\\\\', '/'))\n log.info('User added: %s', member)\n except pywintypes.com_error as exc:\n msg = 'Failed to add {0} to {1}. {2}'.format(\n member, name, win32api.FormatMessage(exc.excepinfo[5]))\n log.error(msg)\n success = False\n\n # remove users not in members_list\n for member in existing_members:\n if member not in members_list:\n try:\n obj_group.Remove('WinNT://' + member.replace('\\\\', '/'))\n log.info('User removed: %s', member)\n except pywintypes.com_error as exc:\n msg = 'Failed to remove {0} from {1}. {2}'.format(\n member, name, win32api.FormatMessage(exc.excepinfo[5]))\n log.error(msg)\n success = False\n\n return success", "response": "Ensures a group contains only the members in the members_list."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_groups(refresh=False):\n '''\n Return a list of groups\n\n Args:\n\n refresh (bool):\n Refresh the info for all groups in ``__context__``. If False only\n the groups in ``__context__`` will be returned. If True, the\n ``__context__`` will be refreshed with current data and returned.\n Default is False\n\n Returns:\n list: A list of groups on the machine\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.list_groups\n '''\n if 'group.list_groups' in __context__ and not refresh:\n return __context__['group.list_groups']\n\n results = _get_all_groups()\n\n ret = []\n\n for result in results:\n ret.append(result.Name)\n\n __context__['group.list_groups'] = ret\n\n return ret", "response": "Return a list of groups on the machine\nAttributeNames CLI Example : bash\nAttributeNames salt '*' group. list_groups"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a function with identical signature as original_function s which Formula will call the wrapped_function.", "response": "def identical_signature_wrapper(original_function, wrapped_function):\n '''\n Return a function with identical signature as ``original_function``'s which\n will call the ``wrapped_function``.\n '''\n context = {'__wrapped__': wrapped_function}\n function_def = compile(\n 'def {0}({1}):\\n'\n ' return __wrapped__({2})'.format(\n # Keep the original function name\n original_function.__name__,\n # The function signature including defaults, i.e., 'timeout=1'\n inspect.formatargspec(\n *salt.utils.args.get_function_argspec(original_function)\n )[1:-1],\n # The function signature without the defaults\n inspect.formatargspec(\n formatvalue=lambda val: '',\n *salt.utils.args.get_function_argspec(original_function)\n )[1:-1]\n ),\n '',\n 'exec'\n )\n six.exec_(function_def, context)\n return wraps(original_function)(context[original_function.__name__])"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _stdout_list_split(retcode, stdout='', splitstring='\\n'):\n '''\n Evaulates Open vSwitch command`s retcode value.\n\n Args:\n retcode: Value of retcode field from response, should be 0, 1 or 2.\n stdout: Value of stdout filed from response.\n splitstring: String used to split the stdout default new line.\n\n Returns:\n List or False.\n '''\n if retcode == 0:\n ret = stdout.split(splitstring)\n return ret\n else:\n return False", "response": "Evaulates Open vSwitch command s retcode value from stdout filed from response."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert from the JSON output provided by ovs - vsctl into a usable Python object tree.", "response": "def _convert_json(obj):\n '''\n Converts from the JSON output provided by ovs-vsctl into a usable Python\n object tree. In particular, sets and maps are converted from lists to\n actual sets or maps.\n\n Args:\n obj: Object that shall be recursively converted.\n\n Returns:\n Converted version of object.\n '''\n if isinstance(obj, dict):\n return {_convert_json(key): _convert_json(val)\n for (key, val) in six.iteritems(obj)}\n elif isinstance(obj, list) and len(obj) == 2:\n first = obj[0]\n second = obj[1]\n if first == 'set' and isinstance(second, list):\n return [_convert_json(elem) for elem in second]\n elif first == 'map' and isinstance(second, list):\n for elem in second:\n if not isinstance(elem, list) or len(elem) != 2:\n return obj\n return {elem[0]: _convert_json(elem[1]) for elem in second}\n else:\n return obj\n elif isinstance(obj, list):\n return [_convert_json(elem) for elem in obj]\n else:\n return obj"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef bridge_list():\n '''\n Lists all existing real and fake bridges.\n\n Returns:\n List of bridges (or empty list), False on failure.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.bridge_list\n '''\n cmd = 'ovs-vsctl list-br'\n result = __salt__['cmd.run_all'](cmd)\n retcode = result['retcode']\n stdout = result['stdout']\n return _stdout_list_split(retcode, stdout)", "response": "Lists all existing real and fake bridges."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntests whether bridge exists as a real or fake bridge.", "response": "def bridge_exists(br):\n '''\n Tests whether bridge exists as a real or fake bridge.\n\n Returns:\n True if Bridge exists, else False.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.bridge_exists br0\n '''\n cmd = 'ovs-vsctl br-exists {0}'.format(br)\n result = __salt__['cmd.run_all'](cmd)\n retcode = result['retcode']\n return _retcode_to_bool(retcode)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef bridge_create(br, may_exist=True, parent=None, vlan=None):\n '''\n Creates a new bridge.\n\n Args:\n br: A string - bridge name\n may_exist: Bool, if False - attempting to create a bridge that exists returns False.\n parent: String, the name of the parent bridge (if the bridge shall be\n created as a fake bridge). If specified, vlan must also be\n specified.\n vlan: Int, the VLAN ID of the bridge (if the bridge shall be created as\n a fake bridge). If specified, parent must also be specified.\n\n Returns:\n True on success, else False.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.bridge_create br0\n '''\n param_may_exist = _param_may_exist(may_exist)\n if parent is not None and vlan is None:\n raise ArgumentValueError(\n 'If parent is specified, vlan must also be specified.')\n if vlan is not None and parent is None:\n raise ArgumentValueError(\n 'If vlan is specified, parent must also be specified.')\n param_parent = '' if parent is None else ' {0}'.format(parent)\n param_vlan = '' if vlan is None else ' {0}'.format(vlan)\n cmd = 'ovs-vsctl {1}add-br {0}{2}{3}'.format(br, param_may_exist, param_parent,\n param_vlan)\n result = __salt__['cmd.run_all'](cmd)\n return _retcode_to_bool(result['retcode'])", "response": "Creates a new bridge."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef bridge_delete(br, if_exists=True):\n '''\n Deletes bridge and all of its ports.\n\n Args:\n br: A string - bridge name\n if_exists: Bool, if False - attempting to delete a bridge that does not exist returns False.\n\n Returns:\n True on success, else False.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.bridge_delete br0\n '''\n param_if_exists = _param_if_exists(if_exists)\n cmd = 'ovs-vsctl {1}del-br {0}'.format(br, param_if_exists)\n result = __salt__['cmd.run_all'](cmd)\n retcode = result['retcode']\n return _retcode_to_bool(retcode)", "response": "Deletes a bridge and all of its ports."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the parent bridge of a bridge.", "response": "def bridge_to_parent(br):\n '''\n Returns the parent bridge of a bridge.\n\n Args:\n br: A string - bridge name\n\n Returns:\n Name of the parent bridge. This is the same as the bridge name if the\n bridge is not a fake bridge. If the bridge does not exist, False is\n returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' openvswitch.bridge_to_parent br0\n\n '''\n cmd = 'ovs-vsctl br-to-parent {0}'.format(br)\n result = __salt__['cmd.run_all'](cmd)\n if result['retcode'] != 0:\n return False\n return result['stdout']"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the VLAN ID of a bridge.", "response": "def bridge_to_vlan(br):\n '''\n Returns the VLAN ID of a bridge.\n\n Args:\n br: A string - bridge name\n\n Returns:\n VLAN ID of the bridge. The VLAN ID is 0 if the bridge is not a fake\n bridge. If the bridge does not exist, False is returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' openvswitch.bridge_to_parent br0\n '''\n cmd = 'ovs-vsctl br-to-vlan {0}'.format(br)\n result = __salt__['cmd.run_all'](cmd)\n if result['retcode'] != 0:\n return False\n return int(result['stdout'])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates on bridge a new port named port.", "response": "def port_add(br, port, may_exist=False, internal=False):\n '''\n Creates on bridge a new port named port.\n\n Returns:\n True on success, else False.\n\n Args:\n br: A string - bridge name\n port: A string - port name\n may_exist: Bool, if False - attempting to create a port that exists returns False.\n internal: A boolean to create an internal interface if one does not exist.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.port_add br0 8080\n '''\n param_may_exist = _param_may_exist(may_exist)\n cmd = 'ovs-vsctl {2}add-port {0} {1}'.format(br, port, param_may_exist)\n if internal:\n cmd += ' -- set interface {0} type=internal'.format(port)\n result = __salt__['cmd.run_all'](cmd)\n retcode = result['retcode']\n return _retcode_to_bool(retcode)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete a port from the specified bridge.", "response": "def port_remove(br, port, if_exists=True):\n '''\n Deletes port.\n\n Args:\n br: A string - bridge name (If bridge is None, port is removed from whatever bridge contains it)\n port: A string - port name.\n if_exists: Bool, if False - attempting to delete a por that does not exist returns False. (Default True)\n\n Returns:\n True on success, else False.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.port_remove br0 8080\n '''\n param_if_exists = _param_if_exists(if_exists)\n\n if port and not br:\n cmd = 'ovs-vsctl {1}del-port {0}'.format(port, param_if_exists)\n else:\n cmd = 'ovs-vsctl {2}del-port {0} {1}'.format(br, port, param_if_exists)\n result = __salt__['cmd.run_all'](cmd)\n retcode = result['retcode']\n return _retcode_to_bool(retcode)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist all of the ports within a bridge.", "response": "def port_list(br):\n '''\n Lists all of the ports within bridge.\n\n Args:\n br: A string - bridge name.\n\n Returns:\n List of bridges (or empty list), False on failure.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.port_list br0\n '''\n cmd = 'ovs-vsctl list-ports {0}'.format(br)\n result = __salt__['cmd.run_all'](cmd)\n retcode = result['retcode']\n stdout = result['stdout']\n return _stdout_list_split(retcode, stdout)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of tags of the port.", "response": "def port_get_tag(port):\n '''\n Lists tags of the port.\n\n Args:\n port: A string - port name.\n\n Returns:\n List of tags (or empty list), False on failure.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.port_get_tag tap0\n '''\n cmd = 'ovs-vsctl get port {0} tag'.format(port)\n result = __salt__['cmd.run_all'](cmd)\n retcode = result['retcode']\n stdout = result['stdout']\n return _stdout_list_split(retcode, stdout)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef port_create_vlan(br, port, id, internal=False):\n '''\n Isolate VM traffic using VLANs.\n\n Args:\n br: A string - bridge name.\n port: A string - port name.\n id: An integer in the valid range 0 to 4095 (inclusive), name of VLAN.\n internal: A boolean to create an internal interface if one does not exist.\n\n Returns:\n True on success, else False.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.port_create_vlan br0 tap0 100\n '''\n interfaces = __salt__['network.interfaces']()\n if not 0 <= id <= 4095:\n return False\n elif not bridge_exists(br):\n return False\n elif not internal and port not in interfaces:\n return False\n elif port in port_list(br):\n cmd = 'ovs-vsctl set port {0} tag={1}'.format(port, id)\n if internal:\n cmd += ' -- set interface {0} type=internal'.format(port)\n result = __salt__['cmd.run_all'](cmd)\n return _retcode_to_bool(result['retcode'])\n else:\n cmd = 'ovs-vsctl add-port {0} {1} tag={2}'.format(br, port, id)\n if internal:\n cmd += ' -- set interface {0} type=internal'.format(port)\n result = __salt__['cmd.run_all'](cmd)\n return _retcode_to_bool(result['retcode'])", "response": "Creates a VLAN on a bridge."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating GRE tunnel between endpoints.", "response": "def port_create_gre(br, port, id, remote):\n '''\n Generic Routing Encapsulation - creates GRE tunnel between endpoints.\n\n Args:\n br: A string - bridge name.\n port: A string - port name.\n id: An integer - unsigned 32-bit number, tunnel's key.\n remote: A string - remote endpoint's IP address.\n\n Returns:\n True on success, else False.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.port_create_gre br0 gre1 5001 192.168.1.10\n '''\n if not 0 <= id < 2**32:\n return False\n elif not __salt__['dig.check_ip'](remote):\n return False\n elif not bridge_exists(br):\n return False\n elif port in port_list(br):\n cmd = 'ovs-vsctl set interface {0} type=gre options:remote_ip={1} options:key={2}'.format(port, remote, id)\n result = __salt__['cmd.run_all'](cmd)\n return _retcode_to_bool(result['retcode'])\n else:\n cmd = 'ovs-vsctl add-port {0} {1} -- set interface {1} type=gre options:remote_ip={2} ' \\\n 'options:key={3}'.format(br, port, remote, id)\n result = __salt__['cmd.run_all'](cmd)\n return _retcode_to_bool(result['retcode'])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef port_create_vxlan(br, port, id, remote, dst_port=None):\n '''\n Virtual eXtensible Local Area Network - creates VXLAN tunnel between endpoints.\n\n Args:\n br: A string - bridge name.\n port: A string - port name.\n id: An integer - unsigned 64-bit number, tunnel's key.\n remote: A string - remote endpoint's IP address.\n dst_port: An integer - port to use when creating tunnelport in the switch.\n\n Returns:\n True on success, else False.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.port_create_vxlan br0 vx1 5001 192.168.1.10 8472\n '''\n dst_port = ' options:dst_port=' + six.text_type(dst_port) if 0 < dst_port <= 65535 else ''\n if not 0 <= id < 2**64:\n return False\n elif not __salt__['dig.check_ip'](remote):\n return False\n elif not bridge_exists(br):\n return False\n elif port in port_list(br):\n cmd = 'ovs-vsctl set interface {0} type=vxlan options:remote_ip={1} ' \\\n 'options:key={2}{3}'.format(port, remote, id, dst_port)\n result = __salt__['cmd.run_all'](cmd)\n return _retcode_to_bool(result['retcode'])\n else:\n cmd = 'ovs-vsctl add-port {0} {1} -- set interface {1} type=vxlan options:remote_ip={2} ' \\\n 'options:key={3}{4}'.format(br, port, remote, id, dst_port)\n result = __salt__['cmd.run_all'](cmd)\n return _retcode_to_bool(result['retcode'])", "response": "Create VXLAN tunnel between endpoints."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef db_get(table, record, column, if_exists=False):\n '''\n Gets a column's value for a specific record.\n\n Args:\n table: A string - name of the database table.\n record: A string - identifier of the record.\n column: A string - name of the column.\n if_exists: A boolean - if True, it is not an error if the record does\n not exist.\n\n Returns:\n The column's value.\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.db_get Port br0 vlan_mode\n '''\n cmd = ['ovs-vsctl', '--format=json', '--columns={0}'.format(column)]\n if if_exists:\n cmd += ['--if-exists']\n cmd += ['list', table, record]\n result = __salt__['cmd.run_all'](cmd)\n if result['retcode'] != 0:\n raise CommandExecutionError(result['stderr'])\n output = _stdout_parse_json(result['stdout'])\n if output['data'] and output['data'][0]:\n return output['data'][0][0]\n else:\n return None", "response": "Gets a column s value for a specific record."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef db_set(table, record, column, value, if_exists=False):\n '''\n Sets a column's value for a specific record.\n\n Args:\n table: A string - name of the database table.\n record: A string - identifier of the record.\n column: A string - name of the column.\n value: A string - the value to be set\n if_exists: A boolean - if True, it is not an error if the record does\n not exist.\n\n Returns:\n None on success and an error message on failure.\n\n CLI Example:\n .. code-block:: bash\n\n salt '*' openvswitch.db_set Interface br0 mac 02:03:04:05:06:07\n '''\n cmd = ['ovs-vsctl']\n if if_exists:\n cmd += ['--if-exists']\n cmd += ['set', table, record, '{0}={1}'.format(column, json.dumps(value))]\n result = __salt__['cmd.run_all'](cmd)\n if result['retcode'] != 0:\n return result['stderr']\n else:\n return None", "response": "Sets a column s value for a specific record."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring that a virtual network with the given name and address_prefixes and resource group are present.", "response": "def virtual_network_present(name, address_prefixes, resource_group, dns_servers=None,\n tags=None, connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a virtual network exists.\n\n :param name:\n Name of the virtual network.\n\n :param resource_group:\n The resource group assigned to the virtual network.\n\n :param address_prefixes:\n A list of CIDR blocks which can be used by subnets within the virtual network.\n\n :param dns_servers:\n A list of DNS server addresses.\n\n :param tags:\n A dictionary of strings can be passed as tag metadata to the virtual network object.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure virtual network exists:\n azurearm_network.virtual_network_present:\n - name: vnet1\n - resource_group: group1\n - address_prefixes:\n - '10.0.0.0/8'\n - '192.168.0.0/16'\n - dns_servers:\n - '8.8.8.8'\n - tags:\n contact_name: Elmer Fudd Gantry\n - connection_auth: {{ profile }}\n - require:\n - azurearm_resource: Ensure resource group exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n vnet = __salt__['azurearm_network.virtual_network_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in vnet:\n tag_changes = __utils__['dictdiffer.deep_diff'](vnet.get('tags', {}), tags or {})\n if tag_changes:\n ret['changes']['tags'] = tag_changes\n\n dns_changes = set(dns_servers or []).symmetric_difference(\n set(vnet.get('dhcp_options', {}).get('dns_servers', [])))\n if dns_changes:\n ret['changes']['dns_servers'] = {\n 'old': vnet.get('dhcp_options', {}).get('dns_servers', []),\n 'new': dns_servers,\n }\n\n addr_changes = set(address_prefixes or []).symmetric_difference(\n set(vnet.get('address_space', {}).get('address_prefixes', [])))\n if addr_changes:\n ret['changes']['address_space'] = {\n 'address_prefixes': {\n 'old': vnet.get('address_space', {}).get('address_prefixes', []),\n 'new': address_prefixes,\n }\n }\n\n if kwargs.get('enable_ddos_protection', False) != vnet.get('enable_ddos_protection'):\n ret['changes']['enable_ddos_protection'] = {\n 'old': vnet.get('enable_ddos_protection'),\n 'new': kwargs.get('enable_ddos_protection')\n }\n\n if kwargs.get('enable_vm_protection', False) != vnet.get('enable_vm_protection'):\n ret['changes']['enable_vm_protection'] = {\n 'old': vnet.get('enable_vm_protection'),\n 'new': kwargs.get('enable_vm_protection')\n }\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Virtual network {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Virtual network {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'resource_group': resource_group,\n 'address_space': {'address_prefixes': address_prefixes},\n 'dhcp_options': {'dns_servers': dns_servers},\n 'enable_ddos_protection': kwargs.get('enable_ddos_protection', False),\n 'enable_vm_protection': kwargs.get('enable_vm_protection', False),\n 'tags': tags,\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Virtual network {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n vnet_kwargs = kwargs.copy()\n vnet_kwargs.update(connection_auth)\n\n vnet = __salt__['azurearm_network.virtual_network_create_or_update'](\n name=name,\n resource_group=resource_group,\n address_prefixes=address_prefixes,\n dns_servers=dns_servers,\n tags=tags,\n **vnet_kwargs\n )\n\n if 'error' not in vnet:\n ret['result'] = True\n ret['comment'] = 'Virtual network {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create virtual network {0}! ({1})'.format(name, vnet.get('error'))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef virtual_network_absent(name, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a virtual network does not exist in the resource group.\n\n :param name:\n Name of the virtual network.\n\n :param resource_group:\n The resource group assigned to the virtual network.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n vnet = __salt__['azurearm_network.virtual_network_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in vnet:\n ret['result'] = True\n ret['comment'] = 'Virtual network {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Virtual network {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': vnet,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.virtual_network_delete'](name, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Virtual network {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': vnet,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete virtual network {0}!'.format(name)\n return ret", "response": "Ensure a virtual network is absent in the resource group."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring a subnet exists in the virtual network.", "response": "def subnet_present(name, address_prefix, virtual_network, resource_group,\n security_group=None, route_table=None, connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a subnet exists.\n\n :param name:\n Name of the subnet.\n\n :param address_prefix:\n A CIDR block used by the subnet within the virtual network.\n\n :param virtual_network:\n Name of the existing virtual network to contain the subnet.\n\n :param resource_group:\n The resource group assigned to the virtual network.\n\n :param security_group:\n The name of the existing network security group to assign to the subnet.\n\n :param route_table:\n The name of the existing route table to assign to the subnet.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure subnet exists:\n azurearm_network.subnet_present:\n - name: vnet1_sn1\n - virtual_network: vnet1\n - resource_group: group1\n - address_prefix: '192.168.1.0/24'\n - security_group: nsg1\n - route_table: rt1\n - connection_auth: {{ profile }}\n - require:\n - azurearm_network: Ensure virtual network exists\n - azurearm_network: Ensure network security group exists\n - azurearm_network: Ensure route table exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n snet = __salt__['azurearm_network.subnet_get'](\n name,\n virtual_network,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in snet:\n if address_prefix != snet.get('address_prefix'):\n ret['changes']['address_prefix'] = {\n 'old': snet.get('address_prefix'),\n 'new': address_prefix\n }\n\n nsg_name = None\n if snet.get('network_security_group'):\n nsg_name = snet['network_security_group']['id'].split('/')[-1]\n\n if security_group and (security_group != nsg_name):\n ret['changes']['network_security_group'] = {\n 'old': nsg_name,\n 'new': security_group\n }\n\n rttbl_name = None\n if snet.get('route_table'):\n rttbl_name = snet['route_table']['id'].split('/')[-1]\n\n if route_table and (route_table != rttbl_name):\n ret['changes']['route_table'] = {\n 'old': rttbl_name,\n 'new': route_table\n }\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Subnet {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Subnet {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'address_prefix': address_prefix,\n 'network_security_group': security_group,\n 'route_table': route_table\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Subnet {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n snet_kwargs = kwargs.copy()\n snet_kwargs.update(connection_auth)\n\n snet = __salt__['azurearm_network.subnet_create_or_update'](\n name=name,\n virtual_network=virtual_network,\n resource_group=resource_group,\n address_prefix=address_prefix,\n network_security_group=security_group,\n route_table=route_table,\n **snet_kwargs\n )\n\n if 'error' not in snet:\n ret['result'] = True\n ret['comment'] = 'Subnet {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create subnet {0}! ({1})'.format(name, snet.get('error'))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef subnet_absent(name, virtual_network, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a virtual network does not exist in the virtual network.\n\n :param name:\n Name of the subnet.\n\n :param virtual_network:\n Name of the existing virtual network containing the subnet.\n\n :param resource_group:\n The resource group assigned to the virtual network.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n snet = __salt__['azurearm_network.subnet_get'](\n name,\n virtual_network,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in snet:\n ret['result'] = True\n ret['comment'] = 'Subnet {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Subnet {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': snet,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.subnet_delete'](name, virtual_network, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Subnet {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': snet,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete subnet {0}!'.format(name)\n return ret", "response": "Ensure a subnet is absent in the virtual network."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef network_security_group_present(name, resource_group, tags=None, security_rules=None, connection_auth=None,\n **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a network security group exists.\n\n :param name:\n Name of the network security group.\n\n :param resource_group:\n The resource group assigned to the network security group.\n\n :param tags:\n A dictionary of strings can be passed as tag metadata to the network security group object.\n\n :param security_rules: An optional list of dictionaries representing valid SecurityRule objects. See the\n documentation for the security_rule_present state or security_rule_create_or_update execution module\n for more information on required and optional parameters for security rules. The rules are only\n managed if this parameter is present. When this parameter is absent, implemented rules will not be removed,\n and will merely become unmanaged.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure network security group exists:\n azurearm_network.network_security_group_present:\n - name: nsg1\n - resource_group: group1\n - security_rules:\n - name: nsg1_rule1\n priority: 100\n protocol: tcp\n access: allow\n direction: outbound\n source_address_prefix: virtualnetwork\n destination_address_prefix: internet\n source_port_range: '*'\n destination_port_range: '*'\n - name: nsg1_rule2\n priority: 101\n protocol: tcp\n access: allow\n direction: inbound\n source_address_prefix: internet\n destination_address_prefix: virtualnetwork\n source_port_range: '*'\n destination_port_ranges:\n - '80'\n - '443'\n - tags:\n contact_name: Elmer Fudd Gantry\n - connection_auth: {{ profile }}\n - require:\n - azurearm_resource: Ensure resource group exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n nsg = __salt__['azurearm_network.network_security_group_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in nsg:\n tag_changes = __utils__['dictdiffer.deep_diff'](nsg.get('tags', {}), tags or {})\n if tag_changes:\n ret['changes']['tags'] = tag_changes\n\n if security_rules:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](nsg.get('security_rules', []), security_rules)\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"security_rules\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['security_rules'] = comp_ret['changes']\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Network security group {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Network security group {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'resource_group': resource_group,\n 'tags': tags,\n 'security_rules': security_rules,\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Network security group {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n nsg_kwargs = kwargs.copy()\n nsg_kwargs.update(connection_auth)\n\n nsg = __salt__['azurearm_network.network_security_group_create_or_update'](\n name=name,\n resource_group=resource_group,\n tags=tags,\n security_rules=security_rules,\n **nsg_kwargs\n )\n\n if 'error' not in nsg:\n ret['result'] = True\n ret['comment'] = 'Network security group {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create network security group {0}! ({1})'.format(name, nsg.get('error'))\n return ret", "response": "Ensures a network security group exists."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure a network security group is absent in the resource group.", "response": "def network_security_group_absent(name, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a network security group does not exist in the resource group.\n\n :param name:\n Name of the network security group.\n\n :param resource_group:\n The resource group assigned to the network security group.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n nsg = __salt__['azurearm_network.network_security_group_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in nsg:\n ret['result'] = True\n ret['comment'] = 'Network security group {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Network security group {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': nsg,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.network_security_group_delete'](name, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Network security group {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': nsg,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete network security group {0}!'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef security_rule_present(name, access, direction, priority, protocol, security_group, resource_group,\n destination_address_prefix=None, destination_port_range=None, source_address_prefix=None,\n source_port_range=None, description=None, destination_address_prefixes=None,\n destination_port_ranges=None, source_address_prefixes=None, source_port_ranges=None,\n connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a security rule exists.\n\n :param name:\n Name of the security rule.\n\n :param access:\n 'allow' or 'deny'\n\n :param direction:\n 'inbound' or 'outbound'\n\n :param priority:\n Integer between 100 and 4096 used for ordering rule application.\n\n :param protocol:\n 'tcp', 'udp', or '*'\n\n :param security_group:\n The name of the existing network security group to contain the security rule.\n\n :param resource_group:\n The resource group assigned to the network security group.\n\n :param description:\n Optional description of the security rule.\n\n :param destination_address_prefix:\n The CIDR or destination IP range. Asterix '*' can also be used to match all destination IPs.\n Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used.\n If this is an ingress rule, specifies where network traffic originates from.\n\n :param destination_port_range:\n The destination port or range. Integer or range between 0 and 65535. Asterix '*'\n can also be used to match all ports.\n\n :param source_address_prefix:\n The CIDR or source IP range. Asterix '*' can also be used to match all source IPs.\n Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used.\n If this is an ingress rule, specifies where network traffic originates from.\n\n :param source_port_range:\n The source port or range. Integer or range between 0 and 65535. Asterix '*'\n can also be used to match all ports.\n\n :param destination_address_prefixes:\n A list of destination_address_prefix values. This parameter overrides destination_address_prefix\n and will cause any value entered there to be ignored.\n\n :param destination_port_ranges:\n A list of destination_port_range values. This parameter overrides destination_port_range\n and will cause any value entered there to be ignored.\n\n :param source_address_prefixes:\n A list of source_address_prefix values. This parameter overrides source_address_prefix\n and will cause any value entered there to be ignored.\n\n :param source_port_ranges:\n A list of source_port_range values. This parameter overrides source_port_range\n and will cause any value entered there to be ignored.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure security rule exists:\n azurearm_network.security_rule_present:\n - name: nsg1_rule2\n - security_group: nsg1\n - resource_group: group1\n - priority: 101\n - protocol: tcp\n - access: allow\n - direction: inbound\n - source_address_prefix: internet\n - destination_address_prefix: virtualnetwork\n - source_port_range: '*'\n - destination_port_ranges:\n - '80'\n - '443'\n - connection_auth: {{ profile }}\n - require:\n - azurearm_network: Ensure network security group exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n exclusive_params = [\n ('source_port_ranges', 'source_port_range'),\n ('source_address_prefixes', 'source_address_prefix'),\n ('destination_port_ranges', 'destination_port_range'),\n ('destination_address_prefixes', 'destination_address_prefix'),\n ]\n\n for params in exclusive_params:\n # pylint: disable=eval-used\n if not eval(params[0]) and not eval(params[1]):\n ret['comment'] = 'Either the {0} or {1} parameter must be provided!'.format(params[0], params[1])\n return ret\n # pylint: disable=eval-used\n if eval(params[0]):\n # pylint: disable=eval-used\n if not isinstance(eval(params[0]), list):\n ret['comment'] = 'The {0} parameter must be a list!'.format(params[0])\n return ret\n # pylint: disable=exec-used\n exec('{0} = None'.format(params[1]))\n\n rule = __salt__['azurearm_network.security_rule_get'](\n name,\n security_group,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in rule:\n # access changes\n if access.capitalize() != rule.get('access'):\n ret['changes']['access'] = {\n 'old': rule.get('access'),\n 'new': access\n }\n\n # description changes\n if description != rule.get('description'):\n ret['changes']['description'] = {\n 'old': rule.get('description'),\n 'new': description\n }\n\n # direction changes\n if direction.capitalize() != rule.get('direction'):\n ret['changes']['direction'] = {\n 'old': rule.get('direction'),\n 'new': direction\n }\n\n # priority changes\n if int(priority) != rule.get('priority'):\n ret['changes']['priority'] = {\n 'old': rule.get('priority'),\n 'new': priority\n }\n\n # protocol changes\n if protocol.lower() != rule.get('protocol', '').lower():\n ret['changes']['protocol'] = {\n 'old': rule.get('protocol'),\n 'new': protocol\n }\n\n # destination_port_range changes\n if destination_port_range != rule.get('destination_port_range'):\n ret['changes']['destination_port_range'] = {\n 'old': rule.get('destination_port_range'),\n 'new': destination_port_range\n }\n\n # source_port_range changes\n if source_port_range != rule.get('source_port_range'):\n ret['changes']['source_port_range'] = {\n 'old': rule.get('source_port_range'),\n 'new': source_port_range\n }\n\n # destination_port_ranges changes\n if sorted(destination_port_ranges or []) != sorted(rule.get('destination_port_ranges', [])):\n ret['changes']['destination_port_ranges'] = {\n 'old': rule.get('destination_port_ranges'),\n 'new': destination_port_ranges\n }\n\n # source_port_ranges changes\n if sorted(source_port_ranges or []) != sorted(rule.get('source_port_ranges', [])):\n ret['changes']['source_port_ranges'] = {\n 'old': rule.get('source_port_ranges'),\n 'new': source_port_ranges\n }\n\n # destination_address_prefix changes\n if (destination_address_prefix or '').lower() != rule.get('destination_address_prefix', '').lower():\n ret['changes']['destination_address_prefix'] = {\n 'old': rule.get('destination_address_prefix'),\n 'new': destination_address_prefix\n }\n\n # source_address_prefix changes\n if (source_address_prefix or '').lower() != rule.get('source_address_prefix', '').lower():\n ret['changes']['source_address_prefix'] = {\n 'old': rule.get('source_address_prefix'),\n 'new': source_address_prefix\n }\n\n # destination_address_prefixes changes\n if sorted(destination_address_prefixes or []) != sorted(rule.get('destination_address_prefixes', [])):\n if len(destination_address_prefixes or []) != len(rule.get('destination_address_prefixes', [])):\n ret['changes']['destination_address_prefixes'] = {\n 'old': rule.get('destination_address_prefixes'),\n 'new': destination_address_prefixes\n }\n else:\n local_dst_addrs, remote_dst_addrs = (sorted(destination_address_prefixes),\n sorted(rule.get('destination_address_prefixes')))\n for idx in six_range(0, len(local_dst_addrs)):\n if local_dst_addrs[idx].lower() != remote_dst_addrs[idx].lower():\n ret['changes']['destination_address_prefixes'] = {\n 'old': rule.get('destination_address_prefixes'),\n 'new': destination_address_prefixes\n }\n break\n\n # source_address_prefixes changes\n if sorted(source_address_prefixes or []) != sorted(rule.get('source_address_prefixes', [])):\n if len(source_address_prefixes or []) != len(rule.get('source_address_prefixes', [])):\n ret['changes']['source_address_prefixes'] = {\n 'old': rule.get('source_address_prefixes'),\n 'new': source_address_prefixes\n }\n else:\n local_src_addrs, remote_src_addrs = (sorted(source_address_prefixes),\n sorted(rule.get('source_address_prefixes')))\n for idx in six_range(0, len(local_src_addrs)):\n if local_src_addrs[idx].lower() != remote_src_addrs[idx].lower():\n ret['changes']['source_address_prefixes'] = {\n 'old': rule.get('source_address_prefixes'),\n 'new': source_address_prefixes\n }\n break\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Security rule {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Security rule {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'access': access,\n 'description': description,\n 'direction': direction,\n 'priority': priority,\n 'protocol': protocol,\n 'destination_address_prefix': destination_address_prefix,\n 'destination_address_prefixes': destination_address_prefixes,\n 'destination_port_range': destination_port_range,\n 'destination_port_ranges': destination_port_ranges,\n 'source_address_prefix': source_address_prefix,\n 'source_address_prefixes': source_address_prefixes,\n 'source_port_range': source_port_range,\n 'source_port_ranges': source_port_ranges,\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Security rule {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n rule_kwargs = kwargs.copy()\n rule_kwargs.update(connection_auth)\n\n rule = __salt__['azurearm_network.security_rule_create_or_update'](\n name=name,\n access=access,\n description=description,\n direction=direction,\n priority=priority,\n protocol=protocol,\n security_group=security_group,\n resource_group=resource_group,\n destination_address_prefix=destination_address_prefix,\n destination_address_prefixes=destination_address_prefixes,\n destination_port_range=destination_port_range,\n destination_port_ranges=destination_port_ranges,\n source_address_prefix=source_address_prefix,\n source_address_prefixes=source_address_prefixes,\n source_port_range=source_port_range,\n source_port_ranges=source_port_ranges,\n **rule_kwargs\n )\n\n if 'error' not in rule:\n ret['result'] = True\n ret['comment'] = 'Security rule {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create security rule {0}! ({1})'.format(name, rule.get('error'))\n return ret", "response": "Ensures a security rule exists in the network."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nensuring a security rule is absent in the network security group.", "response": "def security_rule_absent(name, security_group, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a security rule does not exist in the network security group.\n\n :param name:\n Name of the security rule.\n\n :param security_group:\n The network security group containing the security rule.\n\n :param resource_group:\n The resource group assigned to the network security group.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n rule = __salt__['azurearm_network.security_rule_get'](\n name,\n security_group,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in rule:\n ret['result'] = True\n ret['comment'] = 'Security rule {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Security rule {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': rule,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.security_rule_delete'](name, security_group, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Security rule {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': rule,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete security rule {0}!'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nensures that a load balancer exists and is up - to - date.", "response": "def load_balancer_present(name, resource_group, sku=None, frontend_ip_configurations=None, backend_address_pools=None,\n load_balancing_rules=None, probes=None, inbound_nat_rules=None, inbound_nat_pools=None,\n outbound_nat_rules=None, tags=None, connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a load balancer exists.\n\n :param name:\n Name of the load balancer.\n\n :param resource_group:\n The resource group assigned to the load balancer.\n\n :param sku:\n The load balancer SKU, which can be 'Basic' or 'Standard'.\n\n :param tags:\n A dictionary of strings can be passed as tag metadata to the load balancer object.\n\n :param frontend_ip_configurations:\n An optional list of dictionaries representing valid FrontendIPConfiguration objects. A frontend IP\n configuration can be either private (using private IP address and subnet parameters) or public (using a\n reference to a public IP address object). Valid parameters are:\n\n - ``name``: The name of the resource that is unique within a resource group.\n - ``private_ip_address``: The private IP address of the IP configuration. Required if\n 'private_ip_allocation_method' is 'Static'.\n - ``private_ip_allocation_method``: The Private IP allocation method. Possible values are: 'Static' and\n 'Dynamic'.\n - ``subnet``: Name of an existing subnet inside of which the frontend IP will reside.\n - ``public_ip_address``: Name of an existing public IP address which will be assigned to the frontend IP object.\n\n :param backend_address_pools:\n An optional list of dictionaries representing valid BackendAddressPool objects. Only the 'name' parameter is\n valid for a BackendAddressPool dictionary. All other parameters are read-only references from other objects\n linking to the backend address pool. Inbound traffic is randomly load balanced across IPs in the backend IPs.\n\n :param probes:\n An optional list of dictionaries representing valid Probe objects. Valid parameters are:\n\n - ``name``: The name of the resource that is unique within a resource group.\n - ``protocol``: The protocol of the endpoint. Possible values are 'Http' or 'Tcp'. If 'Tcp' is specified, a\n received ACK is required for the probe to be successful. If 'Http' is specified, a 200 OK response from the\n specified URI is required for the probe to be successful.\n - ``port``: The port for communicating the probe. Possible values range from 1 to 65535, inclusive.\n - ``interval_in_seconds``: The interval, in seconds, for how frequently to probe the endpoint for health status.\n Typically, the interval is slightly less than half the allocated timeout period (in seconds) which allows two\n full probes before taking the instance out of rotation. The default value is 15, the minimum value is 5.\n - ``number_of_probes``: The number of probes where if no response, will result in stopping further traffic from\n being delivered to the endpoint. This values allows endpoints to be taken out of rotation faster or slower\n than the typical times used in Azure.\n - ``request_path``: The URI used for requesting health status from the VM. Path is required if a protocol is\n set to 'Http'. Otherwise, it is not allowed. There is no default value.\n\n :param load_balancing_rules:\n An optional list of dictionaries representing valid LoadBalancingRule objects. Valid parameters are:\n\n - ``name``: The name of the resource that is unique within a resource group.\n - ``load_distribution``: The load distribution policy for this rule. Possible values are 'Default', 'SourceIP',\n and 'SourceIPProtocol'.\n - ``frontend_port``: The port for the external endpoint. Port numbers for each rule must be unique within the\n Load Balancer. Acceptable values are between 0 and 65534. Note that value 0 enables 'Any Port'.\n - ``backend_port``: The port used for internal connections on the endpoint. Acceptable values are between 0 and\n 65535. Note that value 0 enables 'Any Port'.\n - ``idle_timeout_in_minutes``: The timeout for the TCP idle connection. The value can be set between 4 and 30\n minutes. The default value is 4 minutes. This element is only used when the protocol is set to TCP.\n - ``enable_floating_ip``: Configures a virtual machine's endpoint for the floating IP capability required\n to configure a SQL AlwaysOn Availability Group. This setting is required when using the SQL AlwaysOn\n Availability Groups in SQL server. This setting can't be changed after you create the endpoint.\n - ``disable_outbound_snat``: Configures SNAT for the VMs in the backend pool to use the public IP address\n specified in the frontend of the load balancing rule.\n - ``frontend_ip_configuration``: Name of the frontend IP configuration object used by the load balancing rule\n object.\n - ``backend_address_pool``: Name of the backend address pool object used by the load balancing rule object.\n Inbound traffic is randomly load balanced across IPs in the backend IPs.\n - ``probe``: Name of the probe object used by the load balancing rule object.\n\n :param inbound_nat_rules:\n An optional list of dictionaries representing valid InboundNatRule objects. Defining inbound NAT rules on your\n load balancer is mutually exclusive with defining an inbound NAT pool. Inbound NAT pools are referenced from\n virtual machine scale sets. NICs that are associated with individual virtual machines cannot reference an\n Inbound NAT pool. They have to reference individual inbound NAT rules. Valid parameters are:\n\n - ``name``: The name of the resource that is unique within a resource group.\n - ``frontend_ip_configuration``: Name of the frontend IP configuration object used by the inbound NAT rule\n object.\n - ``protocol``: Possible values include 'Udp', 'Tcp', or 'All'.\n - ``frontend_port``: The port for the external endpoint. Port numbers for each rule must be unique within the\n Load Balancer. Acceptable values range from 1 to 65534.\n - ``backend_port``: The port used for the internal endpoint. Acceptable values range from 1 to 65535.\n - ``idle_timeout_in_minutes``: The timeout for the TCP idle connection. The value can be set between 4 and 30\n minutes. The default value is 4 minutes. This element is only used when the protocol is set to TCP.\n - ``enable_floating_ip``: Configures a virtual machine's endpoint for the floating IP capability required\n to configure a SQL AlwaysOn Availability Group. This setting is required when using the SQL AlwaysOn\n Availability Groups in SQL server. This setting can't be changed after you create the endpoint.\n\n :param inbound_nat_pools:\n An optional list of dictionaries representing valid InboundNatPool objects. They define an external port range\n for inbound NAT to a single backend port on NICs associated with a load balancer. Inbound NAT rules are created\n automatically for each NIC associated with the Load Balancer using an external port from this range. Defining an\n Inbound NAT pool on your Load Balancer is mutually exclusive with defining inbound NAT rules. Inbound NAT pools\n are referenced from virtual machine scale sets. NICs that are associated with individual virtual machines cannot\n reference an inbound NAT pool. They have to reference individual inbound NAT rules. Valid parameters are:\n\n - ``name``: The name of the resource that is unique within a resource group.\n - ``frontend_ip_configuration``: Name of the frontend IP configuration object used by the inbound NAT pool\n object.\n - ``protocol``: Possible values include 'Udp', 'Tcp', or 'All'.\n - ``frontend_port_range_start``: The first port number in the range of external ports that will be used to\n provide Inbound NAT to NICs associated with a load balancer. Acceptable values range between 1 and 65534.\n - ``frontend_port_range_end``: The last port number in the range of external ports that will be used to\n provide Inbound NAT to NICs associated with a load balancer. Acceptable values range between 1 and 65535.\n - ``backend_port``: The port used for internal connections to the endpoint. Acceptable values are between 1 and\n 65535.\n\n :param outbound_nat_rules:\n An optional list of dictionaries representing valid OutboundNatRule objects. Valid parameters are:\n\n - ``name``: The name of the resource that is unique within a resource group.\n - ``frontend_ip_configuration``: Name of the frontend IP configuration object used by the outbound NAT rule\n object.\n - ``backend_address_pool``: Name of the backend address pool object used by the outbound NAT rule object.\n Outbound traffic is randomly load balanced across IPs in the backend IPs.\n - ``allocated_outbound_ports``: The number of outbound ports to be used for NAT.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure load balancer exists:\n azurearm_network.load_balancer_present:\n - name: lb1\n - resource_group: group1\n - location: eastus\n - frontend_ip_configurations:\n - name: lb1_feip1\n public_ip_address: pub_ip1\n - backend_address_pools:\n - name: lb1_bepool1\n - probes:\n - name: lb1_webprobe1\n protocol: tcp\n port: 80\n interval_in_seconds: 5\n number_of_probes: 2\n - load_balancing_rules:\n - name: lb1_webprobe1\n protocol: tcp\n frontend_port: 80\n backend_port: 80\n idle_timeout_in_minutes: 4\n frontend_ip_configuration: lb1_feip1\n backend_address_pool: lb1_bepool1\n probe: lb1_webprobe1\n - tags:\n contact_name: Elmer Fudd Gantry\n - connection_auth: {{ profile }}\n - require:\n - azurearm_resource: Ensure resource group exists\n - azurearm_network: Ensure public IP exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n if sku:\n sku = {'name': sku.capitalize()}\n\n load_bal = __salt__['azurearm_network.load_balancer_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in load_bal:\n # tag changes\n tag_changes = __utils__['dictdiffer.deep_diff'](load_bal.get('tags', {}), tags or {})\n if tag_changes:\n ret['changes']['tags'] = tag_changes\n\n # sku changes\n if sku:\n sku_changes = __utils__['dictdiffer.deep_diff'](load_bal.get('sku', {}), sku)\n if sku_changes:\n ret['changes']['sku'] = sku_changes\n\n # frontend_ip_configurations changes\n if frontend_ip_configurations:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](\n load_bal.get('frontend_ip_configurations', []),\n frontend_ip_configurations,\n ['public_ip_address', 'subnet']\n )\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"frontend_ip_configurations\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['frontend_ip_configurations'] = comp_ret['changes']\n\n # backend_address_pools changes\n if backend_address_pools:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](\n load_bal.get('backend_address_pools', []),\n backend_address_pools\n )\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"backend_address_pools\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['backend_address_pools'] = comp_ret['changes']\n\n # probes changes\n if probes:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](load_bal.get('probes', []), probes)\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"probes\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['probes'] = comp_ret['changes']\n\n # load_balancing_rules changes\n if load_balancing_rules:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](\n load_bal.get('load_balancing_rules', []),\n load_balancing_rules,\n ['frontend_ip_configuration', 'backend_address_pool', 'probe']\n )\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"load_balancing_rules\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['load_balancing_rules'] = comp_ret['changes']\n\n # inbound_nat_rules changes\n if inbound_nat_rules:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](\n load_bal.get('inbound_nat_rules', []),\n inbound_nat_rules,\n ['frontend_ip_configuration']\n )\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"inbound_nat_rules\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['inbound_nat_rules'] = comp_ret['changes']\n\n # inbound_nat_pools changes\n if inbound_nat_pools:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](\n load_bal.get('inbound_nat_pools', []),\n inbound_nat_pools,\n ['frontend_ip_configuration']\n )\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"inbound_nat_pools\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['inbound_nat_pools'] = comp_ret['changes']\n\n # outbound_nat_rules changes\n if outbound_nat_rules:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](\n load_bal.get('outbound_nat_rules', []),\n outbound_nat_rules,\n ['frontend_ip_configuration']\n )\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"outbound_nat_rules\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['outbound_nat_rules'] = comp_ret['changes']\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Load balancer {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Load balancer {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'sku': sku,\n 'tags': tags,\n 'frontend_ip_configurations': frontend_ip_configurations,\n 'backend_address_pools': backend_address_pools,\n 'load_balancing_rules': load_balancing_rules,\n 'probes': probes,\n 'inbound_nat_rules': inbound_nat_rules,\n 'inbound_nat_pools': inbound_nat_pools,\n 'outbound_nat_rules': outbound_nat_rules,\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Load balancer {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n lb_kwargs = kwargs.copy()\n lb_kwargs.update(connection_auth)\n\n load_bal = __salt__['azurearm_network.load_balancer_create_or_update'](\n name=name,\n resource_group=resource_group,\n sku=sku,\n tags=tags,\n frontend_ip_configurations=frontend_ip_configurations,\n backend_address_pools=backend_address_pools,\n load_balancing_rules=load_balancing_rules,\n probes=probes,\n inbound_nat_rules=inbound_nat_rules,\n inbound_nat_pools=inbound_nat_pools,\n outbound_nat_rules=outbound_nat_rules,\n **lb_kwargs\n )\n\n if 'error' not in load_bal:\n ret['result'] = True\n ret['comment'] = 'Load balancer {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create load balancer {0}! ({1})'.format(name, load_bal.get('error'))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures a load balancer with the given name is absent in the given resource group.", "response": "def load_balancer_absent(name, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a load balancer does not exist in the resource group.\n\n :param name:\n Name of the load balancer.\n\n :param resource_group:\n The resource group assigned to the load balancer.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n load_bal = __salt__['azurearm_network.load_balancer_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in load_bal:\n ret['result'] = True\n ret['comment'] = 'Load balancer {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Load balancer {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': load_bal,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.load_balancer_delete'](name, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Load balancer {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': load_bal,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete load balancer {0}!'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring that a public IP address exists in the resource group with the given name resource group and tags are passed as tag metadata.", "response": "def public_ip_address_present(name, resource_group, tags=None, sku=None, public_ip_allocation_method=None,\n public_ip_address_version=None, dns_settings=None, idle_timeout_in_minutes=None,\n connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a public IP address exists.\n\n :param name:\n Name of the public IP address.\n\n :param resource_group:\n The resource group assigned to the public IP address.\n\n :param dns_settings:\n An optional dictionary representing a valid PublicIPAddressDnsSettings object. Parameters include\n 'domain_name_label' and 'reverse_fqdn', which accept strings. The 'domain_name_label' parameter is concatenated\n with the regionalized DNS zone make up the fully qualified domain name associated with the public IP address.\n If a domain name label is specified, an A DNS record is created for the public IP in the Microsoft Azure DNS\n system. The 'reverse_fqdn' parameter is a user-visible, fully qualified domain name that resolves to this public\n IP address. If the reverse FQDN is specified, then a PTR DNS record is created pointing from the IP address in\n the in-addr.arpa domain to the reverse FQDN.\n\n :param sku:\n The public IP address SKU, which can be 'Basic' or 'Standard'.\n\n :param public_ip_allocation_method:\n The public IP allocation method. Possible values are: 'Static' and 'Dynamic'.\n\n :param public_ip_address_version:\n The public IP address version. Possible values are: 'IPv4' and 'IPv6'.\n\n :param idle_timeout_in_minutes:\n An integer representing the idle timeout of the public IP address.\n\n :param tags:\n A dictionary of strings can be passed as tag metadata to the public IP address object.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure public IP exists:\n azurearm_network.public_ip_address_present:\n - name: pub_ip1\n - resource_group: group1\n - dns_settings:\n domain_name_label: decisionlab-ext-test-label\n - sku: basic\n - public_ip_allocation_method: static\n - public_ip_address_version: ipv4\n - idle_timeout_in_minutes: 4\n - tags:\n contact_name: Elmer Fudd Gantry\n - connection_auth: {{ profile }}\n - require:\n - azurearm_resource: Ensure resource group exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n if sku:\n sku = {'name': sku.capitalize()}\n\n pub_ip = __salt__['azurearm_network.public_ip_address_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in pub_ip:\n # tag changes\n tag_changes = __utils__['dictdiffer.deep_diff'](pub_ip.get('tags', {}), tags or {})\n if tag_changes:\n ret['changes']['tags'] = tag_changes\n\n # dns_settings changes\n if dns_settings:\n if not isinstance(dns_settings, dict):\n ret['comment'] = 'DNS settings must be provided as a dictionary!'\n return ret\n\n for key in dns_settings:\n if dns_settings[key] != pub_ip.get('dns_settings', {}).get(key):\n ret['changes']['dns_settings'] = {\n 'old': pub_ip.get('dns_settings'),\n 'new': dns_settings\n }\n break\n\n # sku changes\n if sku:\n sku_changes = __utils__['dictdiffer.deep_diff'](pub_ip.get('sku', {}), sku)\n if sku_changes:\n ret['changes']['sku'] = sku_changes\n\n # public_ip_allocation_method changes\n if public_ip_allocation_method:\n if public_ip_allocation_method.capitalize() != pub_ip.get('public_ip_allocation_method'):\n ret['changes']['public_ip_allocation_method'] = {\n 'old': pub_ip.get('public_ip_allocation_method'),\n 'new': public_ip_allocation_method\n }\n\n # public_ip_address_version changes\n if public_ip_address_version:\n if public_ip_address_version.lower() != pub_ip.get('public_ip_address_version', '').lower():\n ret['changes']['public_ip_address_version'] = {\n 'old': pub_ip.get('public_ip_address_version'),\n 'new': public_ip_address_version\n }\n\n # idle_timeout_in_minutes changes\n if idle_timeout_in_minutes and (int(idle_timeout_in_minutes) != pub_ip.get('idle_timeout_in_minutes')):\n ret['changes']['idle_timeout_in_minutes'] = {\n 'old': pub_ip.get('idle_timeout_in_minutes'),\n 'new': idle_timeout_in_minutes\n }\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Public IP address {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Public IP address {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'tags': tags,\n 'dns_settings': dns_settings,\n 'sku': sku,\n 'public_ip_allocation_method': public_ip_allocation_method,\n 'public_ip_address_version': public_ip_address_version,\n 'idle_timeout_in_minutes': idle_timeout_in_minutes,\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Public IP address {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n pub_ip_kwargs = kwargs.copy()\n pub_ip_kwargs.update(connection_auth)\n\n pub_ip = __salt__['azurearm_network.public_ip_address_create_or_update'](\n name=name,\n resource_group=resource_group,\n sku=sku,\n tags=tags,\n dns_settings=dns_settings,\n public_ip_allocation_method=public_ip_allocation_method,\n public_ip_address_version=public_ip_address_version,\n idle_timeout_in_minutes=idle_timeout_in_minutes,\n **pub_ip_kwargs\n )\n\n if 'error' not in pub_ip:\n ret['result'] = True\n ret['comment'] = 'Public IP address {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create public IP address {0}! ({1})'.format(name, pub_ip.get('error'))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nensure that the public IP address is absent in the resource group.", "response": "def public_ip_address_absent(name, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a public IP address does not exist in the resource group.\n\n :param name:\n Name of the public IP address.\n\n :param resource_group:\n The resource group assigned to the public IP address.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n pub_ip = __salt__['azurearm_network.public_ip_address_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in pub_ip:\n ret['result'] = True\n ret['comment'] = 'Public IP address {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Public IP address {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': pub_ip,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.public_ip_address_delete'](name, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Public IP address {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': pub_ip,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete public IP address {0}!'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure that a network interface with the given parameters is present.", "response": "def network_interface_present(name, ip_configurations, subnet, virtual_network, resource_group, tags=None,\n virtual_machine=None, network_security_group=None, dns_settings=None, mac_address=None,\n primary=None, enable_accelerated_networking=None, enable_ip_forwarding=None,\n connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a network interface exists.\n\n :param name:\n Name of the network interface.\n\n :param ip_configurations:\n A list of dictionaries representing valid NetworkInterfaceIPConfiguration objects. The 'name' key is required at\n minimum. At least one IP Configuration must be present.\n\n :param subnet:\n Name of the existing subnet assigned to the network interface.\n\n :param virtual_network:\n Name of the existing virtual network containing the subnet.\n\n :param resource_group:\n The resource group assigned to the virtual network.\n\n :param tags:\n A dictionary of strings can be passed as tag metadata to the network interface object.\n\n :param network_security_group:\n The name of the existing network security group to assign to the network interface.\n\n :param virtual_machine:\n The name of the existing virtual machine to assign to the network interface.\n\n :param dns_settings:\n An optional dictionary representing a valid NetworkInterfaceDnsSettings object. Valid parameters are:\n\n - ``dns_servers``: List of DNS server IP addresses. Use 'AzureProvidedDNS' to switch to Azure provided DNS\n resolution. 'AzureProvidedDNS' value cannot be combined with other IPs, it must be the only value in\n dns_servers collection.\n - ``internal_dns_name_label``: Relative DNS name for this NIC used for internal communications between VMs in\n the same virtual network.\n - ``internal_fqdn``: Fully qualified DNS name supporting internal communications between VMs in the same virtual\n network.\n - ``internal_domain_name_suffix``: Even if internal_dns_name_label is not specified, a DNS entry is created for\n the primary NIC of the VM. This DNS name can be constructed by concatenating the VM name with the value of\n internal_domain_name_suffix.\n\n :param mac_address:\n Optional string containing the MAC address of the network interface.\n\n :param primary:\n Optional boolean allowing the interface to be set as the primary network interface on a virtual machine\n with multiple interfaces attached.\n\n :param enable_accelerated_networking:\n Optional boolean indicating whether accelerated networking should be enabled for the interface.\n\n :param enable_ip_forwarding:\n Optional boolean indicating whether IP forwarding should be enabled for the interface.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure network interface exists:\n azurearm_network.network_interface_present:\n - name: iface1\n - subnet: vnet1_sn1\n - virtual_network: vnet1\n - resource_group: group1\n - ip_configurations:\n - name: iface1_ipc1\n public_ip_address: pub_ip2\n - dns_settings:\n internal_dns_name_label: decisionlab-int-test-label\n - primary: True\n - enable_accelerated_networking: True\n - enable_ip_forwarding: False\n - network_security_group: nsg1\n - connection_auth: {{ profile }}\n - require:\n - azurearm_network: Ensure subnet exists\n - azurearm_network: Ensure network security group exists\n - azurearm_network: Ensure another public IP exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n iface = __salt__['azurearm_network.network_interface_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in iface:\n # tag changes\n tag_changes = __utils__['dictdiffer.deep_diff'](iface.get('tags', {}), tags or {})\n if tag_changes:\n ret['changes']['tags'] = tag_changes\n\n # mac_address changes\n if mac_address and (mac_address != iface.get('mac_address')):\n ret['changes']['mac_address'] = {\n 'old': iface.get('mac_address'),\n 'new': mac_address\n }\n\n # primary changes\n if primary is not None:\n if primary != iface.get('primary', True):\n ret['changes']['primary'] = {\n 'old': iface.get('primary'),\n 'new': primary\n }\n\n # enable_accelerated_networking changes\n if enable_accelerated_networking is not None:\n if enable_accelerated_networking != iface.get('enable_accelerated_networking'):\n ret['changes']['enable_accelerated_networking'] = {\n 'old': iface.get('enable_accelerated_networking'),\n 'new': enable_accelerated_networking\n }\n\n # enable_ip_forwarding changes\n if enable_ip_forwarding is not None:\n if enable_ip_forwarding != iface.get('enable_ip_forwarding'):\n ret['changes']['enable_ip_forwarding'] = {\n 'old': iface.get('enable_ip_forwarding'),\n 'new': enable_ip_forwarding\n }\n\n # network_security_group changes\n nsg_name = None\n if iface.get('network_security_group'):\n nsg_name = iface['network_security_group']['id'].split('/')[-1]\n\n if network_security_group and (network_security_group != nsg_name):\n ret['changes']['network_security_group'] = {\n 'old': nsg_name,\n 'new': network_security_group\n }\n\n # virtual_machine changes\n vm_name = None\n if iface.get('virtual_machine'):\n vm_name = iface['virtual_machine']['id'].split('/')[-1]\n\n if virtual_machine and (virtual_machine != vm_name):\n ret['changes']['virtual_machine'] = {\n 'old': vm_name,\n 'new': virtual_machine\n }\n\n # dns_settings changes\n if dns_settings:\n if not isinstance(dns_settings, dict):\n ret['comment'] = 'DNS settings must be provided as a dictionary!'\n return ret\n\n for key in dns_settings:\n if dns_settings[key].lower() != iface.get('dns_settings', {}).get(key, '').lower():\n ret['changes']['dns_settings'] = {\n 'old': iface.get('dns_settings'),\n 'new': dns_settings\n }\n break\n\n # ip_configurations changes\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](\n iface.get('ip_configurations', []),\n ip_configurations,\n ['public_ip_address', 'subnet']\n )\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"ip_configurations\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['ip_configurations'] = comp_ret['changes']\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Network interface {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Network interface {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'ip_configurations': ip_configurations,\n 'dns_settings': dns_settings,\n 'network_security_group': network_security_group,\n 'virtual_machine': virtual_machine,\n 'enable_accelerated_networking': enable_accelerated_networking,\n 'enable_ip_forwarding': enable_ip_forwarding,\n 'mac_address': mac_address,\n 'primary': primary,\n 'tags': tags,\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Network interface {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n iface_kwargs = kwargs.copy()\n iface_kwargs.update(connection_auth)\n\n iface = __salt__['azurearm_network.network_interface_create_or_update'](\n name=name,\n subnet=subnet,\n virtual_network=virtual_network,\n resource_group=resource_group,\n ip_configurations=ip_configurations,\n dns_settings=dns_settings,\n enable_accelerated_networking=enable_accelerated_networking,\n enable_ip_forwarding=enable_ip_forwarding,\n mac_address=mac_address,\n primary=primary,\n network_security_group=network_security_group,\n virtual_machine=virtual_machine,\n tags=tags,\n **iface_kwargs\n )\n\n if 'error' not in iface:\n ret['result'] = True\n ret['comment'] = 'Network interface {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create network interface {0}! ({1})'.format(name, iface.get('error'))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef network_interface_absent(name, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a network interface does not exist in the resource group.\n\n :param name:\n Name of the network interface.\n\n :param resource_group:\n The resource group assigned to the network interface.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n iface = __salt__['azurearm_network.network_interface_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in iface:\n ret['result'] = True\n ret['comment'] = 'Network interface {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Network interface {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': iface,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.network_interface_delete'](name, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Network interface {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': iface,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete network interface {0}!)'.format(name)\n return ret", "response": "Ensure a network interface does not exist in the resource group."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef route_table_present(name, resource_group, tags=None, routes=None, disable_bgp_route_propagation=None,\n connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a route table exists.\n\n :param name:\n Name of the route table.\n\n :param resource_group:\n The resource group assigned to the route table.\n\n :param routes:\n An optional list of dictionaries representing valid Route objects contained within a route table. See the\n documentation for the route_present state or route_create_or_update execution module for more information on\n required and optional parameters for routes. The routes are only managed if this parameter is present. When this\n parameter is absent, implemented routes will not be removed, and will merely become unmanaged.\n\n :param disable_bgp_route_propagation:\n An optional boolean parameter setting whether to disable the routes learned by BGP on the route table.\n\n :param tags:\n A dictionary of strings can be passed as tag metadata to the route table object.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure route table exists:\n azurearm_network.route_table_present:\n - name: rt1\n - resource_group: group1\n - routes:\n - name: rt1_route1\n address_prefix: '0.0.0.0/0'\n next_hop_type: internet\n - name: rt1_route2\n address_prefix: '192.168.0.0/16'\n next_hop_type: vnetlocal\n - tags:\n contact_name: Elmer Fudd Gantry\n - connection_auth: {{ profile }}\n - require:\n - azurearm_resource: Ensure resource group exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n rt_tbl = __salt__['azurearm_network.route_table_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in rt_tbl:\n # tag changes\n tag_changes = __utils__['dictdiffer.deep_diff'](rt_tbl.get('tags', {}), tags or {})\n if tag_changes:\n ret['changes']['tags'] = tag_changes\n\n # disable_bgp_route_propagation changes\n # pylint: disable=line-too-long\n if disable_bgp_route_propagation and (disable_bgp_route_propagation != rt_tbl.get('disable_bgp_route_propagation')):\n ret['changes']['disable_bgp_route_propagation'] = {\n 'old': rt_tbl.get('disable_bgp_route_propagation'),\n 'new': disable_bgp_route_propagation\n }\n\n # routes changes\n if routes:\n comp_ret = __utils__['azurearm.compare_list_of_dicts'](rt_tbl.get('routes', []), routes)\n\n if comp_ret.get('comment'):\n ret['comment'] = '\"routes\" {0}'.format(comp_ret['comment'])\n return ret\n\n if comp_ret.get('changes'):\n ret['changes']['routes'] = comp_ret['changes']\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Route table {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Route table {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'tags': tags,\n 'routes': routes,\n 'disable_bgp_route_propagation': disable_bgp_route_propagation,\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Route table {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n rt_tbl_kwargs = kwargs.copy()\n rt_tbl_kwargs.update(connection_auth)\n\n rt_tbl = __salt__['azurearm_network.route_table_create_or_update'](\n name=name,\n resource_group=resource_group,\n disable_bgp_route_propagation=disable_bgp_route_propagation,\n routes=routes,\n tags=tags,\n **rt_tbl_kwargs\n )\n\n if 'error' not in rt_tbl:\n ret['result'] = True\n ret['comment'] = 'Route table {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create route table {0}! ({1})'.format(name, rt_tbl.get('error'))\n return ret", "response": "Ensures that a route table with the given name and resource group exists."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring a route table is absent in the resource group.", "response": "def route_table_absent(name, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a route table does not exist in the resource group.\n\n :param name:\n Name of the route table.\n\n :param resource_group:\n The resource group assigned to the route table.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n rt_tbl = __salt__['azurearm_network.route_table_get'](\n name,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in rt_tbl:\n ret['result'] = True\n ret['comment'] = 'Route table {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Route table {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': rt_tbl,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.route_table_delete'](name, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Route table {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': rt_tbl,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete route table {0}!'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef route_present(name, address_prefix, next_hop_type, route_table, resource_group, next_hop_ip_address=None,\n connection_auth=None, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a route exists within a route table.\n\n :param name:\n Name of the route.\n\n :param address_prefix:\n The destination CIDR to which the route applies.\n\n :param next_hop_type:\n The type of Azure hop the packet should be sent to. Possible values are: 'VirtualNetworkGateway', 'VnetLocal',\n 'Internet', 'VirtualAppliance', and 'None'.\n\n :param next_hop_ip_address:\n The IP address packets should be forwarded to. Next hop values are only allowed in routes where the next hop\n type is 'VirtualAppliance'.\n\n :param route_table:\n The name of the existing route table which will contain the route.\n\n :param resource_group:\n The resource group assigned to the route table.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n\n Example usage:\n\n .. code-block:: yaml\n\n Ensure route exists:\n azurearm_network.route_present:\n - name: rt1_route2\n - route_table: rt1\n - resource_group: group1\n - address_prefix: '192.168.0.0/16'\n - next_hop_type: vnetlocal\n - connection_auth: {{ profile }}\n - require:\n - azurearm_network: Ensure route table exists\n\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n route = __salt__['azurearm_network.route_get'](\n name,\n route_table,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' not in route:\n if address_prefix != route.get('address_prefix'):\n ret['changes']['address_prefix'] = {\n 'old': route.get('address_prefix'),\n 'new': address_prefix\n }\n\n if next_hop_type.lower() != route.get('next_hop_type', '').lower():\n ret['changes']['next_hop_type'] = {\n 'old': route.get('next_hop_type'),\n 'new': next_hop_type\n }\n\n if next_hop_type.lower() == 'virtualappliance' and next_hop_ip_address != route.get('next_hop_ip_address'):\n ret['changes']['next_hop_ip_address'] = {\n 'old': route.get('next_hop_ip_address'),\n 'new': next_hop_ip_address\n }\n\n if not ret['changes']:\n ret['result'] = True\n ret['comment'] = 'Route {0} is already present.'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Route {0} would be updated.'.format(name)\n return ret\n\n else:\n ret['changes'] = {\n 'old': {},\n 'new': {\n 'name': name,\n 'address_prefix': address_prefix,\n 'next_hop_type': next_hop_type,\n 'next_hop_ip_address': next_hop_ip_address\n }\n }\n\n if __opts__['test']:\n ret['comment'] = 'Route {0} would be created.'.format(name)\n ret['result'] = None\n return ret\n\n route_kwargs = kwargs.copy()\n route_kwargs.update(connection_auth)\n\n route = __salt__['azurearm_network.route_create_or_update'](\n name=name,\n route_table=route_table,\n resource_group=resource_group,\n address_prefix=address_prefix,\n next_hop_type=next_hop_type,\n next_hop_ip_address=next_hop_ip_address,\n **route_kwargs\n )\n\n if 'error' not in route:\n ret['result'] = True\n ret['comment'] = 'Route {0} has been created.'.format(name)\n return ret\n\n ret['comment'] = 'Failed to create route {0}! ({1})'.format(name, route.get('error'))\n return ret", "response": "Ensure a route exists within a route table."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring a route table is absent in the resource group.", "response": "def route_absent(name, route_table, resource_group, connection_auth=None):\n '''\n .. versionadded:: 2019.2.0\n\n Ensure a route table does not exist in the resource group.\n\n :param name:\n Name of the route table.\n\n :param route_table:\n The name of the existing route table containing the route.\n\n :param resource_group:\n The resource group assigned to the route table.\n\n :param connection_auth:\n A dict with subscription and authentication parameters to be used in connecting to the\n Azure Resource Manager API.\n '''\n ret = {\n 'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}\n }\n\n if not isinstance(connection_auth, dict):\n ret['comment'] = 'Connection information must be specified via connection_auth dictionary!'\n return ret\n\n route = __salt__['azurearm_network.route_get'](\n name,\n route_table,\n resource_group,\n azurearm_log_level='info',\n **connection_auth\n )\n\n if 'error' in route:\n ret['result'] = True\n ret['comment'] = 'Route {0} was not found.'.format(name)\n return ret\n\n elif __opts__['test']:\n ret['comment'] = 'Route {0} would be deleted.'.format(name)\n ret['result'] = None\n ret['changes'] = {\n 'old': route,\n 'new': {},\n }\n return ret\n\n deleted = __salt__['azurearm_network.route_delete'](name, route_table, resource_group, **connection_auth)\n\n if deleted:\n ret['result'] = True\n ret['comment'] = 'Route {0} has been deleted.'.format(name)\n ret['changes'] = {\n 'old': route,\n 'new': {}\n }\n return ret\n\n ret['comment'] = 'Failed to delete route {0}!'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_jid(jid):\n '''\n Returns True if the passed in value is a job id\n '''\n if not isinstance(jid, six.string_types):\n return False\n if len(jid) != 20 and (len(jid) <= 21 or jid[20] != '_'):\n return False\n try:\n int(jid[:20])\n return True\n except ValueError:\n return False", "response": "Returns True if the passed in value is a job id\n Returns False otherwise"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef jid_to_time(jid):\n '''\n Convert a salt job id into the time when the job was invoked\n '''\n jid = six.text_type(jid)\n if len(jid) != 20 and (len(jid) <= 21 or jid[20] != '_'):\n return ''\n year = jid[:4]\n month = jid[4:6]\n day = jid[6:8]\n hour = jid[8:10]\n minute = jid[10:12]\n second = jid[12:14]\n micro = jid[14:20]\n\n ret = '{0}, {1} {2} {3}:{4}:{5}.{6}'.format(year,\n months[int(month)],\n day,\n hour,\n minute,\n second,\n micro)\n return ret", "response": "Convert a salt job id into a time when the job was invoked"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nformatting the job instance correctly", "response": "def format_job_instance(job):\n '''\n Format the job instance correctly\n '''\n ret = {'Function': job.get('fun', 'unknown-function'),\n 'Arguments': list(job.get('arg', [])),\n # unlikely but safeguard from invalid returns\n 'Target': job.get('tgt', 'unknown-target'),\n 'Target-type': job.get('tgt_type', 'list'),\n 'User': job.get('user', 'root')}\n\n if 'metadata' in job:\n ret['Metadata'] = job.get('metadata', {})\n else:\n if 'kwargs' in job:\n if 'metadata' in job['kwargs']:\n ret['Metadata'] = job['kwargs'].get('metadata', {})\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nformats the jid correctly", "response": "def format_jid_instance(jid, job):\n '''\n Format the jid correctly\n '''\n ret = format_job_instance(job)\n ret.update({'StartTime': jid_to_time(jid)})\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef format_jid_instance_ext(jid, job):\n '''\n Format the jid correctly with jid included\n '''\n ret = format_job_instance(job)\n ret.update({\n 'JID': jid,\n 'StartTime': jid_to_time(jid)})\n return ret", "response": "Format the jid correctly with jid included\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef jid_dir(jid, job_dir=None, hash_type='sha256'):\n '''\n Return the jid_dir for the given job id\n '''\n if not isinstance(jid, six.string_types):\n jid = six.text_type(jid)\n jhash = getattr(hashlib, hash_type)(\n salt.utils.stringutils.to_bytes(jid)).hexdigest()\n\n parts = []\n if job_dir is not None:\n parts.append(job_dir)\n parts.extend([jhash[:2], jhash[2:]])\n return os.path.join(*parts)", "response": "Return the jid_dir for the given job id"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchanges the system runlevel on sysV compatible systems CLI Example: .. code-block:: bash salt '*' system.init 3", "response": "def init(runlevel):\n '''\n Change the system runlevel on sysV compatible systems\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.init 3\n '''\n cmd = ['init', '{0}'.format(runlevel)]\n ret = __salt__['cmd.run'](cmd, python_shell=False)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef reboot(at_time=None):\n '''\n Reboot the system\n\n at_time\n The wait time in minutes before the system will be rebooted.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.reboot\n '''\n cmd = ['shutdown', '-r', ('{0}'.format(at_time) if at_time else 'now')]\n ret = __salt__['cmd.run'](cmd, python_shell=False)\n return ret", "response": "Reboot the system\n at_time"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef shutdown(at_time=None):\n '''\n Shutdown a running system\n\n at_time\n The wait time in minutes before the system will be shutdown.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.shutdown 5\n '''\n cmd = ['shutdown', '-h', ('{0}'.format(at_time) if at_time else 'now')]\n ret = __salt__['cmd.run'](cmd, python_shell=False)\n return ret", "response": "Shut down a running system\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _date_bin_set_datetime(new_date):\n '''\n set the system date/time using the date command\n\n Note using a strictly posix-compliant date binary we can only set the date\n up to the minute.\n '''\n cmd = ['date']\n\n # if there is a timezone in the datetime object use that offset\n # This will modify the new_date to be the equivalent time in UTC\n if new_date.utcoffset() is not None:\n new_date = new_date - new_date.utcoffset()\n new_date = new_date.replace(tzinfo=_FixedOffset(0))\n cmd.append('-u')\n\n # the date can be set in the following format:\n # Note that setting the time with a resolution of seconds\n # is not a posix feature, so we will attempt it and if it\n # fails we will try again only using posix features\n\n # date MMDDhhmm[[CC]YY[.ss]]\n non_posix = (\"{1:02}{2:02}{3:02}{4:02}{0:04}.{5:02}\"\n .format(*new_date.timetuple()))\n non_posix_cmd = cmd + [non_posix]\n\n ret_non_posix = __salt__['cmd.run_all'](non_posix_cmd, python_shell=False)\n if ret_non_posix['retcode'] != 0:\n # We will now try the command again following posix\n # date MMDDhhmm[[CC]YY]\n posix = \" {1:02}{2:02}{3:02}{4:02}{0:04}\".format(*new_date.timetuple())\n posix_cmd = cmd + [posix]\n\n ret_posix = __salt__['cmd.run_all'](posix_cmd, python_shell=False)\n if ret_posix['retcode'] != 0:\n # if both fail it's likely an invalid date string\n # so we will give back the error from the first attempt\n msg = 'date failed: {0}'.format(ret_non_posix['stderr'])\n raise CommandExecutionError(msg)\n return True", "response": "Set the date of the system using the date command"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn True if the system has a hardware clock capable of being set from software.", "response": "def has_settable_hwclock():\n '''\n Returns True if the system has a hardware clock capable of being\n set from software.\n\n CLI Example:\n\n salt '*' system.has_settable_hwclock\n '''\n if salt.utils.path.which_bin(['hwclock']) is not None:\n res = __salt__['cmd.run_all'](\n ['hwclock', '--test', '--systohc'], python_shell=False,\n output_loglevel='quiet', ignore_retcode=True\n )\n return res['retcode'] == 0\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _swclock_to_hwclock():\n '''\n Set hardware clock to value of software clock.\n '''\n res = __salt__['cmd.run_all'](['hwclock', '--systohc'], python_shell=False)\n if res['retcode'] != 0:\n msg = 'hwclock failed to set hardware clock from software clock: {0}'.format(res['stderr'])\n raise CommandExecutionError(msg)\n return True", "response": "Set hardware clock to value of software clock."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _offset_to_min(utc_offset):\n '''\n Helper function that converts the utc offset string into number of minutes\n offset. Input is in form \"[+-]?HHMM\". Example valid inputs are \"+0500\"\n \"-0300\" and \"0800\". These would return -300, 180, 480 respectively.\n '''\n match = re.match(r\"^([+-])?(\\d\\d)(\\d\\d)$\", utc_offset)\n if not match:\n raise SaltInvocationError(\"Invalid UTC offset\")\n\n sign = -1 if match.group(1) == '-' else 1\n hours_offset = int(match.group(2))\n minutes_offset = int(match.group(3))\n total_offset = sign * (hours_offset * 60 + minutes_offset)\n return total_offset", "response": "Helper function that converts the utc offset string into number of minutes\n offset. Input is in form HHMM - 0500 - 350 - 480."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the current time adjusted using the input timezone offset.", "response": "def _get_offset_time(utc_offset):\n '''\n Will return the current time adjusted using the input timezone offset.\n\n :rtype datetime:\n '''\n if utc_offset is not None:\n minutes = _offset_to_min(utc_offset)\n offset = timedelta(minutes=minutes)\n offset_time = datetime.utcnow() + offset\n offset_time = offset_time.replace(tzinfo=_FixedOffset(minutes))\n else:\n offset_time = datetime.now()\n return offset_time"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the system time for the current node.", "response": "def set_system_time(newtime, utc_offset=None):\n '''\n Set the system time.\n\n :param str newtime:\n The time to set. Can be any of the following formats.\n - HH:MM:SS AM/PM\n - HH:MM AM/PM\n - HH:MM:SS (24 hour)\n - HH:MM (24 hour)\n\n Note that the salt command line parser parses the date/time\n before we obtain the argument (preventing us from doing utc)\n Therefore the argument must be passed in as a string.\n Meaning you may have to quote the text twice from the command line.\n\n :param str utc_offset: The utc offset in 4 digit (+0600) format with an\n optional sign (+/-). Will default to None which will use the local\n timezone. To set the time based off of UTC use \"'+0000'\". Note: if\n being passed through the command line will need to be quoted twice to\n allow negative offsets.\n :return: Returns True if successful. Otherwise False.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_system_time \"'11:20'\"\n '''\n fmts = ['%I:%M:%S %p', '%I:%M %p', '%H:%M:%S', '%H:%M']\n dt_obj = _try_parse_datetime(newtime, fmts)\n if dt_obj is None:\n return False\n\n return set_system_date_time(hours=dt_obj.hour, minutes=dt_obj.minute,\n seconds=dt_obj.second, utc_offset=utc_offset)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the system date and time for the given set of values.", "response": "def set_system_date_time(years=None,\n months=None,\n days=None,\n hours=None,\n minutes=None,\n seconds=None,\n utc_offset=None):\n '''\n Set the system date and time. Each argument is an element of the date, but\n not required. If an element is not passed, the current system value for\n that element will be used. For example, if you don't pass the year, the\n current system year will be used. (Used by set_system_date and\n set_system_time)\n\n Updates hardware clock, if present, in addition to software\n (kernel) clock.\n\n :param int years: Years digit, ie: 2015\n :param int months: Months digit: 1 - 12\n :param int days: Days digit: 1 - 31\n :param int hours: Hours digit: 0 - 23\n :param int minutes: Minutes digit: 0 - 59\n :param int seconds: Seconds digit: 0 - 59\n :param str utc_offset: The utc offset in 4 digit (+0600) format with an\n optional sign (+/-). Will default to None which will use the local\n timezone. To set the time based off of UTC use \"'+0000'\". Note: if\n being passed through the command line will need to be quoted twice to\n allow negative offsets.\n :return: True if successful. Otherwise False.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_system_date_time 2015 5 12 11 37 53 \"'-0500'\"\n '''\n # Get the current date/time\n date_time = _get_offset_time(utc_offset)\n\n # Check for passed values. If not passed, use current values\n if years is None:\n years = date_time.year\n if months is None:\n months = date_time.month\n if days is None:\n days = date_time.day\n if hours is None:\n hours = date_time.hour\n if minutes is None:\n minutes = date_time.minute\n if seconds is None:\n seconds = date_time.second\n\n try:\n new_datetime = datetime(years, months, days, hours, minutes, seconds, 0,\n date_time.tzinfo)\n except ValueError as err:\n raise SaltInvocationError(err.message)\n\n if not _date_bin_set_datetime(new_datetime):\n return False\n\n if has_settable_hwclock():\n # Now that we've successfully set the software clock, we should\n # update hardware clock for time to persist though reboot.\n return _swclock_to_hwclock()\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_computer_desc():\n '''\n Get PRETTY_HOSTNAME value stored in /etc/machine-info\n If this file doesn't exist or the variable doesn't exist\n return False.\n\n :return: Value of PRETTY_HOSTNAME if this does not exist False.\n :rtype: str\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.get_computer_desc\n '''\n hostname_cmd = salt.utils.path.which('hostnamectl')\n if hostname_cmd:\n desc = __salt__['cmd.run'](\n [hostname_cmd, 'status', '--pretty'],\n python_shell=False\n )\n else:\n desc = None\n pattern = re.compile(r'^\\s*PRETTY_HOSTNAME=(.*)$')\n try:\n with salt.utils.files.fopen('/etc/machine-info', 'r') as mach_info:\n for line in mach_info.readlines():\n line = salt.utils.stringutils.to_unicode(line)\n match = pattern.match(line)\n if match:\n # get rid of whitespace then strip off quotes\n desc = _strip_quotes(match.group(1).strip())\n # no break so we get the last occurance\n except IOError:\n pass\n\n if desc is None:\n return False\n\n return desc.replace(r'\\\"', r'\"').replace(r'\\n', '\\n').replace(r'\\t', '\\t')", "response": "Return the value stored in the NIC_NAME file"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting the computer description in the neccesary way.", "response": "def set_computer_desc(desc):\n '''\n Set PRETTY_HOSTNAME value stored in /etc/machine-info\n This will create the file if it does not exist. If\n it is unable to create or modify this file returns False.\n\n :param str desc: The computer description\n :return: False on failure. True if successful.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_computer_desc \"Michael's laptop\"\n '''\n desc = salt.utils.stringutils.to_unicode(\n desc).replace('\"', r'\\\"').replace('\\n', r'\\n').replace('\\t', r'\\t')\n\n hostname_cmd = salt.utils.path.which('hostnamectl')\n if hostname_cmd:\n result = __salt__['cmd.retcode'](\n [hostname_cmd, 'set-hostname', '--pretty', desc],\n python_shell=False\n )\n return True if result == 0 else False\n\n if not os.path.isfile('/etc/machine-info'):\n with salt.utils.files.fopen('/etc/machine-info', 'w'):\n pass\n\n pattern = re.compile(r'^\\s*PRETTY_HOSTNAME=(.*)$')\n new_line = salt.utils.stringutils.to_str('PRETTY_HOSTNAME=\"{0}\"'.format(desc))\n try:\n with salt.utils.files.fopen('/etc/machine-info', 'r+') as mach_info:\n lines = mach_info.readlines()\n for i, line in enumerate(lines):\n if pattern.match(salt.utils.stringutils.to_unicode(line)):\n lines[i] = new_line\n break\n else:\n # PRETTY_HOSTNAME line was not found, add it to the end\n lines.append(new_line)\n # time to write our changes to the file\n mach_info.seek(0, 0)\n mach_info.truncate()\n mach_info.writelines(lines)\n return True\n except IOError:\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef handle_stream(self, stream):\n '''\n Override this to handle the streams as they arrive\n\n :param IOStream stream: An IOStream for processing\n\n See https://tornado.readthedocs.io/en/latest/iostream.html#tornado.iostream.IOStream\n for additional details.\n '''\n @tornado.gen.coroutine\n def _null(msg):\n raise tornado.gen.Return(None)\n\n def write_callback(stream, header):\n if header.get('mid'):\n @tornado.gen.coroutine\n def return_message(msg):\n pack = salt.transport.frame.frame_msg_ipc(\n msg,\n header={'mid': header['mid']},\n raw_body=True,\n )\n yield stream.write(pack)\n return return_message\n else:\n return _null\n if six.PY2:\n encoding = None\n else:\n encoding = 'utf-8'\n unpacker = msgpack.Unpacker(encoding=encoding)\n while not stream.closed():\n try:\n wire_bytes = yield stream.read_bytes(4096, partial=True)\n unpacker.feed(wire_bytes)\n for framed_msg in unpacker:\n body = framed_msg['body']\n self.io_loop.spawn_callback(self.payload_handler, body, write_callback(stream, framed_msg['head']))\n except tornado.iostream.StreamClosedError:\n log.trace('Client disconnected from IPC %s', self.socket_path)\n break\n except socket.error as exc:\n # On occasion an exception will occur with\n # an error code of 0, it's a spurious exception.\n if exc.errno == 0:\n log.trace('Exception occured with error number 0, '\n 'spurious exception: %s', exc)\n else:\n log.error('Exception occurred while '\n 'handling stream: %s', exc)\n except Exception as exc:\n log.error('Exception occurred while '\n 'handling stream: %s', exc)", "response": "Handle the given stream and yield the messages that are available to the user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nclose the underlying socket and filehandles.", "response": "def close(self):\n '''\n Routines to handle any cleanup before the instance shuts down.\n Sockets and filehandles should be closed explicitly, to prevent\n leaks.\n '''\n if self._closing:\n return\n self._closing = True\n if hasattr(self.sock, 'close'):\n self.sock.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef connect(self, callback=None, timeout=None):\n '''\n Connect to the IPC socket\n '''\n if hasattr(self, '_connecting_future') and not self._connecting_future.done(): # pylint: disable=E0203\n future = self._connecting_future # pylint: disable=E0203\n else:\n if hasattr(self, '_connecting_future'):\n # read previous future result to prevent the \"unhandled future exception\" error\n self._connecting_future.exception() # pylint: disable=E0203\n future = tornado.concurrent.Future()\n self._connecting_future = future\n self._connect(timeout=timeout)\n\n if callback is not None:\n def handle_future(future):\n response = future.result()\n self.io_loop.add_callback(callback, response)\n future.add_done_callback(handle_future)\n\n return future", "response": "Connect to the IPC socket and return a future that will be returned when the socket is ready."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _connect(self, timeout=None):\n '''\n Connect to a running IPCServer\n '''\n if isinstance(self.socket_path, int):\n sock_type = socket.AF_INET\n sock_addr = ('127.0.0.1', self.socket_path)\n else:\n sock_type = socket.AF_UNIX\n sock_addr = self.socket_path\n\n self.stream = None\n if timeout is not None:\n timeout_at = time.time() + timeout\n\n while True:\n if self._closing:\n break\n\n if self.stream is None:\n with salt.utils.asynchronous.current_ioloop(self.io_loop):\n self.stream = IOStream(\n socket.socket(sock_type, socket.SOCK_STREAM),\n )\n\n try:\n log.trace('IPCClient: Connecting to socket: %s', self.socket_path)\n yield self.stream.connect(sock_addr)\n self._connecting_future.set_result(True)\n break\n except Exception as e:\n if self.stream.closed():\n self.stream = None\n\n if timeout is None or time.time() > timeout_at:\n if self.stream is not None:\n self.stream.close()\n self.stream = None\n self._connecting_future.set_exception(e)\n break\n\n yield tornado.gen.sleep(1)", "response": "Connect to a running IPCServer and return a tornado. Future that fires when the connection is complete."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef close(self):\n '''\n Routines to handle any cleanup before the instance shuts down.\n Sockets and filehandles should be closed explicitly, to prevent\n leaks.\n '''\n if self._closing:\n return\n\n if self._refcount > 1:\n # Decrease refcount\n with self._refcount_lock:\n self._refcount -= 1\n log.debug(\n 'This is not the last %s instance. Not closing yet.',\n self.__class__.__name__\n )\n return\n\n self._closing = True\n\n log.debug('Closing %s instance', self.__class__.__name__)\n\n if self.stream is not None and not self.stream.closed():\n self.stream.close()\n\n # Remove the entry from the instance map so\n # that a closed entry may not be reused.\n # This forces this operation even if the reference\n # count of the entry has not yet gone to zero.\n if self.io_loop in self.__class__.instance_map:\n loop_instance_map = self.__class__.instance_map[self.io_loop]\n if self._instance_key in loop_instance_map:\n del loop_instance_map[self._instance_key]\n if not loop_instance_map:\n del self.__class__.instance_map[self.io_loop]", "response": "Closes the current instance."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsends a message to an IPC socket.", "response": "def send(self, msg, timeout=None, tries=None):\n '''\n Send a message to an IPC socket\n\n If the socket is not currently connected, a connection will be established.\n\n :param dict msg: The message to be sent\n :param int timeout: Timeout when sending message (Currently unimplemented)\n '''\n if not self.connected():\n yield self.connect()\n pack = salt.transport.frame.frame_msg_ipc(msg, raw_body=True)\n yield self.stream.write(pack)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nstarts up the Tornado IPC server.", "response": "def start(self):\n '''\n Perform the work necessary to start up a Tornado IPC server\n\n Blocks until socket is established\n '''\n # Start up the ioloop\n log.trace('IPCMessagePublisher: binding to socket: %s', self.socket_path)\n if isinstance(self.socket_path, int):\n self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n if self.opts.get('ipc_so_sndbuf'):\n self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.opts['ipc_so_sndbuf'])\n if self.opts.get('ipc_so_rcvbuf'):\n self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.opts['ipc_so_rcvbuf'])\n self.sock.setblocking(0)\n self.sock.bind(('127.0.0.1', self.socket_path))\n # Based on default used in tornado.netutil.bind_sockets()\n self.sock.listen(self.opts['ipc_so_backlog'])\n else:\n # sndbuf/rcvbuf does not apply to unix sockets\n self.sock = tornado.netutil.bind_unix_socket(self.socket_path, backlog=self.opts['ipc_so_backlog'])\n\n with salt.utils.asynchronous.current_ioloop(self.io_loop):\n tornado.netutil.add_accept_handler(\n self.sock,\n self.handle_connection,\n )\n self._started = True"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef publish(self, msg):\n '''\n Send message to all connected sockets\n '''\n if not self.streams:\n return\n\n pack = salt.transport.frame.frame_msg_ipc(msg, raw_body=True)\n\n for stream in self.streams:\n self.io_loop.spawn_callback(self._write, stream, pack)", "response": "Send message to all connected sockets"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nclose the underlying socket and filehandles.", "response": "def close(self):\n '''\n Routines to handle any cleanup before the instance shuts down.\n Sockets and filehandles should be closed explicitly, to prevent\n leaks.\n '''\n if self._closing:\n return\n self._closing = True\n for stream in self.streams:\n stream.close()\n self.streams.clear()\n if hasattr(self.sock, 'close'):\n self.sock.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreads a message from an IPC socket and return it.", "response": "def read_sync(self, timeout=None):\n '''\n Read a message from an IPC socket\n\n The socket must already be connected.\n The associated IO Loop must NOT be running.\n :param int timeout: Timeout when receiving message\n :return: message data if successful. None if timed out. Will raise an\n exception for all other error conditions.\n '''\n if self.saved_data:\n return self.saved_data.pop(0)\n\n self._sync_ioloop_running = True\n self._read_sync_future = self._read_sync(timeout)\n self.io_loop.start()\n self._sync_ioloop_running = False\n\n ret_future = self._read_sync_future\n self._read_sync_future = None\n return ret_future.result()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_(show_all=False,\n show_disabled=True,\n where=None,\n return_yaml=True):\n '''\n List the jobs currently scheduled on the minion\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.list\n\n # Show all jobs including hidden internal jobs\n salt '*' schedule.list show_all=True\n\n # Hide disabled jobs from list of jobs\n salt '*' schedule.list show_disabled=False\n\n '''\n\n schedule = {}\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'func': 'list',\n 'where': where}, 'manage_schedule')\n if res:\n event_ret = eventer.get_event(tag='/salt/minion/minion_schedule_list_complete', wait=30)\n if event_ret and event_ret['complete']:\n schedule = event_ret['schedule']\n except KeyError:\n # Effectively a no-op, since we can't really return without an event system\n ret = {}\n ret['comment'] = 'Event module not available. Schedule list failed.'\n ret['result'] = True\n log.debug('Event module not available. Schedule list failed.')\n return ret\n\n _hidden = ['enabled',\n 'skip_function',\n 'skip_during_range']\n for job in list(schedule.keys()): # iterate over a copy since we will mutate it\n if job in _hidden:\n continue\n\n # Default jobs added by salt begin with __\n # by default hide them unless show_all is True.\n if job.startswith('__') and not show_all:\n del schedule[job]\n continue\n\n # if enabled is not included in the job,\n # assume job is enabled.\n if 'enabled' not in schedule[job]:\n schedule[job]['enabled'] = True\n\n for item in pycopy.copy(schedule[job]):\n if item not in SCHEDULE_CONF:\n del schedule[job][item]\n continue\n if schedule[job][item] is None:\n del schedule[job][item]\n continue\n if schedule[job][item] == 'true':\n schedule[job][item] = True\n if schedule[job][item] == 'false':\n schedule[job][item] = False\n\n # if the job is disabled and show_disabled is False, skip job\n if not show_disabled and not schedule[job]['enabled']:\n del schedule[job]\n continue\n\n if '_seconds' in schedule[job]:\n # remove _seconds from the listing\n del schedule[job]['_seconds']\n\n if schedule:\n if return_yaml:\n tmp = {'schedule': schedule}\n return salt.utils.yaml.safe_dump(tmp, default_flow_style=False)\n else:\n return schedule\n else:\n return {'schedule': {}}", "response": "List the currently scheduled jobs on the minion"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef is_enabled(name):\n '''\n List a Job only if its enabled\n\n .. versionadded:: 2015.5.3\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.is_enabled name=job_name\n '''\n\n current_schedule = __salt__['schedule.list'](show_all=False, return_yaml=False)\n if name in current_schedule:\n return current_schedule[name]\n else:\n return {}", "response": "Return a Job only if its enabled"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef purge(**kwargs):\n '''\n Purge all the jobs currently scheduled on the minion\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.purge\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n for name in list_(show_all=True, return_yaml=False):\n if name == 'enabled':\n continue\n if name.startswith('__'):\n continue\n\n if 'test' in kwargs and kwargs['test']:\n ret['result'] = True\n ret['comment'].append('Job: {0} would be deleted from schedule.'.format(name))\n else:\n\n persist = True\n if 'persist' in kwargs:\n persist = kwargs['persist']\n\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'name': name,\n 'func': 'delete',\n 'persist': persist}, 'manage_schedule')\n if res:\n event_ret = eventer.get_event(tag='/salt/minion/minion_schedule_delete_complete', wait=30)\n if event_ret and event_ret['complete']:\n _schedule_ret = event_ret['schedule']\n if name not in _schedule_ret:\n ret['result'] = True\n ret['comment'].append('Deleted job: {0} from schedule.'.format(name))\n else:\n ret['comment'].append('Failed to delete job {0} from schedule.'.format(name))\n ret['result'] = True\n\n except KeyError:\n # Effectively a no-op, since we can't really return without an event system\n ret['comment'] = 'Event module not available. Schedule add failed.'\n ret['result'] = True\n return ret", "response": "Purge all the jobs currently scheduled on the minion"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nbuild a schedule item", "response": "def build_schedule_item(name, **kwargs):\n '''\n Build a schedule job\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.build_schedule_item job1 function='test.ping' seconds=3600\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n if not name:\n ret['comment'] = 'Job name is required.'\n ret['result'] = False\n return ret\n\n schedule = {}\n schedule[name] = salt.utils.odict.OrderedDict()\n schedule[name]['function'] = kwargs['function']\n\n time_conflict = False\n for item in ['seconds', 'minutes', 'hours', 'days']:\n if item in kwargs and 'when' in kwargs:\n time_conflict = True\n\n if item in kwargs and 'cron' in kwargs:\n time_conflict = True\n\n if time_conflict:\n ret['result'] = False\n ret['comment'] = 'Unable to use \"seconds\", \"minutes\", \"hours\", or \"days\" with \"when\" or \"cron\" options.'\n return ret\n\n if 'when' in kwargs and 'cron' in kwargs:\n ret['result'] = False\n ret['comment'] = 'Unable to use \"when\" and \"cron\" options together. Ignoring.'\n return ret\n\n for item in ['seconds', 'minutes', 'hours', 'days']:\n if item in kwargs:\n schedule[name][item] = kwargs[item]\n\n if 'return_job' in kwargs:\n schedule[name]['return_job'] = kwargs['return_job']\n\n if 'metadata' in kwargs:\n schedule[name]['metadata'] = kwargs['metadata']\n\n if 'job_args' in kwargs:\n schedule[name]['args'] = kwargs['job_args']\n\n if 'job_kwargs' in kwargs:\n schedule[name]['kwargs'] = kwargs['job_kwargs']\n\n if 'maxrunning' in kwargs:\n schedule[name]['maxrunning'] = kwargs['maxrunning']\n else:\n schedule[name]['maxrunning'] = 1\n\n if 'name' in kwargs:\n schedule[name]['name'] = kwargs['name']\n else:\n schedule[name]['name'] = name\n\n if 'enabled' in kwargs:\n schedule[name]['enabled'] = kwargs['enabled']\n else:\n schedule[name]['enabled'] = True\n\n if 'jid_include' not in kwargs or kwargs['jid_include']:\n schedule[name]['jid_include'] = True\n\n if 'splay' in kwargs:\n if isinstance(kwargs['splay'], dict):\n # Ensure ordering of start and end arguments\n schedule[name]['splay'] = salt.utils.odict.OrderedDict()\n schedule[name]['splay']['start'] = kwargs['splay']['start']\n schedule[name]['splay']['end'] = kwargs['splay']['end']\n else:\n schedule[name]['splay'] = kwargs['splay']\n\n if 'when' in kwargs:\n if not _WHEN_SUPPORTED:\n ret['result'] = False\n ret['comment'] = 'Missing dateutil.parser, \"when\" is unavailable.'\n return ret\n else:\n validate_when = kwargs['when']\n if not isinstance(validate_when, list):\n validate_when = [validate_when]\n for _when in validate_when:\n try:\n dateutil_parser.parse(_when)\n except ValueError:\n ret['result'] = False\n ret['comment'] = 'Schedule item {0} for \"when\" in invalid.'.format(_when)\n return ret\n\n for item in ['range', 'when', 'once', 'once_fmt', 'cron',\n 'returner', 'after', 'return_config', 'return_kwargs',\n 'until', 'run_on_start', 'skip_during_range']:\n if item in kwargs:\n schedule[name][item] = kwargs[item]\n\n return schedule[name]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd a job to the schedule.", "response": "def add(name, **kwargs):\n '''\n Add a job to the schedule\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.add job1 function='test.ping' seconds=3600\n # If function have some arguments, use job_args\n salt '*' schedule.add job2 function='cmd.run' job_args=\"['date >> /tmp/date.log']\" seconds=60\n '''\n\n ret = {'comment': 'Failed to add job {0} to schedule.'.format(name),\n 'result': False}\n\n if name in list_(show_all=True, return_yaml=False):\n ret['comment'] = 'Job {0} already exists in schedule.'.format(name)\n ret['result'] = False\n return ret\n\n if not name:\n ret['comment'] = 'Job name is required.'\n ret['result'] = False\n\n time_conflict = False\n for item in ['seconds', 'minutes', 'hours', 'days']:\n if item in kwargs and 'when' in kwargs:\n time_conflict = True\n if item in kwargs and 'cron' in kwargs:\n time_conflict = True\n\n if time_conflict:\n ret['comment'] = 'Error: Unable to use \"seconds\", \"minutes\", \"hours\", or \"days\" with \"when\" or \"cron\" options.'\n return ret\n\n if 'when' in kwargs and 'cron' in kwargs:\n ret['comment'] = 'Unable to use \"when\" and \"cron\" options together. Ignoring.'\n return ret\n\n persist = True\n if 'persist' in kwargs:\n persist = kwargs['persist']\n\n _new = build_schedule_item(name, **kwargs)\n if 'result' in _new and not _new['result']:\n return _new\n\n schedule_data = {}\n schedule_data[name] = _new\n\n if 'test' in kwargs and kwargs['test']:\n ret['comment'] = 'Job: {0} would be added to schedule.'.format(name)\n ret['result'] = True\n else:\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'name': name,\n 'schedule': schedule_data,\n 'func': 'add',\n 'persist': persist}, 'manage_schedule')\n if res:\n event_ret = eventer.get_event(tag='/salt/minion/minion_schedule_add_complete', wait=30)\n if event_ret and event_ret['complete']:\n schedule = event_ret['schedule']\n if name in schedule:\n ret['result'] = True\n ret['comment'] = 'Added job: {0} to schedule.'.format(name)\n return ret\n except KeyError:\n # Effectively a no-op, since we can't really return without an event system\n ret['comment'] = 'Event module not available. Schedule add failed.'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nmodify an existing job in the schedule", "response": "def modify(name, **kwargs):\n '''\n Modify an existing job in the schedule\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.modify job1 function='test.ping' seconds=3600\n '''\n\n ret = {'comment': '',\n 'changes': {},\n 'result': True}\n\n time_conflict = False\n for item in ['seconds', 'minutes', 'hours', 'days']:\n if item in kwargs and 'when' in kwargs:\n time_conflict = True\n\n if item in kwargs and 'cron' in kwargs:\n time_conflict = True\n\n if time_conflict:\n ret['result'] = False\n ret['comment'] = 'Error: Unable to use \"seconds\", \"minutes\", \"hours\", or \"days\" with \"when\" option.'\n return ret\n\n if 'when' in kwargs and 'cron' in kwargs:\n ret['result'] = False\n ret['comment'] = 'Unable to use \"when\" and \"cron\" options together. Ignoring.'\n return ret\n\n current_schedule = list_(show_all=True, return_yaml=False)\n\n if name not in current_schedule:\n ret['comment'] = 'Job {0} does not exist in schedule.'.format(name)\n ret['result'] = False\n return ret\n\n _current = current_schedule[name]\n if '_seconds' in _current:\n _current['seconds'] = _current['_seconds']\n del _current['_seconds']\n\n _new = build_schedule_item(name, **kwargs)\n if 'result' in _new and not _new['result']:\n return _new\n\n if _new == _current:\n ret['comment'] = 'Job {0} in correct state'.format(name)\n return ret\n\n _current_lines = ['{0}:{1}\\n'.format(key, value)\n for (key, value) in sorted(_current.items())]\n _new_lines = ['{0}:{1}\\n'.format(key, value)\n for (key, value) in sorted(_new.items())]\n _diff = difflib.unified_diff(_current_lines, _new_lines)\n\n ret['changes']['diff'] = ''.join(_diff)\n\n if 'test' in kwargs and kwargs['test']:\n ret['comment'] = 'Job: {0} would be modified in schedule.'.format(name)\n else:\n persist = True\n if 'persist' in kwargs:\n persist = kwargs['persist']\n if name in list_(show_all=True, where='opts', return_yaml=False):\n event_data = {'name': name,\n 'schedule': _new,\n 'func': 'modify',\n 'persist': persist}\n elif name in list_(show_all=True, where='pillar', return_yaml=False):\n event_data = {'name': name,\n 'schedule': _new,\n 'where': 'pillar',\n 'func': 'modify',\n 'persist': False}\n\n out = __salt__['event.fire'](event_data, 'manage_schedule')\n if out:\n ret['comment'] = 'Modified job: {0} in schedule.'.format(name)\n else:\n ret['comment'] = 'Failed to modify job {0} in schedule.'.format(name)\n ret['result'] = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef run_job(name, force=False):\n '''\n Run a scheduled job on the minion immediately\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.run_job job1\n\n salt '*' schedule.run_job job1 force=True\n Force the job to run even if it is disabled.\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n if not name:\n ret['comment'] = 'Job name is required.'\n ret['result'] = False\n\n schedule = list_(show_all=True, return_yaml=False)\n if name in schedule:\n data = schedule[name]\n if 'enabled' in data and not data['enabled'] and not force:\n ret['comment'] = 'Job {0} is disabled.'.format(name)\n else:\n out = __salt__['event.fire']({'name': name, 'func': 'run_job'}, 'manage_schedule')\n if out:\n ret['comment'] = 'Scheduling Job {0} on minion.'.format(name)\n else:\n ret['comment'] = 'Failed to run job {0} on minion.'.format(name)\n ret['result'] = False\n else:\n ret['comment'] = 'Job {0} does not exist.'.format(name)\n ret['result'] = False\n return ret", "response": "Run a scheduled job on the minion immediately\n archive."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef enable_job(name, **kwargs):\n '''\n Enable a job in the minion's schedule\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.enable_job job1\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n if not name:\n ret['comment'] = 'Job name is required.'\n ret['result'] = False\n\n if 'test' in __opts__ and __opts__['test']:\n ret['comment'] = 'Job: {0} would be enabled in schedule.'.format(name)\n else:\n persist = True\n if 'persist' in kwargs:\n persist = kwargs['persist']\n\n if name in list_(show_all=True, where='opts', return_yaml=False):\n event_data = {'name': name, 'func': 'enable_job', 'persist': persist}\n elif name in list_(show_all=True, where='pillar', return_yaml=False):\n event_data = {'name': name, 'where': 'pillar', 'func': 'enable_job', 'persist': False}\n else:\n ret['comment'] = 'Job {0} does not exist.'.format(name)\n ret['result'] = False\n return ret\n\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire'](event_data, 'manage_schedule')\n if res:\n event_ret = eventer.get_event(tag='/salt/minion/minion_schedule_enabled_job_complete', wait=30)\n if event_ret and event_ret['complete']:\n schedule = event_ret['schedule']\n # check item exists in schedule and is enabled\n if name in schedule and schedule[name]['enabled']:\n ret['result'] = True\n ret['comment'] = 'Enabled Job {0} in schedule.'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to enable job {0} in schedule.'.format(name)\n return ret\n except KeyError:\n # Effectively a no-op, since we can't really return without an event system\n ret['comment'] = 'Event module not available. Schedule enable job failed.'\n return ret", "response": "Enable a job in the salt s schedule"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsaves all scheduled jobs on the minion", "response": "def save(**kwargs):\n '''\n Save all scheduled jobs on the minion\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.save\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n if 'test' in kwargs and kwargs['test']:\n ret['comment'] = 'Schedule would be saved.'\n else:\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'func': 'save_schedule'}, 'manage_schedule')\n if res:\n event_ret = eventer.get_event(tag='/salt/minion/minion_schedule_saved', wait=30)\n if event_ret and event_ret['complete']:\n ret['result'] = True\n ret['comment'] = 'Schedule (non-pillar items) saved.'\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to save schedule.'\n except KeyError:\n # Effectively a no-op, since we can't really return without an event system\n ret['comment'] = 'Event module not available. Schedule save failed.'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreload the current scheduler on the minion", "response": "def reload_():\n '''\n Reload saved scheduled jobs on the minion\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.reload\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n # If there a schedule defined in pillar, refresh it.\n if 'schedule' in __pillar__:\n out = __salt__['event.fire']({}, 'pillar_refresh')\n if out:\n ret['comment'].append('Reloaded schedule from pillar on minion.')\n else:\n ret['comment'].append('Failed to reload schedule from pillar on minion.')\n ret['result'] = False\n\n # move this file into an configurable opt\n sfn = '{0}/{1}/schedule.conf'.format(__opts__['config_dir'], os.path.dirname(__opts__['default_include']))\n if os.path.isfile(sfn):\n with salt.utils.files.fopen(sfn, 'rb') as fp_:\n try:\n schedule = salt.utils.yaml.safe_load(fp_)\n except salt.utils.yaml.YAMLError as exc:\n ret['comment'].append('Unable to read existing schedule file: {0}'.format(exc))\n\n if schedule:\n if 'schedule' in schedule and schedule['schedule']:\n out = __salt__['event.fire']({'func': 'reload', 'schedule': schedule}, 'manage_schedule')\n if out:\n ret['comment'].append('Reloaded schedule on minion from schedule.conf.')\n else:\n ret['comment'].append('Failed to reload schedule on minion from schedule.conf.')\n ret['result'] = False\n else:\n ret['comment'].append('Failed to reload schedule on minion. Saved file is empty or invalid.')\n ret['result'] = False\n else:\n ret['comment'].append('Failed to reload schedule on minion. Saved file is empty or invalid.')\n ret['result'] = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nmove a scheduled job to another minion or minions.", "response": "def move(name, target, **kwargs):\n '''\n Move scheduled job to another minion or minions.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.move jobname target\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n if not name:\n ret['comment'] = 'Job name is required.'\n ret['result'] = False\n\n if 'test' in kwargs and kwargs['test']:\n ret['comment'] = 'Job: {0} would be moved from schedule.'.format(name)\n else:\n opts_schedule = list_(show_all=True, where='opts', return_yaml=False)\n pillar_schedule = list_(show_all=True, where='pillar', return_yaml=False)\n\n if name in opts_schedule:\n schedule_data = opts_schedule[name]\n where = None\n elif name in pillar_schedule:\n schedule_data = pillar_schedule[name]\n where = 'pillar'\n else:\n ret['comment'] = 'Job {0} does not exist.'.format(name)\n ret['result'] = False\n return ret\n\n schedule_opts = []\n for key, value in six.iteritems(schedule_data):\n temp = '{0}={1}'.format(key, value)\n schedule_opts.append(temp)\n response = __salt__['publish.publish'](target, 'schedule.add', schedule_opts)\n\n # Get errors and list of affeced minions\n errors = []\n minions = []\n for minion in response:\n minions.append(minion)\n if not response[minion]:\n errors.append(minion)\n\n # parse response\n if not response:\n ret['comment'] = 'no servers answered the published schedule.add command'\n return ret\n elif errors:\n ret['comment'] = 'the following minions return False'\n ret['minions'] = errors\n return ret\n else:\n delete(name, where=where)\n ret['result'] = True\n ret['comment'] = 'Moved Job {0} from schedule.'.format(name)\n ret['minions'] = minions\n return ret\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\npostpone a job in the schedule", "response": "def postpone_job(name,\n current_time,\n new_time,\n **kwargs):\n '''\n Postpone a job in the minion's schedule\n\n Current time and new time should be in date string format,\n default value is %Y-%m-%dT%H:%M:%S.\n\n .. versionadded:: 2018.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' schedule.postpone_job job current_time new_time\n\n salt '*' schedule.postpone_job job current_time new_time time_fmt='%Y-%m-%dT%H:%M:%S'\n '''\n\n time_fmt = kwargs.get('time_fmt') or '%Y-%m-%dT%H:%M:%S'\n ret = {'comment': [],\n 'result': True}\n\n if not name:\n ret['comment'] = 'Job name is required.'\n ret['result'] = False\n return ret\n\n if not current_time:\n ret['comment'] = 'Job current time is required.'\n ret['result'] = False\n return ret\n else:\n try:\n # Validate date string\n datetime.datetime.strptime(current_time, time_fmt)\n except (TypeError, ValueError):\n log.error('Date string could not be parsed: %s, %s',\n new_time, time_fmt)\n\n ret['comment'] = 'Date string could not be parsed.'\n ret['result'] = False\n return ret\n\n if not new_time:\n ret['comment'] = 'Job new_time is required.'\n ret['result'] = False\n return ret\n else:\n try:\n # Validate date string\n datetime.datetime.strptime(new_time, time_fmt)\n except (TypeError, ValueError):\n log.error('Date string could not be parsed: %s, %s',\n new_time, time_fmt)\n\n ret['comment'] = 'Date string could not be parsed.'\n ret['result'] = False\n return ret\n\n if 'test' in __opts__ and __opts__['test']:\n ret['comment'] = 'Job: {0} would be postponed in schedule.'.format(name)\n else:\n\n if name in list_(show_all=True, where='opts', return_yaml=False):\n event_data = {'name': name,\n 'time': current_time,\n 'new_time': new_time,\n 'time_fmt': time_fmt,\n 'func': 'postpone_job'}\n elif name in list_(show_all=True, where='pillar', return_yaml=False):\n event_data = {'name': name,\n 'time': current_time,\n 'new_time': new_time,\n 'time_fmt': time_fmt,\n 'where': 'pillar',\n 'func': 'postpone_job'}\n else:\n ret['comment'] = 'Job {0} does not exist.'.format(name)\n ret['result'] = False\n return ret\n\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire'](event_data, 'manage_schedule')\n if res:\n event_ret = eventer.get_event(tag='/salt/minion/minion_schedule_postpone_job_complete', wait=30)\n if event_ret and event_ret['complete']:\n schedule = event_ret['schedule']\n # check item exists in schedule and is enabled\n if name in schedule and schedule[name]['enabled']:\n ret['result'] = True\n ret['comment'] = 'Postponed Job {0} in schedule.'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to postpone job {0} in schedule.'.format(name)\n return ret\n except KeyError:\n # Effectively a no-op, since we can't really return without an event system\n ret['comment'] = 'Event module not available. Schedule postpone job failed.'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef is_proxy():\n '''\n Return True if this minion is a proxy minion.\n Leverages the fact that is_linux() and is_windows\n both return False for proxies.\n TODO: Need to extend this for proxies that might run on\n other Unices\n '''\n import __main__ as main\n # This is a hack. If a proxy minion is started by other\n # means, e.g. a custom script that creates the minion objects\n # then this will fail.\n ret = False\n try:\n # Changed this from 'salt-proxy in main...' to 'proxy in main...'\n # to support the testsuite's temp script that is called 'cli_salt_proxy'\n #\n # Add '--proxyid' in sys.argv so that salt-call --proxyid\n # is seen as a proxy minion\n if 'proxy' in main.__file__ or '--proxyid' in sys.argv:\n ret = True\n except AttributeError:\n pass\n return ret", "response": "Return True if this minion is a proxy minion."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nfunction to return if host is SmartOS global zone or not", "response": "def is_smartos_globalzone():\n '''\n Function to return if host is SmartOS (Illumos) global zone or not\n '''\n if not is_smartos():\n return False\n else:\n cmd = ['zonename']\n try:\n zonename = subprocess.Popen(\n cmd, shell=False,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n except OSError:\n return False\n if zonename.returncode:\n return False\n if zonename.stdout.read().strip() == 'global':\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a new image for a specific platform and root.", "response": "def bootstrap(\n platform,\n root,\n img_format='dir',\n fs_format='ext2',\n fs_opts=None,\n arch=None,\n flavor=None,\n repo_url=None,\n static_qemu=None,\n img_size=None,\n mount_dir=None,\n pkg_cache=None,\n pkgs=None,\n exclude_pkgs=None,\n epel_url=EPEL_URL,\n ):\n '''\n Create an image for a specific platform.\n\n Please note that this function *MUST* be run as root, as images that are\n created make files belonging to root.\n\n platform\n Which platform to use to create the image. Currently supported platforms\n are rpm, deb and pacman.\n\n root\n Local path to create the root of the image filesystem.\n\n img_format\n Which format to create the image in. By default, just copies files into\n a directory on the local filesystem (``dir``). Future support will exist\n for ``sparse``.\n\n fs_format\n When using a non-``dir`` ``img_format``, which filesystem to format the\n image to. By default, ``ext2``.\n\n fs_opts\n When using a non-``dir`` ``img_format``, a dict of opts may be\n specified.\n\n arch\n Architecture to install packages for, if supported by the underlying\n bootstrap tool. Currently only used for deb.\n\n flavor\n Which flavor of operating system to install. This correlates to a\n specific directory on the distribution repositories. For instance,\n ``wheezy`` on Debian.\n\n repo_url\n Mainly important for Debian-based repos. Base URL for the mirror to\n install from. (e.x.: http://ftp.debian.org/debian/)\n\n static_qemu\n Local path to the static qemu binary required for this arch.\n (e.x.: /usr/bin/qemu-amd64-static)\n\n pkg_confs\n The location of the conf files to copy into the image, to point the\n installer to the right repos and configuration.\n\n img_size\n If img_format is not ``dir``, then the size of the image must be\n specified.\n\n mount_dir\n If img_format is not ``dir``, then the image must be mounted somewhere.\n If the ``mount_dir`` is not specified, then it will be created at\n ``/opt/salt-genesis.``. This directory will be unmounted\n and removed when the process is finished.\n\n pkg_cache\n This points to a directory containing a cache of package files to be\n copied to the image. It does not need to be specified.\n\n pkgs\n A list of packages to be installed on this image. For RedHat, this\n will include ``yum``, ``centos-release`` and ``iputils`` by default.\n\n exclude_pkgs\n A list of packages to be excluded. If you do not want to install the\n defaults, you need to include them in this list.\n\n epel_url\n The URL to download the EPEL release package from.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt myminion genesis.bootstrap pacman /root/arch\n salt myminion genesis.bootstrap rpm /root/redhat\n salt myminion genesis.bootstrap deb /root/wheezy arch=amd64 \\\n flavor=wheezy static_qemu=/usr/bin/qemu-x86_64-static\n\n '''\n if img_format not in ('dir', 'sparse'):\n raise SaltInvocationError('The img_format must be \"sparse\" or \"dir\"')\n\n if img_format == 'dir':\n # We can just use the root as the root\n if not __salt__['file.directory_exists'](root):\n try:\n __salt__['file.mkdir'](root)\n except Exception as exc:\n return {'Error': salt.utils.stringutils.to_unicode(pprint.pformat(exc))}\n elif img_format == 'sparse':\n if not img_size:\n raise SaltInvocationError('An img_size must be specified for a sparse file')\n if not mount_dir:\n mount_dir = '/opt/salt-genesis.{0}'.format(uuid.uuid4())\n __salt__['file.mkdir'](mount_dir, 'root', 'root', '755')\n __salt__['cmd.run'](('fallocate', '-l', img_size, root), python_shell=False)\n _mkpart(root, fs_format, fs_opts, mount_dir)\n\n loop1 = __salt__['cmd.run']('losetup -f')\n log.debug('First loop device is %s', loop1)\n __salt__['cmd.run']('losetup {0} {1}'.format(loop1, root))\n loop2 = __salt__['cmd.run']('losetup -f')\n log.debug('Second loop device is %s', loop2)\n start = six.text_type(2048 * 2048)\n __salt__['cmd.run']('losetup -o {0} {1} {2}'.format(start, loop2, loop1))\n __salt__['mount.mount'](mount_dir, loop2)\n\n _populate_cache(platform, pkg_cache, mount_dir)\n\n if mount_dir:\n root = mount_dir\n\n if pkgs is None:\n pkgs = []\n\n if exclude_pkgs is None:\n exclude_pkgs = []\n\n if platform in ('rpm', 'yum'):\n _bootstrap_yum(\n root,\n pkgs=pkgs,\n exclude_pkgs=exclude_pkgs,\n epel_url=epel_url,\n )\n elif platform == 'deb':\n _bootstrap_deb(\n root,\n arch=arch,\n flavor=flavor,\n repo_url=repo_url,\n static_qemu=static_qemu,\n pkgs=pkgs,\n exclude_pkgs=exclude_pkgs,\n )\n elif platform == 'pacman':\n _bootstrap_pacman(\n root,\n img_format=img_format,\n pkgs=pkgs,\n exclude_pkgs=exclude_pkgs,\n )\n\n if img_format != 'dir':\n blkinfo = __salt__['disk.blkid'](loop2)\n __salt__['file.replace'](\n '{0}/boot/grub/grub.cfg'.format(mount_dir),\n 'ad4103fa-d940-47ca-8506-301d8071d467', # This seems to be the default\n blkinfo[loop2]['UUID']\n )\n __salt__['mount.umount'](root)\n __salt__['cmd.run']('losetup -d {0}'.format(loop2))\n __salt__['cmd.run']('losetup -d {0}'.format(loop1))\n __salt__['file.rmdir'](mount_dir)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _mkpart(root, fs_format, fs_opts, mount_dir):\n '''\n Make a partition, and make it bootable\n\n .. versionadded:: Beryllium\n '''\n __salt__['partition.mklabel'](root, 'msdos')\n loop1 = __salt__['cmd.run']('losetup -f')\n log.debug('First loop device is %s', loop1)\n __salt__['cmd.run']('losetup {0} {1}'.format(loop1, root))\n part_info = __salt__['partition.list'](loop1)\n start = six.text_type(2048 * 2048) + 'B'\n end = part_info['info']['size']\n __salt__['partition.mkpart'](loop1, 'primary', start=start, end=end)\n __salt__['partition.set'](loop1, '1', 'boot', 'on')\n part_info = __salt__['partition.list'](loop1)\n loop2 = __salt__['cmd.run']('losetup -f')\n log.debug('Second loop device is %s', loop2)\n start = start.rstrip('B')\n __salt__['cmd.run']('losetup -o {0} {1} {2}'.format(start, loop2, loop1))\n _mkfs(loop2, fs_format, fs_opts)\n __salt__['mount.mount'](mount_dir, loop2)\n __salt__['cmd.run']((\n 'grub-install',\n '--target=i386-pc',\n '--debug',\n '--no-floppy',\n '--modules=part_msdos linux',\n '--boot-directory={0}/boot'.format(mount_dir),\n loop1\n ), python_shell=False)\n __salt__['mount.umount'](mount_dir)\n __salt__['cmd.run']('losetup -d {0}'.format(loop2))\n __salt__['cmd.run']('losetup -d {0}'.format(loop1))\n return part_info", "response": "Make a partition and make it bootable"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _mkfs(root, fs_format, fs_opts=None):\n '''\n Make a filesystem using the appropriate module\n\n .. versionadded:: Beryllium\n '''\n if fs_opts is None:\n fs_opts = {}\n\n if fs_format in ('ext2', 'ext3', 'ext4'):\n __salt__['extfs.mkfs'](root, fs_format, **fs_opts)\n elif fs_format in ('btrfs',):\n __salt__['btrfs.mkfs'](root, **fs_opts)\n elif fs_format in ('xfs',):\n __salt__['xfs.mkfs'](root, **fs_opts)", "response": "Make a filesystem using the appropriate module\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _populate_cache(platform, pkg_cache, mount_dir):\n '''\n If a ``pkg_cache`` directory is specified, then use it to populate the\n disk image.\n '''\n if not pkg_cache:\n return\n if not os.path.isdir(pkg_cache):\n return\n\n if platform == 'pacman':\n cache_dir = '{0}/var/cache/pacman/pkg'.format(mount_dir)\n\n __salt__['file.mkdir'](cache_dir, 'root', 'root', '755')\n __salt__['file.copy'](pkg_cache, cache_dir, recurse=True, remove_existing=True)", "response": "Populate the cache with the contents of pkg_cache."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbootstrapping an image using the yum tools", "response": "def _bootstrap_yum(\n root,\n pkg_confs='/etc/yum*',\n pkgs=None,\n exclude_pkgs=None,\n epel_url=EPEL_URL,\n ):\n '''\n Bootstrap an image using the yum tools\n\n root\n The root of the image to install to. Will be created as a directory if\n it does not exist. (e.x.: /root/arch)\n\n pkg_confs\n The location of the conf files to copy into the image, to point yum\n to the right repos and configuration.\n\n pkgs\n A list of packages to be installed on this image. For RedHat, this\n will include ``yum``, ``centos-release`` and ``iputils`` by default.\n\n exclude_pkgs\n A list of packages to be excluded. If you do not want to install the\n defaults, you need to include them in this list.\n\n epel_url\n The URL to download the EPEL release package from.\n\n TODO: Set up a pre-install overlay, to copy files into /etc/ and so on,\n which are required for the install to work.\n '''\n if pkgs is None:\n pkgs = []\n elif isinstance(pkgs, six.string_types):\n pkgs = pkgs.split(',')\n\n default_pkgs = ('yum', 'centos-release', 'iputils')\n for pkg in default_pkgs:\n if pkg not in pkgs:\n pkgs.append(pkg)\n\n if exclude_pkgs is None:\n exclude_pkgs = []\n elif isinstance(exclude_pkgs, six.string_types):\n exclude_pkgs = exclude_pkgs.split(',')\n\n for pkg in exclude_pkgs:\n pkgs.remove(pkg)\n\n _make_nodes(root)\n release_files = [rf for rf in os.listdir('/etc') if rf.endswith('release')]\n __salt__['cmd.run']('cp /etc/resolv/conf {rfs} {root}/etc'.format(root=_cmd_quote(root), rfs=' '.join(release_files)))\n __salt__['cmd.run']('cp -r {rfs} {root}/etc'.format(root=_cmd_quote(root), rfs=' '.join(release_files)))\n __salt__['cmd.run']('cp -r {confs} {root}/etc'.format(root=_cmd_quote(root), confs=_cmd_quote(pkg_confs)))\n\n yum_args = ['yum', 'install', '--installroot={0}'.format(_cmd_quote(root)), '-y'] + pkgs\n __salt__['cmd.run'](yum_args, python_shell=False)\n\n if 'epel-release' not in exclude_pkgs:\n __salt__['cmd.run'](\n ('rpm', '--root={0}'.format(_cmd_quote(root)), '-Uvh', epel_url),\n python_shell=False\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nbootstrapping an image using the Debian tools.", "response": "def _bootstrap_deb(\n root,\n arch,\n flavor,\n repo_url=None,\n static_qemu=None,\n pkgs=None,\n exclude_pkgs=None,\n ):\n '''\n Bootstrap an image using the Debian tools\n\n root\n The root of the image to install to. Will be created as a directory if\n it does not exist. (e.x.: /root/wheezy)\n\n arch\n Architecture of the target image. (e.x.: amd64)\n\n flavor\n Flavor of Debian to install. (e.x.: wheezy)\n\n repo_url\n Base URL for the mirror to install from.\n (e.x.: http://ftp.debian.org/debian/)\n\n static_qemu\n Local path to the static qemu binary required for this arch.\n (e.x.: /usr/bin/qemu-amd64-static)\n\n pkgs\n A list of packages to be installed on this image.\n\n exclude_pkgs\n A list of packages to be excluded.\n '''\n\n if repo_url is None:\n repo_url = 'http://ftp.debian.org/debian/'\n\n if not salt.utils.path.which('debootstrap'):\n log.error('Required tool debootstrap is not installed.')\n return False\n\n if static_qemu and not salt.utils.validate.path.is_executable(static_qemu):\n log.error('Required tool qemu not present/readable at: %s', static_qemu)\n return False\n\n if isinstance(pkgs, (list, tuple)):\n pkgs = ','.join(pkgs)\n if isinstance(exclude_pkgs, (list, tuple)):\n exclude_pkgs = ','.join(exclude_pkgs)\n\n deb_args = [\n 'debootstrap',\n '--foreign',\n '--arch',\n _cmd_quote(arch)]\n\n if pkgs:\n deb_args += ['--include', _cmd_quote(pkgs)]\n if exclude_pkgs:\n deb_args += ['--exclude', _cmd_quote(exclude_pkgs)]\n\n deb_args += [\n _cmd_quote(flavor),\n _cmd_quote(root),\n _cmd_quote(repo_url),\n ]\n\n __salt__['cmd.run'](deb_args, python_shell=False)\n\n if static_qemu:\n __salt__['cmd.run'](\n 'cp {qemu} {root}/usr/bin/'.format(\n qemu=_cmd_quote(static_qemu), root=_cmd_quote(root)\n )\n )\n\n env = {'DEBIAN_FRONTEND': 'noninteractive',\n 'DEBCONF_NONINTERACTIVE_SEEN': 'true',\n 'LC_ALL': 'C',\n 'LANGUAGE': 'C',\n 'LANG': 'C',\n 'PATH': '/sbin:/bin:/usr/bin'}\n __salt__['cmd.run'](\n 'chroot {root} /debootstrap/debootstrap --second-stage'.format(\n root=_cmd_quote(root)\n ),\n env=env\n )\n __salt__['cmd.run'](\n 'chroot {root} dpkg --configure -a'.format(\n root=_cmd_quote(root)\n ),\n env=env\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _bootstrap_pacman(\n root,\n pkg_confs='/etc/pacman*',\n img_format='dir',\n pkgs=None,\n exclude_pkgs=None,\n ):\n '''\n Bootstrap an image using the pacman tools\n\n root\n The root of the image to install to. Will be created as a directory if\n it does not exist. (e.x.: /root/arch)\n\n pkg_confs\n The location of the conf files to copy into the image, to point pacman\n to the right repos and configuration.\n\n img_format\n The image format to be used. The ``dir`` type needs no special\n treatment, but others need special treatment.\n\n pkgs\n A list of packages to be installed on this image. For Arch Linux, this\n will include ``pacman``, ``linux``, ``grub``, and ``systemd-sysvcompat``\n by default.\n\n exclude_pkgs\n A list of packages to be excluded. If you do not want to install the\n defaults, you need to include them in this list.\n '''\n _make_nodes(root)\n\n if pkgs is None:\n pkgs = []\n elif isinstance(pkgs, six.string_types):\n pkgs = pkgs.split(',')\n\n default_pkgs = ('pacman', 'linux', 'systemd-sysvcompat', 'grub')\n for pkg in default_pkgs:\n if pkg not in pkgs:\n pkgs.append(pkg)\n\n if exclude_pkgs is None:\n exclude_pkgs = []\n elif isinstance(exclude_pkgs, six.string_types):\n exclude_pkgs = exclude_pkgs.split(',')\n\n for pkg in exclude_pkgs:\n pkgs.remove(pkg)\n\n if img_format != 'dir':\n __salt__['mount.mount']('{0}/proc'.format(root), '/proc', fstype='', opts='bind')\n __salt__['mount.mount']('{0}/dev'.format(root), '/dev', fstype='', opts='bind')\n\n __salt__['file.mkdir'](\n '{0}/var/lib/pacman/local'.format(root), 'root', 'root', '755'\n )\n pac_files = [rf for rf in os.listdir('/etc') if rf.startswith('pacman.')]\n for pac_file in pac_files:\n __salt__['cmd.run']('cp -r /etc/{0} {1}/etc'.format(pac_file, _cmd_quote(root)))\n __salt__['file.copy']('/var/lib/pacman/sync', '{0}/var/lib/pacman/sync'.format(root), recurse=True)\n\n pacman_args = ['pacman', '--noconfirm', '-r', _cmd_quote(root), '-S'] + pkgs\n __salt__['cmd.run'](pacman_args, python_shell=False)\n\n if img_format != 'dir':\n __salt__['mount.umount']('{0}/proc'.format(root))\n __salt__['mount.umount']('{0}/dev'.format(root))", "response": "Bootstrap an image using pacman tools"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _make_nodes(root):\n '''\n Make the minimum number of nodes inside of /dev/. Based on:\n\n https://wiki.archlinux.org/index.php/Linux_Containers\n '''\n dirs = (\n ('{0}/etc'.format(root), 'root', 'root', '755'),\n ('{0}/dev'.format(root), 'root', 'root', '755'),\n ('{0}/proc'.format(root), 'root', 'root', '755'),\n ('{0}/dev/pts'.format(root), 'root', 'root', '755'),\n ('{0}/dev/shm'.format(root), 'root', 'root', '1755'),\n )\n\n nodes = (\n ('{0}/dev/null'.format(root), 'c', 1, 3, 'root', 'root', '666'),\n ('{0}/dev/zero'.format(root), 'c', 1, 5, 'root', 'root', '666'),\n ('{0}/dev/random'.format(root), 'c', 1, 8, 'root', 'root', '666'),\n ('{0}/dev/urandom'.format(root), 'c', 1, 9, 'root', 'root', '666'),\n ('{0}/dev/tty'.format(root), 'c', 5, 0, 'root', 'root', '666'),\n ('{0}/dev/tty0'.format(root), 'c', 4, 0, 'root', 'root', '666'),\n ('{0}/dev/console'.format(root), 'c', 5, 1, 'root', 'root', '600'),\n ('{0}/dev/full'.format(root), 'c', 1, 7, 'root', 'root', '666'),\n ('{0}/dev/initctl'.format(root), 'p', 0, 0, 'root', 'root', '600'),\n ('{0}/dev/ptmx'.format(root), 'c', 5, 2, 'root', 'root', '666'),\n )\n\n for path in dirs:\n __salt__['file.mkdir'](*path)\n\n for path in nodes:\n __salt__['file.mknod'](*path)", "response": "Make the minimum number of nodes inside of the Linux containers."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef avail_platforms():\n '''\n Return which platforms are available\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion genesis.avail_platforms\n '''\n ret = {}\n for platform in CMD_MAP:\n ret[platform] = True\n for cmd in CMD_MAP[platform]:\n if not salt.utils.path.which(cmd):\n ret[platform] = False\n return ret", "response": "Return which platforms are available"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\npacking up a directory structure into a specific format", "response": "def pack(name, root, path=None, pack_format='tar', compress='bzip2'):\n '''\n Pack up a directory structure, into a specific format\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt myminion genesis.pack centos /root/centos\n salt myminion genesis.pack centos /root/centos pack_format='tar'\n '''\n if pack_format == 'tar':\n _tar(name, root, path, compress)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef unpack(name, dest=None, path=None, pack_format='tar', compress='bz2'):\n '''\n Unpack an image into a directory structure\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion genesis.unpack centos /root/centos\n '''\n if pack_format == 'tar':\n _untar(name, dest, path, compress)", "response": "Unpack an image into a directory structure"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\npack up a single image in a tar format", "response": "def _tar(name, root, path=None, compress='bzip2'):\n '''\n Pack up image in a tar format\n '''\n if path is None:\n path = os.path.join(salt.syspaths.BASE_FILE_ROOTS_DIR, 'img')\n if not __salt__['file.directory_exists'](path):\n try:\n __salt__['file.mkdir'](path)\n except Exception as exc:\n return {'Error': salt.utils.stringutils.to_unicode(pprint.pformat(exc))}\n\n compression, ext = _compress(compress)\n\n tarfile = '{0}/{1}.tar.{2}'.format(path, name, ext)\n out = __salt__['archive.tar'](\n options='{0}pcf'.format(compression),\n tarfile=tarfile,\n sources='.',\n dest=root,\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef mksls(fmt, src, dst=None):\n '''\n Convert an installation file/script to an SLS file. Currently supports\n ``kickstart``, ``preseed``, and ``autoyast``.\n\n CLI Examples:\n\n salt genesis.mksls kickstart /path/to/kickstart.cfg\n salt genesis.mksls kickstart /path/to/kickstart.cfg /path/to/dest.sls\n\n .. versionadded:: Beryllium\n '''\n if fmt == 'kickstart':\n return salt.utils.kickstart.mksls(src, dst)\n elif fmt == 'preseed':\n return salt.utils.preseed.mksls(src, dst)\n elif fmt == 'autoyast':\n return salt.utils.yast.mksls(src, dst)", "response": "Convert an installation file or script to an SLS file. Currently supports\n kickstart preseed and autoyast."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nexecuting the desired powershell command and ensure that it returns data in json format and load that into python", "response": "def _pshell(cmd, cwd=None, json_depth=2):\n '''\n Execute the desired powershell command and ensure that it returns data\n in json format and load that into python\n '''\n if 'convertto-json' not in cmd.lower():\n cmd = '{0} | ConvertTo-Json -Depth {1}'.format(cmd, json_depth)\n log.debug('DSC: %s', cmd)\n results = __salt__['cmd.run_all'](cmd, shell='powershell', cwd=cwd, python_shell=True)\n\n if 'pid' in results:\n del results['pid']\n\n if 'retcode' not in results or results['retcode'] != 0:\n # run_all logs an error to log.error, fail hard back to the user\n raise CommandExecutionError('Issue executing powershell {0}'.format(cmd), info=results)\n\n try:\n ret = salt.utils.json.loads(results['stdout'], strict=False)\n except ValueError:\n raise CommandExecutionError('No JSON results from powershell', info=results)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef avail_modules(desc=False):\n '''\n List available modules in registered Powershell module repositories.\n\n :param desc: If ``True``, the verbose description will be returned.\n :type desc: ``bool``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'win01' psget.avail_modules\n salt 'win01' psget.avail_modules desc=True\n '''\n cmd = 'Find-Module'\n modules = _pshell(cmd)\n names = []\n if desc:\n names = {}\n for module in modules:\n if desc:\n names[module['Name']] = module['Description']\n continue\n names.append(module['Name'])\n return names", "response": "Return a list of available Powershell modules."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_modules(desc=False):\n '''\n List currently installed PSGet Modules on the system.\n\n :param desc: If ``True``, the verbose description will be returned.\n :type desc: ``bool``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'win01' psget.list_modules\n salt 'win01' psget.list_modules desc=True\n '''\n cmd = 'Get-InstalledModule'\n modules = _pshell(cmd)\n if isinstance(modules, dict):\n ret = []\n if desc:\n modules_ret = {}\n modules_ret[modules['Name']] = copy.deepcopy(modules)\n modules = modules_ret\n return modules\n ret.append(modules['Name'])\n return ret\n names = []\n if desc:\n names = {}\n for module in modules:\n if desc:\n names[module['Name']] = module\n continue\n names.append(module['Name'])\n return names", "response": "List currently installed PSGet Modules on the system."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ninstalling a Powershell module from powershell gallery on the system.", "response": "def install(name, minimum_version=None, required_version=None, scope=None,\n repository=None):\n '''\n Install a Powershell module from powershell gallery on the system.\n\n :param name: Name of a Powershell module\n :type name: ``str``\n\n :param minimum_version: The maximum version to install, e.g. 1.23.2\n :type minimum_version: ``str``\n\n :param required_version: Install a specific version\n :type required_version: ``str``\n\n :param scope: The scope to install the module to, e.g. CurrentUser, Computer\n :type scope: ``str``\n\n :param repository: The friendly name of a private repository, e.g. MyREpo\n :type repository: ``str``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'win01' psget.install PowerPlan\n '''\n # Putting quotes around the parameter protects against command injection\n flags = [('Name', name)]\n\n if minimum_version is not None:\n flags.append(('MinimumVersion', minimum_version))\n if required_version is not None:\n flags.append(('RequiredVersion', required_version))\n if scope is not None:\n flags.append(('Scope', scope))\n if repository is not None:\n flags.append(('Repository', repository))\n params = ''\n for flag, value in flags:\n params += '-{0} {1} '.format(flag, value)\n cmd = 'Install-Module {0} -Force'.format(params)\n _pshell(cmd)\n return name in list_modules()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating a Powershell module with a specific version or the newest one.", "response": "def update(name, maximum_version=None, required_version=None):\n '''\n Update a PowerShell module to a specific version, or the newest\n\n :param name: Name of a Powershell module\n :type name: ``str``\n\n :param maximum_version: The maximum version to install, e.g. 1.23.2\n :type maximum_version: ``str``\n\n :param required_version: Install a specific version\n :type required_version: ``str``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'win01' psget.update PowerPlan\n '''\n # Putting quotes around the parameter protects against command injection\n flags = [('Name', name)]\n\n if maximum_version is not None:\n flags.append(('MaximumVersion', maximum_version))\n if required_version is not None:\n flags.append(('RequiredVersion', required_version))\n\n params = ''\n for flag, value in flags:\n params += '-{0} {1} '.format(flag, value)\n cmd = 'Update-Module {0} -Force'.format(params)\n _pshell(cmd)\n return name in list_modules()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef remove(name):\n '''\n Remove a Powershell DSC module from the system.\n\n :param name: Name of a Powershell DSC module\n :type name: ``str``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'win01' psget.remove PowerPlan\n '''\n # Putting quotes around the parameter protects against command injection\n cmd = 'Uninstall-Module \"{0}\"'.format(name)\n no_ret = _pshell(cmd)\n return name not in list_modules()", "response": "Remove a Powershell DSC module from the system."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nregistering a PSGet repository on the local machine", "response": "def register_repository(name, location, installation_policy=None):\n '''\n Register a PSGet repository on the local machine\n\n :param name: The name for the repository\n :type name: ``str``\n\n :param location: The URI for the repository\n :type location: ``str``\n\n :param installation_policy: The installation policy\n for packages, e.g. Trusted, Untrusted\n :type installation_policy: ``str``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'win01' psget.register_repository MyRepo https://myrepo.mycompany.com/packages\n '''\n # Putting quotes around the parameter protects against command injection\n flags = [('Name', name)]\n\n flags.append(('SourceLocation', location))\n if installation_policy is not None:\n flags.append(('InstallationPolicy', installation_policy))\n\n params = ''\n for flag, value in flags:\n params += '-{0} {1} '.format(flag, value)\n cmd = 'Register-PSRepository {0}'.format(params)\n no_ret = _pshell(cmd)\n return name not in list_modules()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_repository(name):\n '''\n Get the details of a local PSGet repository\n\n :param name: Name of the repository\n :type name: ``str``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'win01' psget.get_repository MyRepo\n '''\n # Putting quotes around the parameter protects against command injection\n cmd = 'Get-PSRepository \"{0}\"'.format(name)\n no_ret = _pshell(cmd)\n return name not in list_modules()", "response": "Get the details of a local PSGet repository"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a new container with the specified parameters.", "response": "def present(name,\n running=None,\n clone_from=None,\n snapshot=False,\n profile=None,\n network_profile=None,\n template=None,\n options=None,\n image=None,\n config=None,\n fstype=None,\n size=None,\n backing=None,\n vgname=None,\n lvname=None,\n thinpool=None,\n path=None):\n '''\n .. versionchanged:: 2015.8.0\n\n The :mod:`lxc.created ` state has been renamed\n to ``lxc.present``, and the :mod:`lxc.cloned `\n state has been merged into this state.\n\n Create the named container if it does not exist\n\n name\n The name of the container to be created\n\n path\n path to the container parent\n default: /var/lib/lxc (system default)\n\n .. versionadded:: 2015.8.0\n\n\n running : False\n * If ``True``, ensure that the container is running\n * If ``False``, ensure that the container is stopped\n * If ``None``, do nothing with regards to the running state of the\n container\n\n .. versionadded:: 2015.8.0\n\n clone_from\n Create named container as a clone of the specified container\n\n snapshot : False\n Use Copy On Write snapshots (LVM). Only supported with ``clone_from``.\n\n profile\n Profile to use in container creation (see the :ref:`LXC Tutorial\n ` for more information). Values in a\n profile will be overridden by the parameters listed below.\n\n network_profile\n Network Profile to use in container creation\n (see the :ref:`LXC Tutorial `\n for more information). Values in a profile will be overridden by\n the parameters listed below.\n\n .. versionadded:: 2015.5.2\n\n **Container Creation Arguments**\n\n template\n The template to use. For example, ``ubuntu`` or ``fedora``.\n For a full list of available templates, check out\n the :mod:`lxc.templates ` function.\n\n Conflicts with the ``image`` argument.\n\n .. note::\n\n The ``download`` template requires the following three parameters\n to be defined in ``options``:\n\n * **dist** - The name of the distribution\n * **release** - Release name/version\n * **arch** - Architecture of the container\n\n The available images can be listed using the :mod:`lxc.images\n ` function.\n\n options\n\n .. versionadded:: 2015.5.0\n\n Template-specific options to pass to the lxc-create command. These\n correspond to the long options (ones beginning with two dashes) that\n the template script accepts. For example:\n\n .. code-block:: yaml\n\n web01:\n lxc.present:\n - template: download\n - options:\n dist: centos\n release: 6\n arch: amd64\n\n Remember to double-indent the options, due to :ref:`how PyYAML works\n `.\n\n For available template options, refer to the lxc template scripts\n which are ususally located under ``/usr/share/lxc/templates``,\n or run ``lxc-create -t